 |
 |
Basic scripting, If, elif, then, case...confused...
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status:
Offline
|
|
Hi,
I'm trying to write a script which asks a series of questions (related to moving files to a folder) and either moving the files to an existing folder or a new folder. I initially tried doing it using if, elif and then but ran iinto problems. So I thought maybe I should be using case, so I tried that (but I don't know if you can embed case commands within another). Anyways, I'm still having a problem... the version of the script using "case" is below... it errors as soon as it hits the second case (i'm assuming because it hasn't read an esac before it gets to it).
<code>
#!/bin/sh
cd ~/test
echo -n "Move files to an archive? (y/n)... "
read move
case $move in
y* | Y* ) echo "An existing archive? (y/n)... " ; break ;;
read exist
case $exist in
y* | Y* ) echo -n "CHOOSE THE ARCHIVE - [1] testfolder... " ; break ;;
read archive_select
case $archive_select in
1 ) mv *.* ~/docs/testfolder/
[nN]* ) echo -n "Enter a new archive name... "
cd ~/docs/
read ARCHIVE_NAME
mkdir '${ARCHIVE_NAME}'
echo "{$ARCHIVE_NAME} created"
cd '${ARCHIVE_NAME}'
mv ~/test/*.* .
echo -n "Files moved to ${ARCHIVE_NAME}"
[nN]* ) echo "skipping" ; break ;;
q* ) exit ;;
* ) echo "unknown response. Asking again" ;;
esac
</code>
In the earlier attempt, using if, and elif, it seemed to error at the elif command.
I'm kind of mucking around trying to get this to work, any help would be appreciated! Thanks a lot
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I wish I could get this to show my tab indents. Otherwise the whole thing is unreadable. But basically your problem is you prematurely ended (with ;;) some of your case statements, then didn't end most of your cases blocks (with esac) at all. I had to guess which possibilities went with each case block.
In other words your syntax was wrong. Go back and take another look at how to use cases.
Code:
#!/bin/sh
cd ~/test
echo -n "Move files to an archive? (y/n)... "
read move
case $move in
y* | Y* ) echo -n "An existing archive? (y/n)... "
read exist
case $exist in
y* | Y* ) echo -n "CHOOSE THE ARCHIVE - [1] testfolder... "
read archive_select
case $archive_select in
1 ) mv *.* ~/docs/testfolder/ ;;
esac;;
[nN]* ) echo -n "Enter a new archive name... "
cd ~/docs/
read ARCHIVE_NAME
mkdir '${ARCHIVE_NAME}'
echo "{$ARCHIVE_NAME} created"
cd '${ARCHIVE_NAME}'
mv ~/test/*.* .
echo -n "Files moved to ${ARCHIVE_NAME}" ;;
esac;;
[nN]* ) echo "skipping"
break;;
q* ) exit;;
* ) echo "unknown response. Asking again";;
esac
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Man, I hate shell scripting. That's why God gave us proper scripting languages.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Right, because only shell scripts can have syntax errors 
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
I meant because they're uglier than Richard Gere in drag, not because shell scripts can have syntax errors.
Although I guess in fairness, some languages that aren't part of a shell look awful too.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|