Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Basic scripting, If, elif, then, case...confused...

Basic scripting, If, elif, then, case...confused...
Thread Tools
Mac Enthusiast
Join Date: Oct 2000
Location: Toronto
Status: Offline
Reply With Quote
Jun 6, 2006, 09:48 PM
 
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
Reply With Quote
Jun 7, 2006, 09:53 AM
 
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
Reply With Quote
Jun 7, 2006, 03:12 PM
 
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
Reply With Quote
Jun 8, 2006, 06:31 PM
 
Right, because only shell scripts can have syntax errors
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 9, 2006, 01:54 AM
 
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'."
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:05 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2