Page 1 of 1

nested 'if' statements in a shell script?

Posted: Thu Apr 11, 2019 10:48 am
by doubleudee1
I want to move some files from one directory to another, I have the names of the diectories in variables, but initally I am testing it with the code below but am getting the errors show.
How is nesting achived so i can run a statement such as below (the loop to change files & directories is not shown but that does work to chnge them as required)

This is my test code:

Code: Select all

aa="201901"
bb="201902"
cc="201903"


if [ ${j} == ${aa} ]
 then echo "aa moved"  ##mv  "${m}" "${n}"        #########commected out for testing
  elif [ ${j} == ${bb} ]
 then echo "bb moved"  ##mv  "${m}" "$[n}"        #########commected out for testing
  elif [ ${j} == ${cc} ]
 then echo "cc moved"  ##mv  "${m}" "${n}"         #########commected out for testing
##  elif j == "201904" mv -1 "{l}" "n"
##  elif j == "201905" mv -1 "{l}" "n"
##  elif j == "201906" mv -1 "{l}" "n"
##  elif j == "201907" mv -1 "{l}" "n"
##  elif j == "201908" mv -1 "{l}" "n"
##  elif j == "201909" mv -1 "{l}" "n"
##  elif j == "201910" mv -1 "{l}" "n"
##  elif j == "201911" mv -1 "{l}" "n"
##  elif j == "201912" mv -1 "{l}" "n"
else: echo "No Files Left To Move"
fi
############# OUTPUTTED errors in above #############
cc moved
./CreatejpgDirs: line 44: else:: command not found


Re: nested 'if' statements?

Posted: Thu Apr 11, 2019 10:55 am
by RaTTuS

Re: nested 'if' statements?

Posted: Thu Apr 11, 2019 11:02 am
by B.Goode
This is a shell script?

See the documentation in man bash

This is the description for if..then..else
if list; then list; [ elif list; then list; ] ... [
else list; ] fi
The if list is executed. If its exit status
is zero, the then list is executed. Other‐
wise, each elif list is executed in turn, and
if its exit status is zero, the corresponding
then list is executed and the command com‐
pletes. Otherwise, the else list is exe‐
cuted, if present. The exit status is the
exit status of the last command executed, or
zero if no condition tested true.

Maybe it is the extraneous ':' that is the stumbling block?

(Turns out someone else has pointed this out more pithily while I was researching... )

Re: nested 'if' statements?

Posted: Thu Apr 11, 2019 11:03 am
by doubleudee1
RaTTuS wrote:
Thu Apr 11, 2019 10:55 am
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
else:
is not else
So I have to have one 'if' ....... 'fi' statement for every change I want to make?
I only used the elif as I thought I could nest all changes in one 'if' ...... 'fi' statement, or is there an alternataive statement I should be using to make all the changes within it?

Sorry, just saw second reply

so I can do

Code: Select all

aa="201901"
bb="201902"
cc="201903"


if [ ${j} == ${aa} ]
 then echo "aa moved"  ##mv  "${m}" "${n}"        #########commected out for testing
  [ ${j} == ${bb} ]
 then echo "bb moved"  ##mv  "${m}" "$[n}"        #########commected out for testing
  [ ${j} == ${cc} ]
 then echo "cc moved"  ##mv  "${m}" "${n}"         #########commected out for testing
 else echo "No Files Left To Move"
fi

Re: nested 'if' statements?

Posted: Thu Apr 11, 2019 11:07 am
by RaTTuS
no you can have elif , you just need to get else correct
bash is very particular about spaces

Re: nested 'if' statements in a shell script?

Posted: Thu Apr 11, 2019 6:41 pm
by Roken
case may be a better solution for this: https://www.tldp.org/LDP/Bash-Beginners ... 07_03.html

Re: nested 'if' statements in a shell script?

Posted: Thu Apr 11, 2019 7:01 pm
by epoch1970
Agreed, case is more portable, more readable and executes faster.

Code: Select all

case "${j}" in
	201901)
		echo "aa moved"
		;;
	201902)
		echo "bb moved"
		;;
	201903)
		echo "cc moved"
		;;
	*)
		echo "${j}: No Files Left To Move"
		;;
esac
Its only weakness is limited pattern-matching capability, e.g. patterns like "20190?)" or "2019*)" are ok but much more.
Nested case statements are possible and still sort-of-readable.