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

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

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"

accepting an argument and conditions Bash

I´m trying to make a script which takes a single command-line argument. Then it looks on the argument and if its a directory name, it just prints that this directory exists. If its a file name, it prints out the file exists. Otherwise, it tries to create a directory with this name and tests whether it was successful and reports this on the standard output.
my code is:
while read argument; do
if [ $argument -d ]; then
echo "Directory exists"
elif [ $argument -e ]
echo "File exists"
else
mkdir $argument
if [ $argument -d]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
done
Then I run the code ./file_name.sh argument. If I run the code like this, I get an error on line 8, which is just "else". While is probably not necessary here, it was the first option how to accept an argument from the command line that came to my mind.
As you mentioned, you need single command line argument, So no need to loop
#!/bin/bash
if [[ -z "$1" ]]; then
echo "Help : You have to pass one argument"
exit 0
fi
if [[ -d "$1" ]]; then
echo "Directory exists"
elif [[ -f "$1" ]]; then
echo "File exists"
else
mkdir "$1"
if [ -d "$1" ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
if [ -d $1 ]; then
echo "Directory exists"
elif [ -e $1 ]; then
echo "File exists"
else
mkdir $1
if [ -d $1 ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
I made up this solution thanks to the links provided, thank you.

Linux Shell Scripting: Script Check

I am new to Linux bash scripting and I can't seem to find what I'm doing wrong. Here's my code. Entering number 2 and 3, after the prompt that I ask the user my code stops it doesn't continue to the IF ELSE statements. Thank you to those who will help!
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -e "Enter Choice:"
read answer
case "$answer" in
1) ./move_empty
exit 55 ;;
2) echo "Enter a filename"
read filename
if [ -f $filename ];
then ./file_size
fi
;;
3) echo "Enter first file:"
read filename
echo "Enter second file:"
read filename2
if [ ! -f "$filename" ];
then
echo "Supplied file name" $filename "does not exist";
if [ $filename" -nt $filename" ]; then
echo $filename "is newer"
exit 1fi
fi ;;
5) exit ;;
esac
done
If you have completed the check at ShellCheck.net, then you should have received:
$ shellcheck myscript
No issues detected!
If you didn't work it down to that point, you are not done. You have multiple quoting problems in your script and you compare $filename -nt $filename (which is always false). Small "attention to detail" issues that make a big difference. ShellCheck.net does a thorough job, but will not find logic issues, those are left to you. The cleanup of your quoting would look similar to:
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read -r answer
case "$answer" in
1) ./move_empty
exit 55
;;
2) echo -n "Enter a filename: "
read -r filename
if [ -f "$filename" ]
then
./file_size
fi
;;
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename2" ]
then
echo "Supplied file name $filename does not exist";
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
exit 1
fi
fi
;;
5) exit
;;
esac
done
(note: you do not need echo -e as there are no backslash escaped characters to handle in your prompt, likely you intended -n to prevent the addition of a newline at the end of the prompt)
(also note: the use of clear, while fine for some terminals, will cause problems with others. Just be aware of the potential issue.)
If your then is on the same line with your conditional expression, e.g. if [ "$filename" -nt "$filename2" ]; then then a ';' is needed after the closing ']' to indicate a newline, otherwise, there is no need for a ';'.
Logic Problems
As discussed, the logic problems are not caught by ShellCheck and you must work though the code. It looks like you intended something like the following:
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename" ] || [ ! -f "$filename2" ]
then
echo "Supplied file '$filename' or '$filename2' does not exist";
exit 1
fi
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
else
echo "$filename2 is newer"
fi
;;
You just have to take it line by line...
Look things over and let me know if you have further questions.

IF statement for 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

Resources