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
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:
That will execute your script and do the calendar display and directory information as you want. You cannot call the script like so:
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:
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):
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
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 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?
[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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)