Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
|
 |
04-19-2012, 05:56 PM
|
#1
|
LQ Newbie
Registered: Apr 2012
Posts: 12
Rep: 
|
Bash Script Help
Hello everyone. I am trying to create a script as shown below. As you can see by the first line you can see what I am trying to do. I am having trouble getting it started. If you run it as is it will read the prompt and then exit the script without executing the command. Can anyone point me in the right direction? I have done reading and it still is confusing to me how to get started and make it work.
!/bin/bash
echo "Please select l to list files of a directory, b to backup a file or directory, u to edit a user's password, and x to exit the script"
read $answer
case $l in
l)
printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd
ls
;;
esac
|
|
|
04-19-2012, 06:04 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
Have you considered that you are putting the response in one var, but testing a different one ...
|
|
|
04-19-2012, 07:30 PM
|
#3
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
read $answer (you missed that one chrism01  )
The $ is not allowed.
You should test $answer, not $l (as chrism01 said)
Use the red colored command in my signature
jlinkels
Last edited by jlinkels; 04-19-2012 at 07:33 PM.
|
|
|
04-19-2012, 07:56 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
 Can't remember the last time I wrote a shell script that involved the read cmd ...
|
|
|
04-20-2012, 01:30 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
Might just be a typo, but I would also note the missing # from the shebang, should be:
|
|
1 members found this post helpful.
|
04-20-2012, 05:46 AM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Quote:
Originally Posted by grail
Might just be a typo, but I would also note the missing # from the shebang, should be:
|
 !!
jlinkels
|
|
|
04-20-2012, 06:26 AM
|
#7
|
LQ Newbie
Registered: Apr 2012
Posts: 12
Original Poster
Rep: 
|
Yes. The "#" was left off when I copied the script. Writing scripts is all new to me. I understand the very basics and what it is supposed to do but the commands needed and how they are used is confusing to me. I really don't have the time I would like to devote to learning it the way I want to. I am trying to get the script to work for a school project. If I could figure out how to get the first lines to work I could probably figure out the rest. If you run the printf command by itself it does what I want it to for that sections except it allows you to enter any letter you want instead of just the "l". I want to specify the user use these letters only and have a command for each one. Thanks for all your input.
|
|
|
04-20-2012, 08:58 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
So do you have another question or your right to go with the information provided?
|
|
|
04-20-2012, 12:19 PM
|
#9
|
LQ Newbie
Registered: Apr 2012
Posts: 12
Original Poster
Rep: 
|
Grail
I understand I need to get rid of the read command but I am not sure how to exactly start the script to read the "l" and so forth. If I can get that part I should be able to do the rest.
Thanks
|
|
|
04-20-2012, 12:28 PM
|
#10
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
This should be able to get you started then:
Code:
#!/bin/bash
echo "Please select l to list files of a directory, b to backup a file or directory, u to edit a user's password, and x to exit the script"
read answer
case "$answer" in
l)
echo "You entered l"
;;
b)
echo "You entered b"
;;
u)
echo "You entered u"
;;
x)
echo "You entered x"
;;
*)
echo "Invalid selection"
;;
esac
Last edited by suicidaleggroll; 04-20-2012 at 12:29 PM.
|
|
|
04-20-2012, 12:31 PM
|
#11
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
No the read command is fine. You can alternatively use parameters on the command line but at this point they may be a little advanced.
If you follow the advice above, ie. $ is only required when getting information from the variable (like in the case statement) but other wise it should not have one.
The other advice being that if you assign information to one variable, like "answer", then there is little point using another variable in the case statement, like "l" as it will
be blank and hence the case will never match anything.
|
|
|
04-22-2012, 08:07 PM
|
#12
|
LQ Newbie
Registered: Apr 2012
Posts: 12
Original Poster
Rep: 
|
Grail
Thanks for all your help. I would not have been able to figure it out without you unless I had more time. It may be simple to all you guys but the last time I did this was in high school when we had Apple computers in the '80s. Man I feel old! I know where to go if I need more help. Thanks everyone!
|
|
|
All times are GMT -5. The time now is 05:56 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
|
|