Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
11-01-2003, 08:49 PM
|
#1
|
Member
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91
Rep:
|
Shell Scripting Help
I really hope someone can help me here with this
I have just started this at college & know the commands but am still trying to get my head around the way you insert them into a script.
What this script needs to do is list the following options like so
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
It shows these options & then depending on what number you choose it shows the output.
As you can see i echo the options to the screen but besides knowing that i am a bit lost
option 1 is cat .bash_profile |grep bin
option 2 is ls -la *.txt
option 3 is du -s -h
i dont know how to link a number to a specific command
Can anyone give some pointers to get me started
Thanks alot
Andrew
|
|
|
11-01-2003, 11:01 PM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
#!/bin/bash
# this sets inital stuff.
vvar=0
while test ${vvar} != 4; do
clear
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
echo ""
read vvar
if [ ${vvar} -eq 1 ]; then
echo $PATH
echo "press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} -eq 2 ]; then
ls ~/*.txt
echo "press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} -eq 3 ]; then
df -h ~
echo "press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} -eq 4 ]; then
exit 0
fi
done
|
|
|
11-01-2003, 11:01 PM
|
#3
|
Member
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327
Rep:
|
do u know how to take input from the user?
if you do then this is how you check equality:
Checks equality between numbers:
x -eq y Check is x is equals to y
x -ne y Check if x is not equals to y
x -gt y Check ifx is greater than y
x -lt y Check if x is less than y
i got this from http://www.justlinux.com/nhf/Program...Scripting.html
whenever you have such questions you should always google them b4 leaving threads....you would have surely found your answer this way.
hope this helped
|
|
|
11-01-2003, 11:02 PM
|
#4
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
oops, used ym own ways, you should know what to change to get your meathos though.
|
|
|
11-01-2003, 11:13 PM
|
#5
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
read [variable]
takes user input in form of number or string, typed, but del and backspace don't work he normal way
|
|
|
11-01-2003, 11:30 PM
|
#6
|
Member
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91
Original Poster
Rep:
|
That is great, The if [ ${vvar} -eq 2 ]; then
part really helped & i wa able to change the commands as needed. Once i get the hang of this scripting i think i am really going to enjoy it.
One more thing just as a by product I noticed that in this script if you dont hit a key combination that the script is expecting ( say "enter" instead of a number it will bring up the following
./info_script: [: -eq: unary operator expected
./info_script: [: -eq: unary operator expected
./info_script: [: -eq: unary operator expected
./info_script: [: -eq: unary operator expected
./info_script: test: !=: unary operator expected
What is this mean when it shows these errros? & could it loop back to the list or even better can it tell you that you hit the wrong key.
|
|
|
11-01-2003, 11:35 PM
|
#7
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
it could be done, using ifs and tests, test if it is 0-5 let it go otherwise display error but reloop, there are ways, can't write it at moment for you though.. sorry
or try changing it to "==" rather than "-eq" and it will do string comparison thus anything would do it.. that would not fix the while, unless just before "done" you do "vvar=1"
|
|
|
11-01-2003, 11:36 PM
|
#8
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
if you want a good code example go to my project page at: http://www.sourceforge.net/projects/exmmpkg
it is a script set i am making, and if you like it feel free to contribute code as you learn shell scripting, I could use a hand (or several)
|
|
|
11-02-2003, 09:34 AM
|
#9
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
You should use select. I'm on a windows machine right now so I can't help with an example but I think it will do exactly what you want as far as selecting the input.
|
|
|
11-03-2003, 10:42 PM
|
#10
|
Member
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91
Original Poster
Rep:
|
I have been having trouble adding an option to the booton of my script like so
"Please Choose from the following list of options:"
1. Show the path enviroment varible
2. List all text files under your home directory
3. Show the disk usage for your user home directory
4. Exit
Selection _ ( with a flashing cursor )
I need to have it that if wrong selection is made ( say 5 ) it will print an error & return after two seconds to the selection screen again
Any help/advice is appreciated
Regards
Andrew
|
|
|
11-03-2003, 11:10 PM
|
#11
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
Code:
#!/bin/bash
select program in 'Show the path enviroment varible' \
'List all text files under your home directory' \
'Show the disk usage for your user home directory' \
'Exit'
do
case $REPLY in
1)
#Show the path enviroment varible
echo -e ${PATH//:/\\n}
;;
2)
#List all text files under your home directory
file `find ~/ -type f -maxdepth 1`|grep ASCII
;;
3)
#Show the disk usage for your user home directory
du ~/ -s
;;
4)
break
;;
*)
echo "Please select another option from the list"
sleep 2
;;
esac
done
Last edited by /bin/bash; 11-03-2003 at 11:16 PM.
|
|
|
11-04-2003, 06:23 PM
|
#12
|
Member
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91
Original Poster
Rep:
|
One last question ( I promise )
Currently list is shown like this
1. Show the PATH environment variable
2. List all text files under your user home directory
3. Show the disk usage for your user home directory
4. Exit
Selection:
_ (cursor is here)
how can i make it so it is here
1. Show the PATH environment variable
2. List all text files under your user home directory
3. Show the disk usage for your user home directory
4. Exit
Selection: _
I know this is just cosmetic but am interested to know all the same
Regards
Jester
|
|
|
11-04-2003, 06:39 PM
|
#13
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
put these lines in between the echo "4. Exit" and read vvar
echo ""
echo ""
ex:
echo "4. Exit"
echo ""
echo ""
read vvar
|
|
|
11-04-2003, 06:49 PM
|
#14
|
Member
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91
Original Poster
Rep:
|
This is my current code. I cant make sense of code above that puts the prompt after the word "Selection:"
#!/bin/bash
# this sets the inital stuff
vvar=0
while test ${vvar} != 4; do
clear
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
echo "Selection:"
read vvar
|
|
|
11-04-2003, 09:11 PM
|
#15
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
Code:
#!/bin/bash
# this sets the inital stuff
vvar=0
while test ${vvar} != 4; do
clear
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
echo "Selection:"
read vvar
Change the above to
Code:
#!/bin/bash
clear
PS3="Selection: "
select program in 'Show the path enviroment varible' \
'List all text files under your home directory' \
'Show the disk usage for your user home directory' \
'Exit'
do
Now all you have to do is change all the $vvar to $REPLY
|
|
|
All times are GMT -5. The time now is 12:14 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|