LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-19-2012, 06:56 PM   #1
DaverR
LQ Newbie
 
Registered: Apr 2012
Posts: 12

Rep: Reputation: Disabled
Question 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
 
Old 04-19-2012, 07:04 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Have you considered that you are putting the response in one var, but testing a different one ...
 
Old 04-19-2012, 08:30 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
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 08:33 PM.
 
Old 04-19-2012, 08:56 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Can't remember the last time I wrote a shell script that involved the read cmd ...
 
Old 04-20-2012, 02:30 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Might just be a typo, but I would also note the missing # from the shebang, should be:
Code:
#!/bin/bash
 
1 members found this post helpful.
Old 04-20-2012, 06:46 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
Quote:
Originally Posted by grail View Post
Might just be a typo, but I would also note the missing # from the shebang, should be:
Code:
#!/bin/bash
!!

jlinkels
 
Old 04-20-2012, 07:26 AM   #7
DaverR
LQ Newbie
 
Registered: Apr 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-20-2012, 09:58 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
So do you have another question or your right to go with the information provided?
 
Old 04-20-2012, 01:19 PM   #9
DaverR
LQ Newbie
 
Registered: Apr 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-20-2012, 01:28 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
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 01:29 PM.
 
Old 04-20-2012, 01:31 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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.
 
Old 04-22-2012, 09:07 PM   #12
DaverR
LQ Newbie
 
Registered: Apr 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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!
 
Old 04-23-2012, 02:11 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Yeah I remember using the Apple IIe at school

Here are some references for you:

http://mywiki.wooledge.org/TitleIndex
http://tldp.org/LDP/abs/html/
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 11:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration