LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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



Reply
 
Search this Thread
Old 11-01-2003, 09:49 PM   #1
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Rep: Reputation: 15
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
 
Old 11-02-2003, 12:01 AM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
#!/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
 
Old 11-02-2003, 12:01 AM   #3
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Rep: Reputation: 30
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
 
Old 11-02-2003, 12:02 AM   #4
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
oops, used ym own ways, you should know what to change to get your meathos though.
 
Old 11-02-2003, 12:13 AM   #5
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
read [variable]
takes user input in form of number or string, typed, but del and backspace don't work he normal way
 
Old 11-02-2003, 12:30 AM   #6
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
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.
 
Old 11-02-2003, 12:35 AM   #7
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
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"
 
Old 11-02-2003, 12:36 AM   #8
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
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)
 
Old 11-02-2003, 10:34 AM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 11-03-2003, 11:42 PM   #10
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
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
 
Old 11-04-2003, 12:10 AM   #11
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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-04-2003 at 12:16 AM.
 
Old 11-04-2003, 07:23 PM   #12
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
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
 
Old 11-04-2003, 07:39 PM   #13
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
put these lines in between the echo "4. Exit" and read vvar
echo ""
echo ""
ex:
echo "4. Exit"
echo ""
echo ""
read vvar
 
Old 11-04-2003, 07:49 PM   #14
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
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
 
Old 11-04-2003, 10:11 PM   #15
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
Shell Scripting again yaadhav Linux - Newbie 8 08-04-2005 11:21 AM
shell scripting pcdebb Linux - Newbie 17 11-20-2003 04:49 PM
shell scripting markus1982 Linux - General 2 05-20-2003 08:42 AM


All times are GMT -5. The time now is 02:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration