IF statement for LINUX - linux

Modify your program so that if no matching name is found, a message is displayed: "Name 'xyz' not in directory". You could use an 'if' statement to check the value of $? to see if the grep command was successful (remember that '0' indicates success). If the grep is NOT successful, then echo the message (which includes the value of $name).
The original code was:
#!/bin/bash
name=$1
if [ "$name" = "" ]
then echo -n "Enter a name to search for: "
read name
fi
grep -I $name ~uli101/2017a/phonebook
I basically have to insert and IF statement into the program using the instructions above. I've tried many different things such as:
if [ $? = 1 ]
then echo -n "Name 'xyz' not in directory"
fi
but it is not accepting the answer. Pls help me out with this. Any help is appreciated.

This is working for me, try:
#!/bin/bash
name=$1
if [ "$name" = "" ]; then
echo -n "Enter a name to search for: "
read name
fi
grep -I $name phonebook
if [ $? = 1 ]; then
echo "Name '${name}' not in directory"
fi
Or you can change the last IF into:
grep -I $name phonebook && echo -n "Name not in directory"

Try this, it works for me.
#!/bin/bash
#
NAME=$1
if [ -z "${NAME}" ]; then
echo -n "Enter a name to search for: "
read NAME
fi
grep -I $NAME~uli101/2017a/phonebook

Related

programming in linux bash, my program does not respond

im working in linux bash, and i have a problem, im doing a exercise what say this:
"will ask for three strings by keyboard and will test
whether three valid texts have been entered or whether
these strings correspond to directory names.
If so, list what is inside each one
of them and show it. If no three have been entered
strings or any of the three is not a directory,
display a warning indicating the error." okey, in my code, i ask the strings and then, as you can see in each if, I am comparing if they correspond to a directory name or not. However, the bash gives me an error on the line. option_4.sh: line 52: syntactic error: the end of the file was not expected.
#!/bin/bash
echo "Ask for three string from keyboard";
read -p "Give me a string:" string1;
read -p "Give me another string:" string2;
read -p "Give another string:" string3;
if [ -d "$string1" ];
cd "$directorio"
if [ -d "$string1" ];
then
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
if [ -d "$string2" ];
cd "$directorio"
if [ -d "$string2" ];
then
echo ""
echo "The cadena2 is the name of a directory."
echo "His content ($string2):"
ls ./"$string2"
else
echo "ERROR: The cadena2 does not correspond to the name of a directory."
fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ];
then
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
The syntax of an if statement looks like this:
if [ <some test> ]
then
<commands>
else
<commands>
fi
source (edited)
You need to change the first of the two if statements per string to
if [ -d "$string1" ];
then
cd "$directorio";
fi
Or even combine the two checks, because the next check has the same condition:
if [ -d "$string1" ];
then
cd "$directorio" # moved here
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
just choose a snippet to describe it, I am not sure whether you want to use absolute path or relative path. so in your script use pushd and popd will be more convenient.
and if you want to add condition check in your script, you need add then after the keyword if, and at the end you need add keyword fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ]; then
pushd "$directorio"
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
popd
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi

grep filename only in linux bash

I want to find a file. If user input "file.txt", then I need to echo the "file.txt". But if file name not found, just echo "not found". The code i have is for finding a word in a file. But how if I just need to search the file. If I write : result=`grep $filename`, it won't work. What's the format for grep filename?
#!/bin/bash
echo -n "File name : "
read filename
echo -n "Word : "
read word
result=`grep $word $filename`
if [ "$result" != "" ]
then
echo "$result"
else
echo "Not found"
fi
It looks like you are trying to determine if $word is present in $filename. If so:
if ! grep "$word" "$filename"; then
echo not found >&2
fi
The check to determine if the file exists seems redundant, since grep will emit an error message if the file does not exist, but perhaps you want something like:
if test -e "$filename" && ! grep "$word" "$filename"; then
echo "$word" is not found in "$filename" >&2
else
echo "$filename" does not exist >&2
fi
which will not print the redundant "not found" in addition to the error message from grep saying that the file does not exist.
With grep, you can try this way :
#!/bin/bash
OLDIFS="$IFS"
IFS=''
read -p "File name : " filename
IFS=$OLDIFS
grep -l '' "$filename" 2>/dev/null
! [ $? -eq 0 ] && echo "file $filename not found"
bemol : grep do not find a file with a size of 0.
If I read it right...
grep -l "$word" "$filename" 2>&- || echo "not found"
If $word exists in $filename the -l option will output the name of the file.
If not, grep will return an error exit code and the || ("or") will execute the echo.
$: echo foo > bar
$: grep -l foo ???
bar
$: rm bar
$: grep -l foo ??? 2>&- || echo "not found"
not found
The 2>&- closes STDERR so that grep doesn't throw an error message of its own.
Without it -
$: grep -l foo ??? || echo "not found"
grep: ???: No such file or directory
not found
Of course https://mywiki.wooledge.org/BashPitfalls#myprogram_2.3E.26- advises against closing standard streams, so you should probably use 2>/dev/null.
So, the whole script:
#!/bin/bash
read -p "Filename: " filename
read -p "Word: " word
grep -l "$word" "$filename" 2>/dev/null || echo "not found"

What can I do to fix an error inside of case?

My code is:
-cname)
changeName="{2}"
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r $changeName
echo "What would you like to change the name to?"
read -r changeNewName
if ! [ -z "${3}" ]; then
echo "The name has to have no spaces."
exit 1
fi
if [ -f /usr/share/cnote/notes/$changeNewName ]; then
echo "That note name is already used. Please choose a new one."
exit 1
fi
cp "/usr/share/cnote/notes/${changeName}" "/usr/share/cnote/notes/${changeNewName}"
;;
If I remove this part of my case statement it works again but with that part I get that error:
root#minibian:~/cnote# ./cnote -cname
./cnote: line 73: syntax error near unexpected token ;;'
./cnote: line 73: ;;'
Your first if block is not closed. and you should not put the $ symbol ahead of changeName. So the first part must be
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r changeName
echo "What would you like to change the name to?"
read -r changeNewName
fi

How to create a script in linux to display an error?

I have the code below, but whenever I run it and enter an incorrect name it doesn't show the error message, it just goes blank.
#!/bin/bash
name=$1
if [ "$name" = "" ]
then echo -n "Enter a name to search for: "
read name
else
echo "Name '$name' is not in directory"
fi
grep -i $name ~uli101/2015a/phonebook
Read the code: The "error message" is shown when $1 is not empty. You probably wanted something like
if ! grep -i "$name" ~uli101/2015a/phonebook ; then
echo "Name '$name' is not in directory"
fi

Reading username and password from file

I'm looking at a away of reading the username and password of a file and inputing those to either add user or delete user.
EG: I have a file named 'userlist' with the following content with this format:
user1 pass1
user2 pass2
user3 pass3
What I don't understand completely is how to use BASH script to add these accounts.
What I have so far is this:
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
IDK WHAT TO DO HERE.
elif [ $uArg = "d" -o $uArg = "D" ]
then
IDK WHAT TO DO HERE.
fi
fi
Alright, what I don't understand is how to read each word line by line and input the username and password to add a new user or delete an existing user.
The program is ment to read the whole file and add each user with there corrosponding password. If delete is chosen then it deletes each user within the file.
I'm new to BASH so any help would be greatyl appreciated.
awk perfectly feets your needs.
See this example:
$ awk '{print "Hi! my name is " $1 ", and my pass is " $2}' ./userpass.txt
Hi! my name is user1, and my pass is pass1
Hi! my name is user2, and my pass is pass2
Hi! my name is user3, and my pass is pass3
Awk stores usernames in $1 and passwords in $2 (first and second column).
You can use pipelines to execute the strings you get from awk as commands:
$ awk '{print "echo " $1}' ./userpass.txt | /bin/bash
user1
user2
user3
something along the lines of...
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
useradd -m $user
passwd $user <<EOP
$passwd
$passwd
EOP
fi
done < $uFile
elif [ $uArg = "d" -o $uArg = "D" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
userdel $user
fi
done < $uFile
fi
fi
First comments:
learn and get used to basic commands like grep, sed, echo, and generally with file manipulation, awk is a good choice as well if you want to know bash basics, a lot you will encounter is about file manipulation
the code could use more error testing, it's just a basic skeleton
careful about string and variables, quote them whenever possible, spaces in strings can do a lot of bad
Could be something along these lines:
echo "wish to add or delete? a/d"
read uArg
echo "enter username"
read uName
grep "^$uName " password-file
RET=$?
if [ "$uArg" == "a" -o "$uArg" == "A" ]
then
[ $RET -eq 0 ] && echo "User is already in file"
if [ $RET -ne 0 ]
then
echo "enter password"
read uPass
echo "$uName $uPass" >> password-file
fi
elif [ "$uArg" == "d" -o "$uArg" == "D" ]
then
[ $RET -ne 0 ] && echo "User is not file"
if [ $RET -eq 0 ]
then
sed -i "/^$uName /d" password-file
echo "User deleted"
fi
fi

Resources