0

I am writing a bash script that asks the user for a file name, then gives the user the option to copy, move or delete a file. Unfortunately when I execute the script, I keep getting errors saying "./Script3.txt: line 19: [: ==: unary operator expected". I have tried putting double quotes around my $option variables; as seen on stackoverflow, as well as using x"$option" but am still not understanding why I'm getting these errors.


echo "Please enter a file name: "
read fileName

if [ -f $fileName ]; then
        echo "Enter C, M or D to Copy, Move or Delete a file: "
        read fileName

        if [ $option == "C" ]; then
        echo "Where would you like to copy $fileName to?: "
        read folderName

                if [ -d $folderName ]; then
                        cp $fileName $folderName
                        echo "$fileName has been copied to $folderName"
                fi
        fi
        elif [ $option == "M" ]; then
                echo "Where would you like to move $fileName to?: "
                read folderName

                if [ -d $folderName ]; then 
                        mv $fileName $folderName

                        echo "$fileName moved to $folderName"
                fi
        elif [ $option == "D" ]; then
                echo "You are about to delete a file"
                rm $fileName

        else
                echo "This is an invalid option. Please enter C, M or D"
fi
fatmink
  • 1
  • 3
  • You never set `option`. The second `read fileName` should be `read option`. You should also quote all your variables. – Barmar Feb 20 '20 at 20:50
  • 3
    See http://shellcheck.net. If you want to use `==`, use `[[ ... ]]`. If you don't want to quote your parameter expansion, use `[[ ... ]]`. Otherwise, use `[ "$option" = "C" ]`. – chepner Feb 20 '20 at 20:54
  • Make sure you have a shebang in the first line of your script. Most probably the script is called with sh not bash interpreter. Just put #!/bin/bash in the first line. – Vicctor Feb 20 '20 at 21:25
  • 1
    @Vicctor, even with bash, `[ $option == "M" ]` is not correct, as the value in `$option` is subject to string-splitting and glob expansion; it would need to be `[ "$option" == M ]`, and really *should* be `[ "$option" = M ]` (using the POSIX-compliant `=` [`test`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) operator rather than the `==` bashism) regardless. – Charles Duffy Feb 20 '20 at 21:34
  • @Barmar thank you so much I appreciate the help. Totally should have caught that – fatmink Feb 21 '20 at 02:55
  • @CharlesDuffy Thank you so much for the help. It worked! I actually only did that to the $option variables, however, I didn't do it to fileName or folderName. Is there a specific reason the program still worked correctly? Or should I put these in quotes as well just to be safe? – fatmink Feb 21 '20 at 02:58
  • 1
    If the filename doesn't have spaces, you won't notice the problem. – Barmar Feb 21 '20 at 02:59
  • 1
    In general, quote your variables unless you have a good reason not to. – Barmar Feb 21 '20 at 02:59

0 Answers0