Bash shell script remove text from file/ find text in file [duplicate] - linux

This question already has answers here:
Does not work to execute command in double brackets in bash
(3 answers)
Getting "command not found" error while comparing two strings in Bash
(4 answers)
How to delete from a text file, all lines that contain a specific string?
(21 answers)
using awk with column value conditions
(6 answers)
Closed 5 years ago.
I am new to Bash and was wondering if someone could give me some insight on how I can make this program work more accurately.
Goal: To write a bash shell script that presents a menu to the user with the options of add a contact, remove a contact, find a contact, list all contacts, and exit the program.
This is my code so far:
#!/bin/bash
touch contacts.dat
echo "Select one of the following options:"
echo "-------------------------------------"
echo "1. Add a contact"
echo "2. Remove a contact"
echo "3. Find a contact"
echo "4. List all contacts"
echo "5. Exit the program"
read -p "Enter a choice (1-5): " choice
echo
#
case "$choice" in
1)
read -p "Enter a name: " name
read -p "Enter an email address: " email
echo "$name , $email" >> contacts.dat
;;
2)
read -p "Enter a name: " name
if (grep -q name contacts.dat) then
grep -v name contacts.dat > deletedNames.dat
echo "$name was removed from the file."
else
echo "$name does not exist in the file."
fi
;;
3)
read -p "Enter a name: " name
if (grep -q name contacts.dat) then
echo "$name , $email"
else
echo "The $name was not found."
fi
;;
4)
sort -k 1 contacts.dat
;;
5)
echo "Thank you for using this program!"
# break?
exit 1
;;
*)
echo "Please enter a valid choice (1-5)."
;;
esac
The program seems to work with options 1, 4, and 5. However, not with 2 and 3.
How can I get 2 and 3 to remove the contact and find the contact (respectfully)? Thank you in advance for any help you may be able to offer.

Related

Problems with the control design [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
Good evening (or day. Whichever)! I ran into a problem while writing a bash script. I have the code
#!/bin/bash
echo "Available options:"
echo "1. text"
echo "2. exit"
read var1
echo
if [[$var -ge 0]]; then
echo "hello world"
elif [[$var -ge 1]]; then
echo "good bye"
else
echo "error"
fi
and all the time it puts the emphasis on the operator
./proba3.sh: line 9: [[: command not found
./proba3.sh: line 11: [[: command not found
I tried many variations of the branching operation, but still get the same error. I will be very glad for advice or hints on how to fix this error. Thanks in advance
You need spaces inside the brackets.
Also you misspelled the variable name.
Finally, you should probably test for equality not ge
Fixed:
#!/bin/bash
echo "Available options:"
echo "1. text"
echo "2. exit"
read var
echo
if [[ $var -eq 1 ]]; then
echo "hello world"
elif [[ $var -eq 2 ]]; then
echo "good bye"
else
echo "error"
fi

BASH scripting - menu not showing echos

I'm extremely new to this, but can anyone tell me why when I run this, it doesn't show my echo's?
It accepts all my inputs, but say when I do the subtraction one it doesn't display
echo "$a - $b = $(($a-$b))"
It worked if I put an Exit after each case, but I would like it to keep going after it completes one input.
#!/bin/bash
while true; do
clear
cat <<EOF
Please Select:
Option 1. Quit
Option 2. Display Options Again
Option 3. Subtraction
Option 4. Division
Option 5. Who am I?
EOF
read -p "Enter selection [1-5] > "
case "$REPLY" in
1)
break
;;
2)
;;
3)
read -p "Enter the First Number: " a
read -p "Enter the Second Number: " b
echo "$a - $b = $(($a-$b))"
;;
4)
printf "Enter the First Number: "
read a
printf "Enter the Second Number: "
read b
echo "$a / $b = $(($a/$b))"
;;
5)
echo "My name is Michelle"
;;
*)
echo "Invalid entry."
;;
esac
done
echo "Program terminated."
Any help would be greatly appreciated.
Problem here is that the first command in the loop is clear. This removes the output immediately after it was displayed.

Calling a different bash script from a running script based on passed arguments [duplicate]

This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 2 years ago.
I am tasked with making a user menu that will have 2 different variations,
one for the novice user and the others for an expert.
I got it calling all my novice scripts, but in order to call my expert scripts, my main script needs to detect if a user has passed any of the arguments like "help, man, status" etc. And if they do I need my main script to call expert scripts in that case.
I am very new to bash scripting so I hope I can get some guidance here.
#!/bin/bash
echo -n "Name please? "
read name
echo "Menu for $name"
echo "===================================================="
echo -e "Enter
( 1 ) File and Directory Managment Commands\nEnter
( 2 ) Text Processing Commands\nEnter
( 3 ) System Status Commands\nEnter
( 4 ) Exit"
echo "Enter your choice :"
read en
case "$en" in
1)sh ./fileNovice.sh;;
2)sh ./textNovice.sh;;
3)sh ./systemStatusNovice.sh;;
4)echo "Good Bye..."
sleep 0;;
*)echo "Wrong Option selected" ;;
esac
Do not reinvent the wheel and use Bash's select:
#!/usr/bin/env bash
declare -a level_names=([0]='Novice' [1]='Expert')
declare -i level=0
while :; do
printf 'Current mode : %s\n' "${level_names[level]}"
PS3='Enter your choice : '
select _ in \
'File and Directory Managment Commands' \
'Text Processing Commands' \
'System Status Commands' \
"Switch to ${level_names[level ^ 1]} mode" \
'Exit'; do
case "$REPLY" in
1) bash "./file${level_names[level]}.sh"; exit ;;
2) bash "./text${level_names[level]}.sh"; exit ;;
3) bash "./systemStatus${level_names[level]}.sh"; exit ;;
4) level=$((level ^ 1)); break ;;
5) exit ;;
*) echo 'Wrong Option selected'; break ;;
esac
done
done

do-while loop using user input in shell scritping [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 2 years ago.
I am a beginner in bash scripting. and i look for similar questions but none gave me the results am looking for.
I want to create a shell that keep giving the user some options to choose from while the choice is not exit
Here is my code
choice=0
while [$choice!=4]
do
echo "please choose the operation you want!"
echo "1) Delete Data"
echo "2) Insert Data"
echo "3) Get Number of customers"
echo "4) Exit"
choice=$1
if [$choice=1]; then
echo "you have chosen to Delete Data"
elif [$choice=2]; then
echo "you have chosen to Insert Data"
elif [$choice=3]; then
echo "you have chosen to Get number of customers"
fi # EOF if
done
As you can see that i want my code to keep asking for the user options until the answer is not exit.
My Question is this code gives me an infinite loop.
Bash, ksh or zsh provides you with the select command that is exactly designed to present numbered options menu:
To get the usage of the select command, type help select in your bash terminal.
select: select NAME [in WORDS … ;] do COMMANDS; done
Select words from a list and execute commands.
The WORDS are expanded, generating a list of words. The set of expanded words is printed on the standard error, each preceded by a number. If in WORDS is not present, in "$#" is assumed. The PS3 prompt is then displayed and a line read from the standard input. If the line consists of the number corresponding to one of the displayed words, then NAME is set to that word. If the line is empty, WORDS and the prompt are redisplayed. 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. COMMANDS are executed after each selection until a break command is executed.
Exit Status:
Returns the status of the last command executed.
Here is an implementation of it with your selection menu:
#!/usr/bin/env bash
choices=('Delete Data' 'Insert Data' 'Get Number of customers' 'Exit')
PS3='Please choose the operation you want: '
select answer in "${choices[#]}"; do
case "$answer" in
"${choices[0]}")
echo 'You have chosen to Delete Data.'
;;
"${choices[1]}")
echo 'You have chosen to Insert Data.'
;;
"${choices[2]}")
echo 'You have chosen to Get number of customers.'
;;
"${choices[3]}")
echo 'You have chosen to Exit.'
break
;;
*)
printf 'Your answer %q is not a valid option!\n' "$REPLY"
;;
esac
done
unset PS3
Another implementation of the select command, using arguments rather than an array of choices:
#!/usr/bin/env ksh
PS3='Please choose the operation you want: '
set -- 'Delete Data' 'Insert Data' 'Get Number of customers' 'Exit'
select answer; do
case "$answer" in
"$1")
echo 'You have chosen to Delete Data.'
;;
"$2")
echo 'You have chosen to Insert Data.'
;;
"$3")
echo 'You have chosen to Get number of customers.'
;;
"$4")
echo 'You have chosen to Exit.'
break
;;
*)
printf 'Your answer %q is not a valid option!\n' "$REPLY"
;;
esac
done
If you want to use while loop with conditional 'case' syntax, try this:
while true
do
echo "1) Delete Data"
echo "2) Insert Data"
echo "3) Get Number of customers"
echo "4) Exit"
echo -ne "Enter your choice [0-4] > \c"
read choice
case "$choice" in
1) echo "you have chosen to Delete Data" ; break
;;
2) echo "you have chosen to Insert Data"; break
;;
3) echo "you have chosen to Get number of customers"; break
;;
4) exit
;;
*) clear
;;
esac
done
i found the error. it was statement typing issue not in the algorithm
my new code is
while [ "$(read choice)"!="4" ]
do
if [ "$choice"="1" ]; then
echo "you have chosen to Delete Data"
elif [ "$choice"="2" ]; then
echo "you have chosen to Insert Data"
elif [ "$choice"="3" ]; then
echo "you have chosen to Get number of customers"
fi # EOF if
echo "please choose the operation you want!"
echo "1) Delete Data"
echo "2) Insert Data"
echo "3) Get Number of customers"
echo "4) Exit"
done

Writing a Shell Script - which can print the error and could continue to keep the input you just run in command line

I am writing a shell script (named "test-script.sh"). One feature is as below:
When I typing the script name in CLI and click <ENTER>, The script name will be keeped in CLI following with the cursor when an error encountered.
Such as:
me#ubuntu: $ test-script.sh <ENTER>
me#ubuntu: $ Sorry, you must input at least one parameter.
me#ubuntu: $ test-script.sh[cursor] <<<< Here the script name auto printed and following with the cursor.
Let's take echo ${!!d} as an example. When you run echo ${!!d} in CLI, an error will print but the cmd echo ${!!d} will hold in CLI.
Here echo is my test-script.sh.
How to achieve this feature in my script?
Thank you~
echo $0 will print the script name: $0 - is script name, $1 is 1st CL argument, $2 is 2nd CL argument and so on..., $# is number of argument.
Example:
output:
$ ./test_script.sh
Sorry, you must input at least one parameter.
here are the options:
1. for option one
2. for option two
3. for option three
./test_script.sh 1
do you stuff for option 1 here
$
Script:
#!/bin/sh
menu(){
echo "here are the options:"
echo "1. for option one"
echo "2. for option two"
echo "3. for option three"
}
if [ $# -eq 0 ];
then
echo "Sorry, you must input at least one parameter."
menu
read -p "$0 " option
else
option=$1
fi
case $option in
"1") echo "do you stuff for option 1 here" ;;
"2") echo "do you stuff for option 2 here" ;;
"3") echo "do you stuff for option 3 here" ;;
*)
echo
echo "Invalid option: ${option}"
echo "...exiting...";sleep 1
exit
;;
esac

Resources