I am trying to create a BASH shell script in which I prompt the user to enter an animal, and return "The $animal has (number I set in case statement) legs"
I am using a case statement for this. My current statement is below:
#!/bin/bash
echo -n "Enter an animal: "
read animal
case $animal in
spider|octopus) echo "The $animal has 8 legs";;
horse|dog) echo "The $animal has 4 legs";;
kangaroo|person) echo "The $animal has 2 legs";;
cat|cow) echo "The $ animal has 4 legs";;
*) echo "The $animal has an unknown number of legs"
esac
For a cat or cow, I need to be able to echo "The (xyz) (cat or cow) has 4 legs" I am thinking of using a grep somewhere but don't know if that is the best option for this. Can anyone help out?
Thanks!
With chepner's suggestion:
#!/bin/bash
echo -n "Enter an animal: "
read -r animal
case $animal in
spider|octopus) n=8;;
horse|dog) n=4;;
kangaroo|person) n=2;;
cat|cow) n=4;;
*) n="an unknown number of"
esac
echo "The $animal has $n legs"
It seems that adding *cat|*cow) instead of cat|cow) worked without any need for grep. Thanks
Related
I'm writing a script in Unix but I need a way to check an argument that is entered in the command line is a specific word.
So if when using the script the user types:
$ ./script hello
my script can tell that "hello" was entered as an argument and can display a message appropriately.
And if the user types something other than "hello" as an argument then my script can display another message.
Thanks.
This should work:
#!/bin/bash
if [[ $1 == hello ]];then
echo "hello was entered"
else
echo "hello wasn't entered"
fi
There are a number of ways to check positional arguments against a list. When there are a number of items in the list, you can use a case statement instead of a string of if ... elif ... elif ... fi comparisons. The syntax is as follows:
#!/bin/bash
case "$1" in
"hello" )
printf "you entered hello\n"
;;
"goodbye" )
printf "well goodbye to you too\n"
;;
* )
printf "you entered something I don't understand.\n"
;;
esac
exit 0
Output/Use
$ ./caseex.sh hello
you entered hello
$ ./caseex.sh goodbye
well goodbye to you too
$ ./caseex.sh morning
you entered something I don't understand.
In Bash arguments passed to shell scripts are stored in variables named as follows:
$0 = name of the script.
$1~$n = arguments.
$# = number of arguments.
$* = single string of all arguments: "arg1,arg2,..."
you can simply use if [ $1 == "some string" ]; then ...
You can retrieve the command line arguments with $(number)
for example the first argument would exist at $1 the second at $2 etc.
You can use conditionals in BASH (I assume you are using bash) just like any other language; however the syntax is a bit wonky :). here is a link for you
http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_07.html
If you are sure about the position of the argument you can :
#!/bin/bash
if [[ $1 == SearchWord]];then
echo "SearchWord was entered"
else
echo "SearchWord wasn't entered"
fi
Incase you are not sure:
You can use $*
[ `echo $* | grep $SearchWord| wc -l` -eq 1 ] && echo "Present"|| echo "Not present"
i'm quite new to shell programming.. and working on project
am doing Select Menu using the Select Case
below is the scripting
#! /usr/bin/bash
echo "1) Add new book"
echo "2) Remove existing book info"
echo "3) Update book info and quantity"
echo "4) Search for book by title/author"
echo "5) Process a book sold"
echo "6) Inventory summary report"
echo "7) Quit"
PS3="Please enter your option: "
select book in Add Remove update search process inventory quit
#read book
do
case $book in
1) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
2) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
3) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
4) echo "Search for book by title/author"
break
;;
5) echo "Process a book sold"
break
;;
6) echo "Inventory summary report"
break
;;
7) echo "Quit"
exit
;;
esac
done
and below is the shell commands in the Ubuntu terminal
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
1) Add 3) update 5) process 7) quit
2) Remove 4) search 6) inventory
Please enter your option: 1
Please enter your option:
the words in the echo do not appear after i entered 1.and even if i I choose Quit, it doesn't go back to the menu. how do i get it to work? :(
Any help is greatly appreciated. thanks! :)
From the bash manpage (my italics and bold):
select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below).
The PS3 prompt is then displayed and a line read from the standard input.
If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word.
If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes.
Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed.
The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.
Hence, in your case statement, you need "Add" rather than 1, ditto for the others.
You should also look into what you're actually doing in each case. Obviously the latter ones are empty because you haven't yet gotten around to them, but your use of sed in the second case will not remove anything from the file as it stands. And, for the third case:
echo $title < BookDB.txt
in ... interesting. If it were output redirection, I could understand the beginnings of a plan but the line as you have it will simply output $title and totally ignore the content of your input redirection.
No doubt you'll fix them in time, I just thought I'd bring them to your attention as something that needs looking at.
Try this:
PS3="Please enter your option: "
select book in Add Remove Update Search Process Inventory Quit
do
case $book in
"Add")
...
;;
"Remove")
...
;;
"Update")
...
;;
etc...
esac
done
Under select statement, in case you should use the string stated in select. Here you should do it like this:
select book in Add Remove update search process inventory quit
do
case $book in
Add) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
Remove) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
update) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
search) echo "Search for book by title/author"
break
;;
process) echo "Process a book sold"
break
;;
inventory) echo "Inventory summary report"
break
;;
quit) echo "Quit"
exit
;;
esac
done
You can understand the code as below:
select prepare many values for book, and which value assigned is due to what we input. The input should be integer indexed begin from 1.
For further understanding, you should learn much about bash and select system call.
Replace
select book in Add Remove update search process inventory quit
With:
select book in 1 2 3 4 5 6 7
#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans= 'expr $input/5'
echo "$ans"
So i want this code to be something like this, a user will enter a number that can divided by 5 (5,10,15,20 etc etc) and then it will echo the answer, i am not sure whats wrong with my code it keeps saying "no such file or directory " for output not sure how to fix that.
I think the spaces were your problem...
#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans=`expr $input / 5`
echo "$ans"
Program errors on the lines with the double semi-colons, stating a Syntax error near unexpected token. Any suggestions?
#!/bin/bash
echo "Enter the access password..."
while
do
read INPUT_STRING
case $INPUT_STRING in
##CORRECT PASSWORD##
lux)
ls -l -S > directory.txt
echo "Enter your username..."
read a
sed '1 i\ $a' directory.txt
date=`date`
sed '2 i\ $date' directory.txt
date=
echo "The operation has completed successfully"
;;
##INCORRECT PASSWORD##
*)
x=1
while [ $x -le 3 ]
do
echo "Incorrect Password, try again. The program will exit after 3 failed attempts."
x=$(( $x + 1 ))
sleep 2
echo "Enter the access password..."
if x=3
then exit
fi
;;
esac
done
echo
echo "Process Complete, Goodbye!"
Your while syntax is messed up. You need a condition between the while and the do. That's probably screwing up the parsing of the case statement.
There is a syntax error in your code. You need to provide an expression after while.
Right now, you have:
while
do
and you need to specify the epxression that the while keyword will loop on. More specifically, it looks like you just want an infinite loop. If this is true, you need to specify:
while true
do
You seem to be lacking a done to close the while statement in the *) case
I don't understand why this simple read doesn't work. Mind you, I am VERY new to bash. :)
#!/bin/bash
echo -n "Project Name: "
read PROJECT_NAME
if [ -n "$PROJECT_NAME" ]; then
echo "You must provide a project name."
exit 2
fi
-- snip --
When this executes, it asks for the project name. After I press enter, I get "You must provide a project name." and then the scripts exists instead of continuing.
What am I doing wrong?
Thanks
Eric
You want [ -z "$PROJECT_NAME" ], not -n:
From man test:
-n STRING
the length of STRING is nonzero
...
-z STRING
the length of STRING is zero
to avoid confusion from -n or -z , you can just use case/esac to compare strings
case "$PROJECT_NAME" in
"" ) echo "No";;
*) echo "Have";;
esac