Using shell script

I will get to 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 get the following errors: MY_VAR not found, and [6: not found, so I assume a rather unnecessary error. I feel that the logic is reasonably justified by a simple, simple syntax error, which I make somewhere as a result of two errors that, I think, may be in the declaration.

+4
source share
2 answers

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

Here is your script rewritten 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 

But:

 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 
+3
source

Your two errors are caused by:

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

[I used $ () instead of backticks because I could not get backlinks in the code section]

The first problem is the lack of spaces around the square brackets - at both ends. The shell looks for the command [6 (after the expansion of $MY_VAR ) instead of [ (see /usr/bin/[ - this is actually a program). You should also use -eq to do numerical comparisons. = should work fine here, but leading zeros can disrupt the string comparison where the numerical comparison will be performed:

 until [ "$MY_VAR" -eq 0 ] 

Second problem: you have spaces in the variable assignment. When you write MY_VAR = ... , the shell looks for the command MY_VAR . Instead, write as:

 MY_VAR=`expr $MY_VAR - 1` 

These answers directly answer your questions, but you should study Dennis Williamson's answer for a better way to do this.

0
source

Source: https://habr.com/ru/post/1334535/


All Articles