Case Statement in a While Loop, Shell Scripting

Case Statement in a while loop, shell scripting

Of course:

while true; do
read x
case $x of
exit) break ;;
1) echo January ;;
2) echo February ;;
# ...
12) echo December ;;
*) echo "Unknown response, enter a number 1-12 or type 'exit' to quit" ;;
esac
done

bash: while loop going to infinite loop while case statement

Your outer function has redirected standard input to read from the cut process substitution, so that's where read is reading its input from.

Perhaps use a separate file descriptor for the process substitution.

Furthermore, your verify function recursively calls check_status again; probably take that out!

verify()
{
while true ;do
read -p "Have you fixed? Yes/No: " yn
case $yn in
YES|Yes|yes|y|Y)
echo "Hola"
# check_status # DON'T!
break
;;
NO|No|no|n|N)
echo "Please fix"
;;
*)
echo "Please answer yes or no."
;;
esac
done
}

check_status()
{
# Notice read -u 3 to read from fd3
# Notice _ to read fields 3 and up
while IFS=" " read -u 3 -r rec1 rec2 _
do
# Syntax: single =, add quoting
if [ "$rec2" = 'up' ]
then
echo "$rec1 is up"
else
echo "$rec1 is down so please fix"
verify
fi
# Notice 3<
done 3< compute_list
}

check_status

I also took the liberty to fix your indentation and avoid the unnecessary process substitution; read can perfectly well read and discard the fields after the second.

printf is more versatile than echo but in this case, when you simply want to output static strings, I switched to echo.

Demo: https://ideone.com/pVerFm
(I left in the process substitution there in case you want to see what it looks like syntactically.)

Nesting a case statement within a for loop

As I read it, this is what 2.1 and 2.2 are asking for:

for folder in rent utilities groceries other; do 
mkdir "$1/$folder"

case $folder in
rent)
...
;;
utilities)
...
;;
groceries)
...
;;
other)
...
;;
esac
done

I've left the cases blank for you to fill out.

For what it's worth, I would never code a script this way. Having a case statement inside a for loop is an anti-pattern. The loop really adds no value. If this weren't an assignment I would code it differently:

mkdir "$1"

# Populate rent/ directory.
mkdir "$1"/rent
touch "$1"/rent/...

# Populate utilities/ directory.
mkdir "$1"/utilities
touch "$1"/utilities/...

# Populate groceries/ directory.
mkdir "$1"/groceries
touch "$1"/groceries/...

# Populate other/ directory.
mkdir "$1"/other
touch "$1"/other/...

Bash - case statement always enters default

Apparently, the argument (here: "valueX") read from the file is correct

Since you consider the quotes being part of the argument to be correct, in order to match them with a case pattern, you have to escape the pattern quotes:

  case ${arg} in
\"valueX\")

Interestingly, this appears to be a Bash (documentation) error, as that says only:

Each pattern undergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

It doesn't tell that the pattern undergoes quote removal.



Related Topics



Leave a reply



Submit