Bourne Shell Scripting CRISIS

:frowning: :frowning: :frowning:

G'day all

This is a desperate call to those out there who know anything about Bourne Shell scripting, as I'm in the middle of a crisis

Pleae E-Mail me if you believe you can help \(No spamming please\)

The sooner the better, if you're an aussie that lives in Victoria, I'll even try to get you a six-pack of VB.
(That's an incentive alright! Hahahahaha)

Thankyou all very much

Aussie_Bloke

:frowning: :frowning: :frowning:

Pls read the forum rules

(10) Don't post your email address and ask for an email reply. The forums are for the benefit of all, so all Q&A should take place in the forums.

Anyway what is the question about bourne shell ? you can try post it for us to help, not asking for email reply's.

G'day

The problem that I've got is that I've written a Bourne Shell script, complete with case statements, and it seems that UNIX believes that there's a part missing.

The error that I receive (Ungratefully) is:
syntax error: ;; unexpected
Or at least something to the tune of this, as I know that it's dealing with the case statement, the only question is where I may be wrong, any suggestions anyone?

The only reason that I'm putting this up here is because the ;; that is being referred to is supposedly at the end of the file, where no such symbol / expression exists?

I'm beginning to think that the system is hallucinating.....

Any suggestions as to what the problem might be?
Is the system really on LSD? or is it just my s***ty programming?

Thanks all

Aussie_Bloke

Are your case statements correct?
They follow the format:

case $something_here in
whatever_1) something_else_here ;;
whatever_2) something_else_else_here ;;
esac

Also, if you have unmatched " or ' marks anywhere in there, it could be messing you up there somewhere...

Can you post parts of the actual case statement and surrounding code?

[On a side note: I noticed that this is very likely a homework assignment based on your email address... I guess it's not as bad as asking someone to write the script for you, but generally it's not cool to post homework questions. I'm willing to help troubleshoot as long as you write the code yourself, though]

G'day LivinFree

The case statement that is out of sorts is actually within another case statement, would that possible have any effect on the outcome, given that there's 4 items to choose from in both?

Another possibility could be that I'm using windows notepad to create the file, porting it to UNIX via FTP to a local server, could that create problems?

I've also noticed that even though I'm sending the same file the file sizes are different, under dos and UNIX. The difference varies, but it's not more than 100 bytes, should that be cause for concern?

As requested, here's an edited selection of the file:

        echo -----------
	echo -Logs-Menu-
	echo -----------
	echo
	echo "1. Search Logs for a file"
	echo
	echo "2. View all sorted by date / time"
	echo
	echo "3. View by type (Backup or Restore)"
	echo
	echo "4. Quit"
	echo
	echo Please make your selection
	read selection
	case $selection in
	1) #Search logs for something
	echo Please enter a file name to search for
	read searchname
	cd logs
	grep $searchname *.*
	grep $searchname >> log_file2
	;;
	2) #View all sorted by date / time
	echo Displaying all log files
	more *.*
	;;
	3) #View by type
	echo Please enter what type you want
	echo "(B for backup or R for restore)"
	read choice
	if [ $choice = "B" ]
	then
	cd logs
	grep B *.*
		else
		grep R *.*
	;;
	4) #Quit
	done
	quit
	exit
	;;

esac
4) #Quit
exit
;;
esac

The text at the bottom (The 4) #Quit portion) is the last part of the first case statement.

Note that I'm now also getting another message, claiming that the indicated line (** Code **) has an unexpected `)'
Just wondering if it would be possible to explain that one too...

Thanks

Aussie_Bloke

[I added code tags for readability....Perderabo]

The "if" statement that you posted is wrong...you need a "fi to end it.

if [ $choice = "B" ]
then
    cd logs
    grep B *.* 
else
    grep R *.*
fi

Sorry mate

Should've told yas sooner, I've already found that one

Thanks anyways

Still doesn't work though, claims that there's an unexpected `)'
(Indicated with ** Code **)
Somewhere in there

then
cd logs

** grep B *.*
else
grep R *.*
;;
4) #Quit **
done
quit
exit
;;

esac
4) #Quit
exit
;;
esac

Thanks for your help, as it's both needed and greatly apprecitaed

Aussie_Bloke

Looking at your last post and focusing on the section between the first ** and the last *, I continue to see the "else" clause of an "if" statement that is missing a terminating "fi". This has the effect of placing the line:
4) #Quit *

inside the "else" clause of the "if" statement where it is unexpected.

It's great that you found this problem. But now you need to actually insert the "fi" in your code.

Rightio fellas (No offense)

Now that the mongrel code seems to have been conquered (At least for the moment)

But now it seems that the ending of the file has given Unix the chance to have another shot at me and my bad programming....

It now claims that, again within the section of code provided, it's found an unexpected End of file

How about I include an edited version of the complete file, so that you smart people (I know I'm sucking-up here, but I've got goodd reason too, it's not everyday that you get to contact the UNIX gods.....) can perhaps find another spot that could be stuffing the whole thing up?
Although for the moment, to spare those who have slower connections, I'll just put in the code required

Included code:

	grep R \*.*
fi
;;
4\) \#Quit
exit
;;

esac
fi
exit

You'll notice that there's a few parts missing, including the ending of the first case statement, I've chosen to do this for error checking, and have found that there is a problem with it, which really bugs me (No pun intended)
It's included below:

4) #Quit
exit
;;
*) #Bad choice
echo "Invalid menu selection, please enter a valid choice"
;;

esac

Cheers again all

I can't see the end-of-file problem from what you have posted. But I'm pretty sure that you have another incomplete compound command somewhere. Compound commands are "if", "case", etc. One way to find these is pull out code until it's obvious. For example, remove an inner "case" statement and see if the problem disappears.

Tell yas what, I'll just post the whole file here, 'cept that I'll remove a few comments (The language leaves something to be desired)

#!/bin/sh
#Menu bit
if [ $# -ne 2 ]
then
echo Invalid number of argumeents
echo
echo "Usage: $0 Arguement 1 Arguement 2 - No spaces please"
else
$1=log_file
$2=log_file2
echo ----------------
echo ---Main--Menu---
echo ----------------
echo
echo "1. Backup files"
echo
echo "2. Restore Backed-up files"
echo
echo "3. View Logs"
echo
echo "4. Quit"
echo
echo Please enter your selection
read selection
case $selection in
1) if [ ! -d backup ]
then
mkdir backup
else
#Beaut, now that we've either created the backup directory, or verified
#that it exists, let's ask the user kindly which file they'd like
#backed-up
fname=cont
function=B
while [ "$fname" != "quit" ]
do
echo Please Enter a the name of a file to backup
read fname
if [ -f $fname ]
then
echo Backing up $fname, please wait
echo ......
#Marker Line 50
cp $fname backup/$fname.bak
echo Successfully backed-up file $fname
echo `date`:$dname/$fname:$function >> $log_file
#Rightio, now that it's backed-up, let's ask them to either enter another
#file to backup, or to go back to the menu
fi
done
fi
;;
2)
#Ok, now we've got to restore the file, basically the same as the backup
#in theory, just a little different.....
if [ ! -d backup ]
then
echo The backup directory is either missing, or has been deleted, you therefore
echo cannot use this function
else
#Beaut, now that we've either created the backup directory, or verified
#that it exists, let's ask the user kindly which file they'd like
#backed-up
fname=cont
function=R
while [ "$fname" != "quit" ]
do
#Marker Line 75
echo Please Enter a the name of a file to restore
read fname
if [ -f $fname ]
then
echo
echo Please enter a directory to restore $fname to
read dname
if [ -d $dname ]
then
if [ -r $dname/$fname ]
then
echo Restoring $fname, please wait
echo "......"
else
echo "File already exists in $dname, Do you want it replaced?"
echo yes or no
read choice
if [ "$choice" = "yes" ]
then
echo Now restoring $fname
cp backup/$fname $dname
echo `date`:$dname/$fname:$function >> $log_file
echo Successfully restored $fname
else
echo You've chosen not to over-write the older file
echo
echo "Quitting due to unforseen circumstance......."
exit
#Rightio, now that it's restored, let's ask them to either enter another
#file to restore, or to go back to the menu
fi
fi
fi
fi
done
fi
;;
#Marker Line 100
3) #Damn thing doesn't want to check if a directory exists, so bugger it, I'll just create it
#I'm really starting to dislike UNIX.....
#mkdir logs
echo -----------
echo -Logs-Menu-
echo -----------
echo
echo "1. Search Logs for a file"
echo
echo "2. View all sorted by date / time"
echo
echo "3. View by type (Backup or Restore)"
echo
echo "4. Quit"
echo
echo Please make your selection
read selection
case $selection in
1) #Search logs for something
echo Please enter a file name to search for
read searchname
cd logs
grep $searchname *.*
grep $searchname >> log_file2
;;
2) #View all sorted by date / time
echo Displaying all log files
more *.*
;;
3) #View by type
echo Please enter what type you want
echo "(B for backup or R for restore)"
read choice
if [ $choice = "B" ]
then
cd logs
grep B *.*
else
grep R *.*
fi
;;
4) #Quit
exit
;;
esac
fi
exit

This might help yas, if anything it'll show you all how NOT to program in Unix, hahahaha.

Thanks

Aussie_Bloke

Tell yas what, I'll just post the whole file here, 'cept that I'll remove a few comments (The language leaves something to be desired)

#!/bin/sh
#Menu bit
if [ $# -ne 2 ]
then
echo Invalid number of argumeents
echo
echo "Usage: $0 Arguement 1 Arguement 2 - No spaces please"
else
$1=log_file
$2=log_file2
echo ----------------
echo ---Main--Menu---
echo ----------------
echo
echo "1. Backup files"
echo
echo "2. Restore Backed-up files"
echo
echo "3. View Logs"
echo
echo "4. Quit"
echo
echo Please enter your selection
read selection
case $selection in
1) if [ ! -d backup ]
then
mkdir backup
else
#Beaut, now that we've either created the backup directory, or verified
#that it exists, let's ask the user kindly which file they'd like
#backed-up
fname=cont
function=B
while [ "$fname" != "quit" ]
do
echo Please Enter a the name of a file to backup
read fname
if [ -f $fname ]
then
echo Backing up $fname, please wait
echo ......
#Marker Line 50
cp $fname backup/$fname.bak
echo Successfully backed-up file $fname
echo `date`:$dname/$fname:$function >> $log_file
#Rightio, now that it's backed-up, let's ask them to either enter another
#file to backup, or to go back to the menu
fi
done
fi
;;
2)
#Ok, now we've got to restore the file, basically the same as the backup
#in theory, just a little different.....
if [ ! -d backup ]
then
echo The backup directory is either missing, or has been deleted, you therefore
echo cannot use this function
else
#Beaut, now that we've either created the backup directory, or verified
#that it exists, let's ask the user kindly which file they'd like
#backed-up
fname=cont
function=R
while [ "$fname" != "quit" ]
do
#Marker Line 75
echo Please Enter a the name of a file to restore
read fname
if [ -f $fname ]
then
echo
echo Please enter a directory to restore $fname to
read dname
if [ -d $dname ]
then
if [ -r $dname/$fname ]
then
echo Restoring $fname, please wait
echo "......"
else
echo "File already exists in $dname, Do you want it replaced?"
echo yes or no
read choice
if [ "$choice" = "yes" ]
then
echo Now restoring $fname
cp backup/$fname $dname
echo `date`:$dname/$fname:$function >> $log_file
echo Successfully restored $fname
else
echo You've chosen not to over-write the older file
echo
echo "Quitting due to unforseen circumstance......."
exit
#Rightio, now that it's restored, let's ask them to either enter another
#file to restore, or to go back to the menu
fi
fi
fi
fi
done
fi
;;
#Marker Line 100
3) #Damn thing doesn't want to check if a directory exists, so bugger it, I'll just create it
#I'm really starting to dislike UNIX.....
#mkdir logs
echo -----------
echo -Logs-Menu-
echo -----------
echo
echo "1. Search Logs for a file"
echo
echo "2. View all sorted by date / time"
echo
echo "3. View by type (Backup or Restore)"
echo
echo "4. Quit"
echo
echo Please make your selection
read selection
case $selection in
1) #Search logs for something
echo Please enter a file name to search for
read searchname
cd logs
grep $searchname *.*
grep $searchname >> log_file2
;;
2) #View all sorted by date / time
echo Displaying all log files
more *.*
;;
3) #View by type
echo Please enter what type you want
echo "(B for backup or R for restore)"
read choice
if [ $choice = "B" ]
then
cd logs
grep B *.*
else
grep R *.*
fi
;;
4) #Quit
exit
;;
esac
fi
exit

This might help yas, if anything it'll show you all how NOT to program in Unix, hahahaha.

The file was properly formatted (IE: Tabbed in) but I'm unable to fix this, as I'm too tired at the mo'
Too many early mornings (2-3:30am) are taking their toll

Thanks

Aussie_Bloke

One problem - two case statements but only one esac in the script.

Another thing - you set a variable (selection) on both case statements - this may confuse it - change one to something else.

Alright, have done that, but I'm still getting unexpected end of file messages

You are also missing one set of ;; at the end of 3) in the second case statement.

Ok, added second ;; in 3) of second case statement, now claims that it's unexpected

Filename: syntax error at line 157: ';;' unexpected

Is the UNIX server that I'm using on drugs? Or is it just me?

Thanks

Maybe the code I grabbed off here didn't have it. Just make sure for each case option, you have the same amount of ending ;;. You have 8 total option (4 options in each case) so you should have 8 ;; closing each option. Just as you had the two case statements but didn't have the closing esac statements (you fixed that).

Also, are you attempting to use 4) Quit in both case statements? That would be illegal/wrong/unadvisable.

I pulled out all the extra and just checked your code this way:

#!/bin/sh
echo "A case example"
echo ----------------
echo ---Main--Menu---
echo ----------------
echo
echo "1. Backup files"
echo
echo "2. Restore Backed-up files"
echo
echo "3. View Logs"
echo
echo "4. Quit"
echo
echo Please enter your selection
read selection
case $selection in
1)
echo "backup of file"
;;
2)
echo "Restore of files"
;;
3)
echo "Menu choices again"
echo "1. Search logs"
echo "2. View by date"
echo "3. View by type"
echo "4. Quit"
echo "Please enter your selection"
read sel1
case $sel1 in
1)
echo "Searching logs"
;;
2)
echo "Viewing logs by date"
;;
3)
echo "Viewing logs by type"
;;
4)
echo "quitting"
;;
esac
;;
4)
echo "Quitting"
;;
esac
echo "done"
exit

Ummm....single quotes are special to the shell. You need to quote that quote with a backslash.

G'day again

Can you see anything wrong with this? As it's still caliming that the indicated area is unexpected.

    cd logs
    grep B \*.*
            else
            grep R \*.*
    fi
    ;;

** 4) #Leave the program **
exit
;;
esac
exit
4) #Drop the user back to the prompt
exit
;;
fi
exit

See if you can make any sense of it please

Thanks again
(Would've written sooner, 'cept that I needed to sleep)

Aussie_Bloke

Have you tried putting the line:
set -x
at the top? It will show you what the script's doing as it runs...

I'm going to eat now, but if I get a chance later, I'll run the script, and see what I get...