doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

nested 'if' statements in a shell script?

Thu Apr 11, 2019 10:48 am

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


User avatar
RaTTuS
Posts: 10323
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: nested 'if' statements?

Thu Apr 11, 2019 10:55 am

How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
B.Goode
Posts: 7870
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: nested 'if' statements?

Thu Apr 11, 2019 11:02 am

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... )

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: nested 'if' statements?

Thu Apr 11, 2019 11:03 am

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
Last edited by doubleudee1 on Thu Apr 11, 2019 11:09 am, edited 2 times in total.

User avatar
RaTTuS
Posts: 10323
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: nested 'if' statements?

Thu Apr 11, 2019 11:07 am

no you can have elif , you just need to get else correct
bash is very particular about spaces
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Roken
Posts: 282
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

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

Thu Apr 11, 2019 6:41 pm

case may be a better solution for this: https://www.tldp.org/LDP/Bash-Beginners ... 07_03.html
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

epoch1970
Posts: 3064
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

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

Thu Apr 11, 2019 7:01 pm

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.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “Other programming languages”