shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell script
# 1  
Old 04-25-2008
shell script

i do not know why this piece of code is not working
please help

#!/bin/sh
choice=0
if [ $# -eq 1 -o $# -eq 2 ]; then
while [ $choice -ne 4 ]; do
clear
echo "" > temp
echo " MENU "
echo "1. display information"
echo "2. perform arimethic operation"
echo "3. find the first alphabetic letter from the input"
echo "4. exit the program"
echo "5. please enter your choice(1-4 only)"
read choice
if [ $choice -eq 1 ]; then #this part is my main problem
if [ -d $1 ]; then # i want to display calender and the current
cal -3 # working directory in the monitor only if the user
cd $1 # doesnot input the second argument $2
pwd #so i use if statement but it doesnot work
ls -l | sort -r +8 -9
elif [ -d $1 -a -n $2 ]; then
cal -3 >> $2
pwd `cd $1` >> $2
(ls -l | sort -r +8 -9) >> $2
fi
elif [ $choice -ne 1 ]; then
case $choice in
"2") echo "please enter 2 numbers and a choice of operation(A,S,M,D)"
read inp1 inp2 cho
(echo inp1 > inp1.f; echo inp2 > inp2.f)
if [ "$a" = "" -o "$b" = "" ]; then
case $cho in
"A") result=`expr $inp1 + $inp2`
echo $result;;
"S") result=`expr $inp1 - $inp2`
echo $result;;
"M") result=`expr $inp1 \* $inp2`
echo $result;;
"D") result=`expr $inp1 / $inp2`
echo $result;;
*) echo please enter the right choice;;
esac
else
echo invalid number
fi ;;
*) echo "invalid"
esac
fi

if [ $choice -ne 4 ]; then
echo please press enter to continue
read a
fi

done
fi
# 2  
Old 04-25-2008
It works for me - or at least I think it does. Can you be more specific on what you are having issues with? You have a few comments in the code, and it appears that you can issue the following:

Code:
./yourscript.sh /root

That will execute your script and do the calendar display and directory information as you want. You cannot call the script like so:

Code:
sh yourscript.sh /root

It won't work.

Also, in order to do the above example (the first one), you will need to make your script executable, like so:

Code:
chown 700 ./yourscript.sh

Again, if you can be more specific on your issues and possibly give us some error messages (if any) so that we can help you further.

Oh, one quick thing, you can debug the script by placed a -x after the shell path like so (in your script):

Code:
#!/bin/sh -x

# 3  
Old 04-25-2008
shellscript

i have change the code, now it is executable and i think this is the right code . what i am doing is actually to execute for option in my menu list, user can choose to enter one or two argument but not more , the problem is the first argument is mandatory and it must be a directory while the second argument is optional and it should be a file. if user enter only first argument then the first option should be display information such as user name but if user also provide second argument that the output of information in option 1 should be directed to that particular file

this is my new code

#!/bin/sh -x
choice=0
if [ $# -eq 1 -o $# -eq 2 ]; then
if [ -d $1 ];then
while [ $choice -ne 4 ]; do
clear
echo "" > temp
echo " MENU "
echo "1. display information"
echo "2. perform arimethic operation"
echo "3. find the first alphabetic letter from the input"
echo "4. exit the program"
echo "5. please enter your choice(1-4 only)"
read choice
if [ $choice -eq 1 ]; then
cd $1
if [ $# -eq 2 ]; then
if [ -f $2 ]; then
(cal -3; pwd; (ls -l | sort -r +8 -9)) > $2
fi
else
cal -3
pwd
ls -l | sort -r +8 -9
fi
elif [ $choice -ne 1 ]; then


this is not the complete code, my main issue now is the output redirection doesnot work the file specify by $2 is empty eventhough i have executed the code. i do not know what's wrong
# 4  
Old 04-25-2008
Alright, here's some working code:

Code:
#!/bin/sh
choice=0
if [ $# -eq 1 -o $# -eq 2 ]; then
        if [ -d $1 ];then
                while [ $choice -ne 4 ]; do
                clear
                echo "" > temp
                echo " MENU "
                echo "1. display information"
                echo "2. perform arimethic operation"
                echo "3. find the first alphabetic letter from the input"
                echo "4. exit the program"
                echo "5. please enter your choice(1-4 only)"
                read choice
                        if [ $choice -eq 1 ]; then
                                cd $1
                                if [ $# -eq 2 ]; then
                                        if [ -f $2 ]; then
                                                echo "File exists. Redirecting output. (sleeping for 2 seconds to let you know)"
                                                sleep 2
                                                (cal -3; pwd; (ls -l | sort -r +8 -9)) > $2
                                        else
                                                echo "File $2 does not exist.  Creating file now. (sleeping for 2 seconds to let you know)"
                                                sleep 2
                                                touch $2
                                                (cal -3; pwd; (ls -l | sort -r +8 -9)) > $2
                                        fi
                                else
                                        (cal -3; pwd; (ls -l | sort -r +8 -9))
                                        echo "Sleeping for 10 seconds.  Once 10 seconds go by, the script will run back through your while loop and then display your menu again."
                                        sleep 10
                                fi
                        fi
                done
        fi
fi

I've put a few sleep functions in so that you can see the output. Of course, you can change this up as you want to - I only did it so that you could see what was going on. I fixed the redirection and if the actual file you specify doesn't exist, it will create the file on the fly. I don't know if that was exactly what you wanted, but I did my best in trying.

Please let me know if you have more issues or need something looked at further, or if I just didn't actually fix what was broken.

Thanks,
Drew
# 5  
Old 04-25-2008
shellscript

thanks for code
i now know what my issue is. i try to see the contect of $2 using cat command and i found out that there is no such file in the directory but when i change my directory to the one that i specify in $1 the file is actually inside it, i do not know why it create inside that directory but not on my current working directory, do not why it is so?

thanks a lot for help
# 6  
Old 04-25-2008
Please supple the actual running of the script (including arguments). For example:

Code:
./yourscript /root test.txt

That would mean that /root was $1 and test.txt is $2.

I also need to know what gets created and where so tat I know specific probems.

Thanks,
Drew

Last edited by drewrockshard; 04-25-2008 at 08:19 AM.. Reason: code tag wrong
# 7  
Old 04-25-2008
shellscript

[CODE][
#!/bin/sh
choice=0
if [ $# -eq 1 -o $# -eq 2 ]; then
if [ -d $1 ];then
while [ $choice -ne 4 ]; do
clear
echo "" > temp
echo " MENU "
echo "1. display information"
echo "2. perform arimethic operation"
echo "3. find the first alphabetic letter from the input"
echo "4. exit the program"
echo "5. please enter your choice(1-4 only)"
read choice
if [ $choice -eq 1 ]; then
cd $1
if [ $# -eq 2 ]; then
if [ -f $2 ]; then
(cal -3; pwd; (ls -l | sort -r +8 -9)) > $2
else
(cal -3; pwd; (ls -l) | sort -r +8 -9) > $2
fi
else
cal -3
pwd
ls -l | sort -r +8 -9
fi
elif [ $choice -ne 1 ]; then
case $choice in
"2") echo "please enter 2 numbers and a choice of operation(A,S,M,D)"
read inp1 inp2 cho
(echo inp1 > inp1.f; echo inp2 > inp2.f)
if inpr1=`grep -w [0-9] inp1.f` -o inpr2 =`grep -w [0-9] inp2.f` ; then
case $cho in
"A") result=`expr $inp1 + $inp2`
echo $result;;
"S") result=`expr $inp1 - $inp2`
echo $result;;
"M") result=`expr $inp1 \* $inp2`
echo $result;;
"D") result=`expr $inp1 / $inp2`
echo $result;;
*) echo please enter the right choice;;
esac
else
echo invalid number
fi ;;
"3") echo "please enter an alphabet"
input3=1
echo "" > temp
while [ "$input3" != "0" ]; do
read input3
echo $input3 > check
if [ "$input3" != "0" ]; then
if check1=`grep -w "[a-z]" check` ; then
echo $input3 >> temp
else
echo input should be lower case alphabet
fi
fi
done

grep -w "[a-z]" temp > temp1
echo the first letter in alphabetic order is `sort temp1 | head -1`;;
"4") echo "do you really want to exit(y/n)?"
read input
if [ "$input" = "y" -o "$input" = "Y" ]; then
echo end of file,thanks for using the menu
elif [ "$input" = "n" -o "$input" = "N" ]; then
choice=1
else
echo invalid choice
choice=1
fi;;
*) echo "you have enter wrong input";;
esac
fi

if [ $choice -ne 4 ]; then
echo please press enter to continue
read a
fi

cd ../
done

fi
fi]

the argument is ./menu work input
work is in my current directory and the directory exists
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

5. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

6. Shell Programming and Scripting

Correct shell script to Call One shell script from another shell script

Hi All, I have new for shell scripting. Problem : I have one scrip at serv1 and path of server is /apps/dev/provimage/scripts and script name:extract_ancillary.bat. I need to call this script at server2(my working server) and execute at server2 . Please let me know how to build the... (5 Replies)
Discussion started by: Vineeta Nigam
5 Replies

7. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

8. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

9. Shell Programming and Scripting

invoking a shell script inside cgi shell script

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

How to Run a shell script from Perl script in Parent shell?

Hi Perl/UNIX experts, I have a problem in running a shell script from my perl script (auto.pl). I run the perl script using perl auto.pl from the shell prompt The shell script picks the files in "input" folder and procesess it. The shell script blue.sh has this code. export... (16 Replies)
Discussion started by: hifake
16 Replies
Login or Register to Ask a Question