4

I'll get to the meat and bones:

MY_VAR=6
until [$MY_VAR = 0]
do
dir/dir_$MY_VAR.log
ps | grep "NAME_$MY_VAR.ksh"
check some things
if [results = ok]
echo "program $MY_VAR sucessful"
else
echo "program $MY_VAR failure"
MY_VAR = `expr $MY_VAR - 1`
done

Now I am getting the following errors MY_VAR not found and [6: not found, so I'm assuming a rather noobish mistake. I feel the logic is sound enough just a simple syntax error I am making somewhere by the looks of the two errors I think it could be in the declaration.

2 Answers 2

3

You need to have a space after [ and before ] since [ is actually a command and not a delimiter.

Here is your script re-written in Bash (or ksh):

my_var=6
until ((my_var == 0))
do
    dir/dir_$my_var.log    # I have no idea what this is supposed to be
    ps | grep "NAME_$my_var.ksh"
    # check some things
    if [[ $results = ok ]]
    then
        echo "program $my_var successful"
    else
        echo "program $my_var failure"
        ((my_var--))
    fi
done

However:

for my_var in {6..1}
do
    dir/dir_$my_var.log    # I have no idea what this is supposed to be
    ps | grep "NAME_$my_var.ksh"
    # check some things
    if [[ $results = ok ]]
    then
        echo "program $my_var successful"
    else
        echo "program $my_var failure"
    fi
done
Sign up to request clarification or add additional context in comments.

3 Comments

I get the error MY_VAR--: more tokens expected, could you elaborate on your solution such as the use of (()) and [[]]
@Jewsef: Using which shell and on which line of which version of the scripts from my answer? One thing to note, I had changed the case of the variable name, but I did so inconsistently. That may have caused some trouble. Sorry about that - I've fixed it. In Bash and ksh double square brackets enable some additional capabilities. Using double parentheses for numeric comparisons allows you to use == and >, for example, instead of the ugly -eq and -gt. It also allows you to do arithmetic operations such as the decrement ((my_var--)) shown.
@Jewsef: I just noticed that the script in your question is missing the fi to close the if. I copied your script into my answer and missed fixing that (now fixed). That could be the source of your error.
0

Your two errors are caused by:

  • until [$MY_VAR = 0]
  • MY_VAR = $(expr $MY_VAR - 1)

[I've used $() instead of backticks because I couldn't get backticks into the code section]

The first problem is the lack of spaces around the square brackets - on both ends. The shell is looking for the command [6 (after expanding $MY_VAR), instead of [ (have a look at /usr/bin/[ - it's actually a program). You should also use -eq to do numeric comparisons. = should work ok here, but leading zeros can break a string comparison where a numeric comparison would work:

until [ "$MY_VAR" -eq 0 ]

The second problem is you have spaces in your variable assignment. When you write MY_VAR = ... the shell is looking for the command MY_VAR. Instead write it as:

MY_VAR=`expr $MY_VAR - 1`

These answers directly answer your questions, but you should study Dennis Williamson's answer for better ways to do these things.

1 Comment

It's true that [ exists as /usr/bin/[, but it's a shell builtin, too, and as such has precedence. Be careful of relying on the leading zero behavior, [ 008 -eq 8 ] works, but [[ 008 -eq 8 ]] gives a value too great for base error. Backticks should be avoided anyway, use of $() is preferred.

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.