0

I have a working piece of code that I cant integrate into a for loop. The code updates the first 51 lines of each .html file in the directory to match the index.html file. This is the code that is working:

    tmp=$(mktemp) && 
    { head -n 51 index.html; tail -n +52 alt.html; } > "$tmp" &&
    mv -- "$tmp"  alt.html

This is the for loop that isn't working:

FILES="/run/media/george/General/testing/*.html"

for f in $FILES;
do
    if $f = index.html
    then
    echo "skip main file"
    else    
        echo "Updating $f file..."
        tmp=$(mktemp) && 
        { head -n 51 index.html; tail -n +52 $f; } > "$tmp" &&
        mv -- "$tmp" $f
    fi
done

This code just deletes the code in all the html files. What have I done wrong in this loop?

  • In case it's not obvious, your code replaces the first 51 lines with the contents of `index.html` in the _current_ directory. If that happens to be an empty file, that would explain the problem. – tripleee Aug 23 '21 at 18:39
  • 1
    As an aside, storing the wildcard string in a variable is just pure, unadulterated waste of memory. (Also, [avoid uppercase for your private variables.)](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – tripleee Aug 23 '21 at 18:41
  • 1
    The `if` statement's syntax is wrong. Probably try http://shellcheck.net/ – tripleee Aug 23 '21 at 18:42
  • And put your entire code (not just a snippet) through [shellcheck](https://shellcheck.net) ... it bemoans your comparison ... https://github.com/koalaman/shellcheck/wiki/SC2283 – tink Aug 23 '21 at 18:42
  • `/run/media` is a really weird place to keep your private files. – tripleee Aug 23 '21 at 18:42
  • As a further aside, you are creating a boatload of temporary files which never get removed. – tripleee Aug 23 '21 at 18:51
  • Ah, thank you. Of course it was something stupid like that. I also needed to add [] around the if statement for it to work. I will post an answer to this thread with the working code. – George Johnson Aug 23 '21 at 18:56
  • /run/media is where drives are mounted by default in Manjaro. This project folder is stored on a HDD, not my boot drive. – George Johnson Aug 23 '21 at 19:09
  • To narrow down a bit on the comment by tripleee: The `if` statement is not **syntactically** wrong, it is **semantically** nonsense: You ask the shell here to run `$f` as a program with the arguments `=` and `index.html`, and based on the exit code, take either the _then_ part or the _else_ part. This is the general syntax of `if`: The statement `if SOMETHING a b c` executes the program _SOMETHING_ with arguments a, b, c and branches on the outcom (= exit code). – user1934428 Aug 24 '21 at 06:17

1 Answers1

0

Problem solved by doing the if statement properly and running the script in the target directory instead of using a file path. Here is the new code that works:

FILES="*.html"

for f in $FILES;
do
    if [ $f = "index.html" ];
    then
    echo "Skipping index.html"
    else    
        echo "Updating $f file..."
        tmp=$(mktemp) && 
        { head -n 51 index.html; tail -n +52 $f; } > "$tmp" &&
        mv -- "$tmp" $f
    fi
done