LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2020, 12:08 PM   #1
Pinguino99
Member
 
Registered: Dec 2019
Posts: 41

Rep: Reputation: Disabled
Alternative to elif in shell script?


I created a function to check if the directories and files exist but it is too big, is there any other alternative?

Code:
_CHECK(){
if [[ ! -d "${DIR1}" ]]; then
echo "$DIR1 does not exist."
exit 1
elif [[ ! -d "${DIR2}" ]]; then
echo "$DIR2 does not exist."
exit 1
elif [[ ! -d "${DIR3}" ]]; then
echo "$DIR3 does not exist."
exit 1
elif [[ ! -d "${DIR4}" ]]; then
echo "$DIR4 does not exist."
exit 1
elif [[ ! -d "${DIR5}" ]]; then
echo "${DIR5} does not exist."
exit 1
elif [[ ! -e "${FILE6}" ]]; then
echo "${FILE6} does not exist."
exit 1
elif [[ ! -e "${FILE7}" ]]; then
echo "${FILE7} does not exist."
exit 1
elif [[ ! -e "${FILE8}" ]]; then
echo "${FILE8} does not exist."
exit 1
elif [[ ! -e "${FILE9}" ]]; then
echo "${FILE9} does not exist."
exit 1
elif [[ ! -e "${FILE10}" ]]; then
echo "${FILE10} does not exist."
exit 1
elif [[ ! -e "${FILE11}" ]]; then
echo "${FILE11} does not exist."
exit 1
fi

Last edited by Pinguino99; 09-30-2020 at 01:06 PM.
 
Old 09-30-2020, 12:18 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,366
Blog Entries: 3

Rep: Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669Reputation: 2669
There are also case statements available. See "man sh" or "man bash" and scroll down to the flow control section in the one or the compound commands section in the other. Or search for the string "esac".
 
Old 09-30-2020, 12:24 PM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,883

Rep: Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250
actually you do not need that elif at all
Code:
function err_exit() {
    echo "Error: $* does not exist" >&2
    exit 1
}
for d in "${DIR1}" "${DIR2}" ...
do
    [[ -d $d ]] || err_exit $d
done
for f in $FILE1 ....
do
    [[ -e $f ]] || err_exit $f
done
 
1 members found this post helpful.
Old 09-30-2020, 12:24 PM   #4
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 571

Rep: Reputation: Disabled
you can also do a for loop. I'm not 100% on the file loop but should be close.

Code:
for dir in DIR{1..5} ; do
  [[ -d "$dir" ]] || echo "${dir} NAO EXISTE."
done
Code:
for file in FILE{1..9} FILE1{0..1} ; do
  [[ -f "$file" ]] || echo "${file} NAO EXISTE."
done
*EDIT* posted same time. slightly different approaches. I like yours better pan.

Last edited by jmgibson1981; 09-30-2020 at 12:28 PM.
 
Old 09-30-2020, 06:46 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,929

Rep: Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501Reputation: 4501
Put all of those file paths in an array and then loop through it.

https://linuxhint.com/bash_loop_list_strings/
 
Old 10-01-2020, 12:40 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,125

Rep: Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508
What is the actual question? Where these file and directory names come in the actual problem?
 
Old 10-01-2020, 07:19 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,568

Rep: Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710
Quote:
Originally Posted by pan64 View Post
actually you do not need that elif at all
Code:
function err_exit() {
    echo "Error: $* does not exist" >&2
    exit 1
}
for d in "${DIR1}" "${DIR2}" ...
do
    [[ -d $d ]] || err_exit $d
done
for f in $FILE1 ....
do
    [[ -e $f ]] || err_exit $f
done
Plus my 2 cents:
Code:
function err_exit() {
    echo "Error: $* does not exist" >&2
    exit 1
}
for d in "${DIR1}" "${DIR2}" ...
do
    [ -d "$d" ] || err_exit "$d" 
done
for f in "$FILE1" ....
do
    [ -f "$f" ] || err_exit "$f"
done
The -f follows a symlink then tests for being a file, so is perfect here.
[[ ]] is okay but not standard (yet).

BTW case, doing a globmatch with a string, cannot be used here!

Last edited by MadeInGermany; 10-01-2020 at 07:21 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script -- if then else elif problems Momof5.5 Linux - Newbie 5 12-05-2013 10:09 AM
Bash shell scripting --> comparing elements in different arrays via if/elif lord_brand Programming 5 06-21-2012 04:49 AM
Python shell help (using elif) danred Linux - General 8 10-20-2009 04:55 AM
Bash script if elif problem assyrian1 Linux - Software 2 12-09-2008 10:00 AM
elif??????????? embsupafly Programming 1 11-27-2002 04:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration