I'm stuck on a particular problem and need some guidance. I have a file with a name and a phone number in it (teledir.txt). I need to do a $# in a separate script to take a positional parameter and check to see if it is in the file. To quote the question:
If one argument is supplied, check to see if it starts with a digit, then search for it in the teledir.txt file. If it doesn't exist, echo "Doesn't Exist".
If argument starts with a letter, search for it in the file, If it doesn't exist, echo "Name does not exist"
If two arguments are supplied, set the search_pattern to be �name: number�, and search it in the file.
[*]If it is not found, echo �Adding entry�, and append to the file/variable.
[*]Otherwise, echo that �Entry exists�
I am only two weeks into learning the command line prompt. This is the code I have gotten so far but I am stuck on where to go from here. Not sure if this will even work. Any pointers please!!!
#!/bin/bash
name=$1
number=$2
if [ $# -eq 0 ]; then
echo "Two arguments are expected in the form of ./scriptname [name number]"
fi
if [ $# -eq 2 ]; then
grep -qiw "$name\|$number" teledir.txt
echo "$name or $number exists"
fi
Thanks for all of the help. I truly appreciate it. I am running into one more issue. When I enter a positional parameter of a name that I know is in the file. It still outputs as name does not exist. This is the code I have so far:
#!/bin/bash
name=$1
number=$2
if [ $# -eq 0 ]; then
echo "Two arguments are expected in the form of ./scriptname [name number]"
fi
if [ $# -eq 1 ]; then
if grep -qiw "$name | $number" teledir.txt; then
echo "$name exists"
else
echo "Does Not Exist"
fi
fi
and with this design, 2 parameters are needed and not equal to an empty string
Otherwise
if [ $# -eq 1 ]; then
if grep -qiw "$name" teledir.txt; then
echo exists
else
echo no exists
fi
fi
--- Post updated at 21:16 ---
#!/bin/bash
if [ $# -lt 2 ]; then
echo 2 parameters required
exit
else
if [ -z "$1" -o -z "$2" ]; then
echo The parameter cannot be an empty string
exit
fi
if [ -n "${1//[[:alpha:]]}" -o -n "${2//[0-9-]}" ]; then
echo Invalid parameters
exit
fi
fi
if grep -qiw "$1\|$2" teledir.txt; then
echo "$1 or $2 exists"
else
echo "Does Not Exist"
fi
The issue I'm running into is that if I enter a number as a parameter, it functions correctly, but when I enter a name, nothing happens.
#!/bin/bash
name=$1
number=$2
if [ $# -eq 0 ]; then
echo "Two arguments are expected in the form of ./scriptname [name number]"
fi
case $name in
$1)
if [[ $1 == [[:alpha]]* ]]; then
if grep -qiw "$name" teledir.txt; then
echo "$name exists"
else
echo "Name does NOT exist"
fi
elif [[ $1 == [0-9]* ]]; then
if grep -qiw "$name" teledir.txt; then
echo "$name exists"
else
echo "Number does NOT exist"
fi
fi
esac
This is how I would approach the problem shown in post #1, making use of bash features like "parameter expansion / Remove matching suffix pattern" and test ing empty "conditional expressions". It could seriously benefit from some polishing, e.g. combining grep runs, variable quoting, error handling, and upfront argument compliance checks. Try
name=$1
number=$2
FN=teledir.txt
case $# in
1) if [ ${name%%[[:digit:]]*} ]
then if ! grep -iw "$name:" $FN
then echo "Name doesn't exist."
fi
elif [ ${name%%[[:alpha:]]*} ]
then if ! grep -iw "$name" $FN
then echo "$name doesn't exist."
fi
fi ;;
2) grep -qi "$name: $number" $FN &&
echo "$name: $number exist" ||
echo "$name: $number" >> $FN
;;
*) echo "Two arguments are expected in the form of ./scriptname [name] [number]";;
esac