#!/bin/sh
echo Enter choice :
echo a : To create a file, name given by user.
echo b : To copy file to another location.
echo c : To determine minimum age to vote.
read choice
case $choice in
a) echo Enter the name of file.
read name
mkdir $name ;;
b) echo Enter file name you want to copy
read file_name
echo Enter location where you want to copy $file_name
read location
cp -r $file_name "$location";;
c) echo Enter age :
read age
if [ age -ge 18 ]
then
echo You are eligible
else
echo You are not eligible
"fi"
;;
*) echo Please! Enter correct choice.
esac
in this I am choosing option c but it is showing the error which i cannot able to solve.
Error :
Enter choice :
a : To create a file, name given by user.
b : To copy file to another location.
c : To determine minimum age to vote.
c
./a.sh: 27: Syntax error: ";;" unexpected (expecting "fi")
Help to fix your code, you can compare your code as a reference
#!/bin/sh
echo Enter choice :
echo a : To create a file, name given by user.
echo b : To copy file to another location.
echo c : To determine minimum age to vote.
read -r choice
case $choice in
a) echo Enter the name of file.
read -r name
mkdir "$name" ;;
b) echo Enter file name you want to copy
read -r file_name
echo Enter location where you want to copy "$file_name"
read -r location
cp -r "$file_name" "$location";;
c) echo Enter age :
read -r age
if [ "$age" -ge 18 ]
then
echo You are eligible
else
echo You are not eligible
fi
;;
*) echo Please! Enter correct choice.
esac
Related
I am quite new in bash and would like to link appropriate choice with profile parameters for that user. Problem is that is not recognize my selection
I have already tried to test but have had syntax error near unexpected token `newline'
> echo "Name? Use, alphabetic letter from multiple choice.
a)Donald
b)Alan
c)Brian"
read -p "Insert appropriate letter a, b or c" don ala br
echo "Perfect" $don
echo "Perfect, OK" $ala
echo "Perfect, Nice" $br
case "$dev"
echo "OK"
esac
I would like to hit letter and enter afterwards go to case where define params for the profile.
I have encountered below error:
syntax error near unexpected token `newline'
select is usually used for command line menus. It normally expects only one answer, but you can tweak it for example in the following way:
#! /bin/bash
names=(Donald Alan Brian)
selected=()
PS3='Name? You can select multiple space separated options: '
select name in "${names[#]}" ; do
for reply in $REPLY ; do
selected+=(${names[reply - 1]})
done
[[ $selected ]] && break
done
echo Selected: "${selected[#]}"
Your current read command tries to split the input into 3 words, assigning each piece to one of don, ala, br. You just want to read one word, and compare it to each of a, b, or c. You can do this with a case statement:
read -p "Insert appropriate letter a, b, or c" choice
case $choice in
a) echo "Perfect $choice" ;;
b) echo "Perfect, OK $choice" ;;
c) echo "Perfect, Nice $choice" ;;
*) echo "Unrecognized selection: $choice" ;;
esac
Here's something that will restrict the input to only a, b, c; allowing upper or lower case; waiting for one of those values.
Putting the upper case value in THIS_SELECTION and then using it in a case statement:
#!/bin/bash
echo "Name? Use, alphabetic letter from multiple choice.
a)Donald
b)Alan
c)Brian"
THIS_SELECTION=
until [ "${THIS_SELECTION^}" == "A" ] || [ "${THIS_SELECTION^}" == "B" ] || [ "${THIS_SELECTION^}" == "C" ]; do
read -n1 -p "Insert appropriate letter a, b or c: " THIS_SELECTION
THIS_SELECTION=${THIS_SELECTION^}
echo;
done
case "${THIS_SELECTION}" in
A) echo A Donald ;;
B) echo B Alan ;;
C) echo C Brian ;;
*) echo other ;;
esac
#!/bin/bash
# search medicine list and generate a report
# script name: search.sh
# Loop to ask medication code and generic name or dose
# Enter 'ZZZ' to quit
while :
do
# taking medication code from the user
echo -n "Enter Medication code? "
read -r mcode
# converting medication code to upper case for comparision if necessary
mcode=$(echo $mcode|tr 'a-z' 'A-Z')
# if mcode is ZZZ, quit from the outer while loop
if [ "$mcode" == 'ZZZ' ]
then
break
fi
# loop to ensure generic name or dose is passed correctly
# if generic name is 'G' or 'D' this loop terminates
while :
do
# taking generic name or dose as input from user
echo -n "See Generic Name (G/g) or Dose (D/d) ? "
read gname
# converting gname to upper for comparision in if condition below
gname=$(echo $gname|tr 'a-z' 'A-Z')
if [ "$gname" == 'G' -o "$gname" == 'D' ]
then
break
else
echo "Please enter only G or D."
fi
done
# grepping given mcode in the medslist file and redirecting to file
/tmp/result
grep $mcode medslist>/tmp/result
# traversing through /tmp/result file and print required category and
medcode
while read line
do
genname=$(echo $line|cut -c5-25)
dose=$(echo $line|cut -c26-39)
if gname='G'
then echo "$genname"
elif gname='D'
then echo "$dose"
fi
done</tmp/result
# below condition will be true if medication code is wrong
if [ ! -s /tmp/result ]
then
echo "No such medication code"
fi
done
The problem i am having at this time is when i request the dosage information it is returning identical output to the output produced when the generic name is requested. Everything else seems to be working fine except i just can't get the program to display the dosage information.
You have several issues about quoting: paste your code into https://www.shellcheck.net/ and follow the recommendations.
However the main problem is this
if gname='G'
then echo "$genname"
elif gname='D'
then echo "$dose"
fi
You are checking the exit status of a variable assignment, not the result of a test command. You need
if [ "$gname" = 'G' ]
then echo "$genname"
elif [ "$gname" = 'D' ]
then echo "$dose"
fi
I am creating a simple phonebook using unix shell scripts. I have gotten all of my functions to work except the removal of a contact after it has been created. I have tried combining grep and sed in order to accomplish this, but cannot seem to get over the hump. The removal shell i've tried is as follows.
#!/bin/sh
#removeContact.sh
echo “Remove Submenu”
echo “Please input First Name:”
read nameFirst
echo “Please input Last Name:”
read nameLast
x=$(grep -e “$nameFirst” -e “$nameLast” ContactList)
echo $x
sed '/'$x'/ d' ContactList;
echo “$nameFirst $nameLast is removed from your contacts”
exit 0
I'm not sure if I am declaring x incorrectly, or if my syntax is wrong when sed is used.
Any help would be greatly appreciated. Thank you.
#!/bin/bash
ContactList="contacts.txt"
export ContactList
exit=0
while [ $exit -ne 1 ]
do
echo "Main Menu"
echo "(a) Add a Contact"
echo "(r) Remove a Contact"
echo "(s) Search a Contact"
echo "(d) Display All Contact’s Information"
echo "(e) Exit"
echo "Your Choice?"
read choice
if [ "$choice" = "a" ]
then
./addContact.sh
elif [ "$choice" = "r" ]
then
./removeContact.sh
elif [ "$choice" = "s" ]
then
./searchContact.sh
elif [ "$choice" = "d" ]
then
./displayContact.sh
elif [ "$choice" = "e" ]
then
exit=1
else
echo "Error"
sleep 2
fi
done
exit 0
#!/bin/sh
#addContact.sh
ContactList="contacts.txt"
echo “Please input First Name:”
read nameFirst
echo “Please input Last Name:”
read nameLast
echo “Please input Phone Number:”
read number
echo “Please Input Address”
read address
echo “Please input Email:”
read email
echo $nameFirst:$nameLast:$number:$address:$email>> ContactList;
echo "A new contact is added to your book."
exit 0
sed '/'$x'/ d' ContactList
won't remove anything from the file ContactList, it will simply output the changes to standard output.
If you want to edit the file in-place, you'll need the -i flag (easy) or to make a temporary file which is then copied back over ContactList (not so easy, but needed if your sed has no in-place editing option).
In addition, since ContactList is a shell variable referencing the real file contacts.txt, you'll need to use $ContactList.
And, as a final note, since you're using the full line content to do deletion, the presence of an address like 1/15 Station St is going to royally screw up your sed command by virtue of the fact it contains the / character.
I would suggest using awk rather than sed for this task since it's much better suited to field-based data. With the record layout:
$nameFirst:$nameLast:$number:$address:$email
you could remove an entry with something like (including my patented paranoid perfect protection policy):
cp contacts.txt contacts.txt.$(date +%Y.%m.%d.%H.%M.%S_$$)
awk <contacts.txt >tmp.$$ -F: "-vF=$nameFirst" "-vL=$nameLast" '
F != $1 || L != $2 {print}'
mv tmp.$$ contacts.txt
I keep getting an error on line 22: [!: command not found
My script Asks for a name, phone number, and date of birth and then amends these details to a comma separated value file called “birthday.csv”.
It then Sorts “birthday.csv” by date of birth. The newly sorted file is then displayed and calculates their age.
Can someone take a look at my script and see why this is popping up.
#!/bin/bash
a=0
while [ $a -lt 2 ];
do
echo Please enter a first name
read firstName
echo Please enter last name
read lastName
echo Please enter phone number
read phoneNumber
echo Please enter date of birth - format dd/mm/yyyy
read dob
echo "$firstName,$lastName,$phoneNumber,$dob" >> userBirthdays.csv
echo If you would like to add another person press 1 or enter 2 to proceed
read a
done
INPUT=./userBirthdays.csv
OLDIFS=$IFS
IFS=","
[! -f INPUT] & while read Name Surname Telephone DOB
do
birthMonth=${DOB:0:2}
birthDay=#10${DOB:3:2}
birthYear=${DOB:6:4}
currentDate=`date +%d/%m/%Y`
currentMonth=${currenDate:0:2}
currentDay=#10${currentDate:3:2}
currentYear=${currentDate:6:4}
if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$((#10$currentDay))" -lt "$((#10$birthDay))" ]]
then
let Age=currentYear-birthYear-1
else
let Age=currentYear-birthYear
fi
echo "Name : $Name"
echo "Surname : $Surname"
echo "Telephone : $Telephone"
echo "DOB : $DOB"
echo "Age : $Age"
echo "##########################################"
done < $INPUT
IFS=$OLDIFS
echo $DATE
exit 0;
Thank you in advance
you need a space between between the [ and ! chars, i.e.
[ ! -f $INPUT ] && while read ....
#^-^--------^-^---^------------
Note that you almost certainly want two '&' chars, as in my correction.
Thanks to #GordonDavisson for another '$' ;-)
IHTH
Separate [ and ! with a space, so [ will be a command, and ! will be its first argument, as you meant them to be.
(Not sure there are no other problems).
I am getting the error: line 34: #10#1001: syntax error: operand expected (error token is "#10#1001")
I have just changed
[! -f INPUT ] &
to
[ ! -f INPUT ] &&
and now i am getting the error
line 34: #10#1001: syntax error: operand expected (error token is "#10#1001")
when i run the script.
The script inputs users names, phones numbers and dob and then sorts the infomation and calculates their ages.
What could be causing this error as i cannot work out what operand it is demanding
#!/bin/bash
a=0
while [ $a -lt 2 ];
do
echo Please enter a first name
read firstName
echo Please enter last name
read lastName
echo Please enter phone number
read phoneNumber
echo Please enter date of birth - format dd/mm/yyyy
read dob
echo "$firstName,$lastName,$phoneNumber,$dob" >> userBirthdays.csv
echo If you would like to add another person press 1 or enter 2 to proceed
read a
done
INPUT=./userBirthdays.csv
OLDIFS=$IFS
IFS=","
[ ! -f INPUT ] && while read while read Name Surname Telephone DOB
do
birthMonth=${DOB:0:2}
birthDay=#10${DOB:3:2}
birthYear=${DOB:6:4}
currentDate=`date +%d/%m/%Y`
currentMonth=${currenDate:0:2}
currentDay=#10${currentDate:3:2}
currentYear=${currentDate:6:4}
if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$((#10$currentDay))" -lt "$((#10$birthDay))" ]]
then
let Age=currentYear-birthYear-1
else
let Age=currentYear-birthYear
fi
echo "Name : $Name"
echo "Surname : $Surname"
echo "Telephone : $Telephone"
echo "DOB : $DOB"
echo "Age : $Age"
echo "##########################################"
done < $INPUT
IFS=$OLDIFS
echo $DATE
exit 0;
You have two mistakes on this line:
[ ! -f INPUT ] && while read while read Name Surname Telephone DOB
It should be:
[ -f ${INPUT} ] && while read Name Surname Telephone DOB
I recommend debugging your script with:
bash -x /path/to/birthdays.bash
This will print command traces before executing each command.
-lt comparison (and friends) are for integers, not strings. And you turn currendDay into a non-integer string by prepending #10 to it (you do it twice, by the way).
Your intentions here are not very clear. Do you want to compare strings? Then use < instead of -lt. Do you want currentDay to be a number? Then don't prepend #10 to its value (in both places you do it now).