0

I have this script that does different things depending on the parameters you put as the input:

        #!/bin/bash
cadena="ls -alis"
while [ $# -e 0 ]
do
    case $1 in
        -p) [ $cadena = "$cadena | grep $2" ] ;;
        -o) [ if [ $2 = 'mida' ] then
                $cadena="$cadena -lS"
              elif [ $2 = 'inode' ] then
                $cadena="$cadena | sort -t 1" 
              fi
            ] ;;
        -d) [ if [ `expr substr $2 1 1`” = '/' ] then
                $cadena="$cadena $2"
              elif [ `expr substr $2 1 1`” != '/' ] then
                $cadena="$cadena `pwd`/$2" 
              fi
            ] ;;
        -s) [ $cadena="$cadena > $2" ] ;;
        shift 2
    esac
done
$cadena

Updated the code with case. The error is the same one a the beginning, in the line 9 with the elif [ $2 = 'inode' ] then statement, it says incorrect token.

5
  • 1
    The second line is wrong: [$# -e 0] needs spaces around the brackets. In general, all [ ] need spaces around, so cross check it because I see more more. Commented Apr 3, 2014 at 11:39
  • You're right, I updated it but the error wasn't from there, any suggestions? Commented Apr 3, 2014 at 11:41
  • 1
    Apparently you want to store the output of ls -alis --> cadena=$(ls -alis) Commented Apr 3, 2014 at 11:42
  • I don't really need to save it, I just need to execute it. Edited the code. Commented Apr 3, 2014 at 11:50
  • If you do cadena="ls -alis" you are storing a string. If you want this command to be executed then you need to use cadena=$(ls -alis). Commented Apr 3, 2014 at 11:51

2 Answers 2

2
  1. Don't mix single bracket notation [ ] with ==, it's [ ... = ... ] or [[ ... == ... ]].

  2. You don't need two shifts, you can shift 2 instead (if this is really what you want).

  3. You can replace the outer if with a switch for readability and avoid the nested if mess:

    case $1 in
        -p) [...] ;;
        -o) [...] ;;
        -d) [...] ;;
        -s) [...] ;;
        *)  [...] ;;
    esac
    
  4. You will want to read BashFAQ/050 aka I'm trying to put a command in a variable, but the complex cases always fail!.

Sign up to request clarification or add additional context in comments.

3 Comments

I really like this format. I updated the code, however it still gives me errors that I cannot fix. Can you give me a hand?
You need two colons ;; (this is the shell equivalent of a required break statement and your if-elif constructs need a closing fi. Ok, there is actually more wrong with your code...maybe you want to read a bash tutorial first? Or at least BashGuide/TestsAndConditionals.
Yeah... I guess I'll have to read that because I really don't understand what's wrong with the elif statement. I've looked up the web and I find it in this format. Thank anyway!
0

At the very least you need a fi to close each of those inner if statements.

The first line of the script has spaces before the #!. If it really looks like that, then you might be picking up a different shell.

But the syntax error is caused by having the then on the same line as the if. The shell only recognizes reserved words at the beginning of a line or equivalent (e.g. after a semicolon). So you want:

if [ $2 = 'mida' ]
then

or

if [ $2 = 'mida' ]; then

2 Comments

I think it is fine there. But after elif [ $2 == 'inode' ] then he does need one fi, indeed.
I edited the code because after doing this and adding the fi at the end of every elif it still didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.