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
Related
The problem I have is with echo cannot echo e.g: "$prefix_$suffix". This is a assignment for a class in school if that changes things.
I've tried e.g "$prefix _$suffix" but that creates a space between the prefix and suffix
#!bin/bash
read -p "Username prefix: " prefix
read -p "Amount of users: " amount
read -p "Name of file to store, include extension (e.g test.txt): " filename
touch "$filename"
new="$amount"
suffix=0
state=true
while [ state=true ] ; do
#in this function i reverse the user input amount of users so it appears as user 1,2,3 (and so on) in the first line of the text file that is also user input.
if [ "$new" -ge 1 ] ; then
newpass="$(gpg --gen-random --armor 1 12)"
#reversing process, making the suffix start at 1 so user 1 gets assigned suffix 1 for the username and i decrease the "new" variable that gets set to "$amount" so the while loop isn't indefinite
new=`expr "$new" - 1`
suffix=`expr "$suffix" + 1`
echo -n "$prefix" >> "$filename"
echo -n "_$suffix" >> "$filename"
echo -n " " >> "$filename"
echo "$newpass" >> "$filename"
echo -e >> "$filename"
elif [ "$new" -eq 0 ] ; then
break
fi
done
a run of this bash results in 5 lines e.g:
re_1 UlrZW3jB5L9zt6Nf
and so on, depending how many users you choose at the input
however the next task is to create users with the username, in this example re_1 with the password: UlrZW3jB5L9zt6Nf. This is where the clunky echo stuff I've done doesn't work. I tried doing useradd -u "$prefix_$suffix" and "$prefix $suffix" , none of these work since "$prefix$suffix" is treated as one call instead of two and the "$prefix _$suffix" adds one space in between the prefix and suffix which is not acceptable.
Even if this looks very introverted to you, hence i added comments to make it understandable, help is very appreciated.
Feel free to ask question if you do not understand and want to help!
This will do what you want:
echo "${prefix}_${suffix}"
So I have a file called "nouns" that looks like this:
English word:matching Spanish word
Englsih word:matching Spanish word
..etc etc
I need to make a program that list all the English words with an option to quit. The program displays the English words and ask the user for the word he wants translated and he can also type "quit" to exit.
This is what I have so far that shows me the list in English
select english in $(cut -d: -f1 nouns)
do
if [ "$english" = 'quit' ]
then
exit 0
fi
done
I know that I need to run a command that pulls up the second column (-f2) by searching for the corresponding English word like this
result=$(grep -w $english nouns|cut -d: -f2)
My end result should just out put the corresponding Spanish word. I am just not sure how to get all the parts to fit together. I know its based in a type of "if" format (I think) but do I start a separate if statement for the grep line?
Thanks
You need a loop in which you ask for input from user. The rest is putting things together with the correct control flow. See my code below:
while :
do
read -p "Enter word (or quit): " input
if [ "$input" = "quit" ]; then
echo "exiting ..."
break
else
echo "searching..."
result=$(grep $input nouns | cut -d ':' -f 2)
if [[ $result ]]; then
echo "$result"
else
echo "not found"
fi
fi
done
dfile=./dict
declare -A dict
while IFS=: read -r en es; do
dict[$en]=$es
done < "$dfile"
PS3="Select word>"
select ans in "${!dict[#]}" "quit program"; do
case "$REPLY" in
[0-9]*) w=$ans;;
*) w=$REPLY;;
esac
case "$w" in
quit*) exit 0;;
*) echo "${dict[$w]}" ;;
esac
done
You want to run this in a constant while loop, only breaking the loop if the user enters "quit." Get the input from the user using read to put it in a variable. As for the searching, this can be done pretty easily with awk (which is designed to work with delimited files like this) or grep.
#!/bin/sh
while true; do
read -p "Enter english word: " word
if [ "$word" = "quit" ]; then
break
fi
# Take your pick, either of these will work:
# awk -F: -v "w=$word" '{if($1==w){print $2; exit}}' nouns
grep -Pom1 "(?<=^$word:).*" nouns
done
I have a problem with writing bash script. The problem is in comparison of strings. When I launch it, there's no errors. However in result, it is always changing the variable client.
So if for an example we have two lines in file
apple A
orange D
and if I give the who=A I expect to see in result apple, or if at D - orange
But no matter of what I choose A or D it is always giving me the result - orange
No matter of the strings, it always change the variable client, like ignoring the comparison. Please help.
while read line
do
IFS=" "
set -- $line
echo $2" "$who":"$1
if [[ "$2"="$who" ]]
then
echo "change"
client=$1
fi
done < $file
echo $client
So now I changed the code as in one of the comment below, but now the caparison always false therefore the variable client is always empty
while read -r line
do
#IFS=" "
#set -- $line
#echo $2" "$who":"$1
#if [[ "$2" = "$who" ]]
a="${line% *}"
l="${line#* }"
if [[ "$l" == "$who" ]]
then
echo "hi"
client="$a"
fi
done < $file
If you have data in a file with each line like apple D and you want to read the file and separate then items, the parameter expansion/substring extraction is the correct way to process the line. For example (note $who is taken from your problem statement):
while read -r line
do
fruit="${line% *}" # remove from end to space
letter="${line#* }" # remove from start to space
if [[ "$letter" == "$who" ]]
then
echo "change"
client="$fruit"
fi
done < $file
Short Example
Here is a quick example of splitting the words with parameter expansion/substring extraction:
#!/bin/bash
while read -r line
do
fruit="${line% *}"
letter="${line#* }"
echo "fruit: $fruit letter: $letter"
done
exit 0
input
$ cat dat/apple.txt
Apple A
Orange D
output
$ bash apple.sh <dat/apple.txt
fruit: Apple letter: A
fruit: Orange letter: D
Change if [[ "$2"="$who" ]] to
if [[ "$2" = "$who" ]]
spaces around =
Example (for clarification):
who=A
while read line
do
IFS=" "
set -- $line
echo $2" "$who":"$1
if [[ "$2" = "$who" ]]
then
echo "change"
client=$1
fi
done < file #this is the file I used for testing
echo $client
Output:
A A:apple
change
D A:orange
apple
For who=D:
A D:apple
D D:orange
change
orange
You do need spaces around that = operator.
However, I think you're facing yet another issue as you're trying to change the value of the client variable from inside the while loop (which executes in a subshell). I don't think that will work; see this quesion for details.
Ok, so now I have it setup like this but still it is giving me a ; exit;
logout
[Process completed]
when run through the terminal I know that there are configuration profiles that are not on my computer that should make the script continue or rather to keep looping. What is this not happening?
Thanks in advance....!
Here is what i have so far:
#!/bin/bash
profilesInstalled=`profiles -P|awk '/attribute/ {print $4}'`
while read line ; do
if [ "$line" = "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" ];then
echo "AD Binding is present"
elif [ "$line" = "1C94DAD1-5FC7-46CE-9E09-576841C15093" ];then
echo "Energy Saver is present"
elif [ "$line" = "A0E5B977-F0AF-44C9-8001-DA0511B702B8" ];then
echo "Finder is present"
elif [ "$line" = "5E9DE5BF-34E4-4A7F-AA29-461FB0631943" ];then
echo "FV2 Redirect is present"
elif [ "$line" = "9AE91C88-D1B2-4227-9E95-80F492DCAA11" ];then
echo "Login Window/Security and Privacy is present"
elif [ "$line" = "00000000-0000-0000-A000-4A414D460003" ];then
echo "MDM Profile is present"
elif [ "$line" = "5E85BBF0-3483-4C80-A1FC-70AF20F82E7C" ];then
echo "Restrictions is present"
elif [ "$line" = "E433D546-5502-4C3F-9E5F-4732ED1F0032" ];then
echo "SAC SUBCA-01 is present"
elif [ "$line" = "5C2AE16B-D4E9-4D15-B190-3CD7B28779E8" ];then
echo "SAC SUBCA-02 is present"
elif [ "$line" = "2C620A13-DF1E-4F6A-A32B-9FA3149F8A56" ];then
echo "SAC-CA-01 is present"
elif [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then
echo "Screensaver is present"
elif [ "$line" = "396A9D84-A9CA-4575-8D09-C9F054B76AF7" ];then
echo "Spotlight is present"
elif [ "$line" = "E0138F02-9A15-47BD-8CA5-7D1D0985A1A6" ];then
echo "Workday Corp is present"
fi
exit 0
done <<<"$profilesInstalled"
You need a space around the = in those tests. That first test will always pass as written.
You should also quote the "$line" variable expansion.
Unless you use $profilesInstalled somewhere else you don't need that variable at all and can just pipe the profiles pipeline to the while loop directly.
You can also replace grep in that pipeline with awk '/attribute/ {print $4}'.
Some "meta" remarks first:
Please don't change your original question substantially, as that can invalidate existing answers (such as Etan Reisner's helpful answer)
Instead, add later changes to the original question and mark the changes as such.
If a different (follow-up) question arises, ask it as a separate, new question.
Please learn how to format your code properly - it was done for you, and you managed to destroy that formatting again with your later edits.
Please read about how to provide an MCVE (a Minimal, Complete, and Verifiable Example).
Not doing these things:
makes it far less likely that you'll get the help you need.
makes your question and its answers less valuable to future readers.
Here's a cleaned-up version of your code:
# Helper function to determine a string's element index in an array.
# SYNOPSIS
# indexOf needle "${haystack[#]}"
# *Via stdout*, returns the zero-based index of a string element in an array of strings or -1, if not found.
# The *return code* indicates if the element was found or not.
indexOf() {
local e ndx=-1
for e in "${#:2}"; do (( ++ndx )); [[ "$e" == "$1" ]] && echo $ndx && return 0; done
echo '-1'; return 1
}
# Define array of profile IDs, and parallel ID of profile names.
# Note: in bash 4+, this could be handled more elegantly with a single
# associative array.
profileIds=( F2CC78D2-A63F-45CB-AE7D-BF2221D41218 1C94DAD1-5FC7-46CE-9E09-576841C15093
A0E5B977-F0AF-44C9-8001-DA0511B702B8 5E9DE5BF-34E4-4A7F-AA29-461FB0631943
9AE91C88-D1B2-4227-9E95-80F492DCAA11 00000000-0000-0000-A000-4A414D460003
5E85BBF0-3483-4C80-A1FC-70AF20F82E7C E433D546-5502-4C3F-9E5F-4732ED1F0032
5C2AE16B-D4E9-4D15-B190-3CD7B28779E8 2C620A13-DF1E-4F6A-A32B-9FA3149F8A56
3B44AE14-E0CE-4621-BACF-1A9C3BA4A459 396A9D84-A9CA-4575-8D09-C9F054B76AF7
E0138F02-9A15-47BD-8CA5-7D1D0985A1A6 )
profileNames=( "AD Binding" "Energy Saver"
"Finder" "FV2 Redirect"
"Login Window/Security and Privacy" "MDM Profile"
"Restrictions" "SAC SUBCA-01"
"SAC SUBCA-02" "SAC-CA-01"
"Screensaver" "Spotlight"
"Workday Corp" )
# Feeding the list of installed profile IDs via a process
# substitution (<(...)), loop over them and print their
# respective names.
while read -r line ; do
# Find the line in the array of profile IDs and
# print the corresponding name.
if ndx=$(indexOf "$line" "${profileIds[#]}"); then
echo "${profileNames[ndx]} is present"
else
echo "WARNING: Unknown profile: $line" >&2
fi
done < <(profiles -P | awk '/attribute/ {print $4}')
As for why your code didn't loop:
You have an unconditional exit 0 statement in your loop, which means that the loop is always exited after the 1st line.
Due to using <<< to feed the list of profiles, you always get at least 1 line of input, because <<< appends a trailing newline to its input. If the input is empty, you'll get one iteration with an empty line.
As for this message:
; exit;
logout
[Process completed]
It tells me two things:
You're on OSX, and you ran the script from Finder.
The script produced no output (if there had been output, it would have printed between the exit; and logout lines).
When you run a script from Finder, the shell that is used to run the script exits after the script has run - and whether its Terminal window stays open or not depends on your Terminal preferences - in your case, the window stays open, but since the shell has exited, you can't interact with it anymore.
Either way, your particular script should yield the same output, whether it is run from Terminal directly, or via Finder.
I'm trying to fix a bash script by adding in some error catching. I have a file (list.txt) that normally has content like this:
People found by location:
person: john [texas]
more info on john
Sometimes that file gets corrupted, and it only has that first line:
People found by location:
I'm trying to find a method to check that file to see if any data exists on line 2, and I want to include it in my bash script. Is this possible?
Simple and clean:
if test $(sed -n 2p < /path/to/file); then
# line 2 exists and it is not blank
else
# otherwise...
fi
With sed we extract the second line only. The test expression will evaluate to true only if there is a second non-blank line in the file.
I assume that you want to check whether line 2 of a given file contains any data or not.
[ "$(sed -n '2p' inputfile)" != "" ] && echo "Something present on line 2" || echo "Line 2 blank"
This would work even if the inputfile has just one line.
If you simply want to check whether the inputfile has one line or more, you can say:
[ "$(sed -n '$=' z)" == "1" ] && echo "Only one line" || echo "More than one line"
Sounds like you want to check if your file has more than 1 line
if (( $(wc -l < filename) > 1 )); then
echo I have a 2nd line
fi
Another approach which doesn't require external commands is:
if ( IFS=; read && read -r && [[ -n $REPLY ]]; ) < /path/to/file; then
echo true
else
echo false
fi