You are not logged in.
I've got a case statement writen down and I thought I knew enough about bash . For every reason I think it should be breaking but it's not, instead it's returning to the top of the loop. When I type in the terminal, I get:
# script-name d file
Create a new directory? (y/n): n
Select PSP VIDEO sub-directory:
1) /run/media/todd/PSP/VIDEO/Explorer/
2) /run/media/todd/PSP/VIDEO/NGC-Specials/
3) /run/media/todd/PSP/VIDEO/Other/
#? 2
Create a new directory? (y/n):
Here's the loop:
if [ "$1" == "d" ]; then
while true; do
read -p " Create a new directory? (y/n): " yn
case $yn in
[Yy] ) read -p " Directory name (no spaces): " newdir
vid_dir="$vid_dir"/"$newdir"
mkdir "$vid_dir" && break ;;
[Nn] ) printf " Select PSP VIDEO sub-directory:\n"
select vid_sub in "$vid_dir"/*/
do
vid_dir="$vid_sub"
test -n "$vid_dir" && break
echo " Select 1, 2, ..."
done ;;
* ) echo " Answer (y)es or (n)o."
esac
done
shift
fi
Any thoughts?
Last edited by Gen2ly (2012-07-10 09:23:29)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
My Bash is not the greatest, but don't the break's break the case statement rather than the while statement?
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
ewaller's spot on. 'break' and 'continue' only refer to the immediate loop in the execution context. You can break out of multiple loops by passing a number as an argument, i.e. 'break 2' will get you out of both the while and the case statement.
Offline
close, in bash break does not work for case statements (try it, it errors). it's for while, until, for and select loops. in Gen2ly's code above entering 'y' should produce the correct behaviour and the incorrect behaviour only when entered 'n' as there is an extra select block there. the solution is as what falconindy gave, break n, where n is the number of loops to break out of.
man bash:
break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n lev‐
els. n must be ≥ 1. If n is greater than the number of enclosing loops, all enclosing
loops are exited. The return value is 0 unless n is not greater than or equal to 1.
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
Ah, my presumption was that break would return to the head of the script (i.e. without any loops), now I know better. Actually learning this... this is a really nicely thought out. Couldn't ask for a better explanation from better people, thanks guys.
Last edited by Gen2ly (2012-07-10 09:24:14)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline