I am trying to do a simple comparison to check if a line is empty using bash:
line=$(cat test.txt | grep mum )
if [ "$line" -eq "" ]
then
echo "mum is not there"
fi
But it is not working, it says: [: too many arguments
Thanks a lot for your help!
You could also use the $? variable that is set to the return status of the command. So you'd have:
line=$(grep mum test.txt)
if [ $? -eq 1 ]
then
echo "mum is not there"
fi
For the grep command if there are any matches $? is set to 0 (exited cleanly) and if there are no matches $? is 1.
if [ ${line:-null} = null ]; then
echo "line is empty"
fi
or
if [ -z "${line}" ]; then
echo "line is empty"
fi
The classical sh answer that will also work in bash is
if [ x"$line" = x ]
then
echo "empty"
fi
Your problem could also be that you are using '-eq' which is for arithmetic comparison.
grep "mum" file || echo "empty"
if line=$(grep -s -m 1 -e mum file.txt)
then
echo "Found line $line"
else
echo 'Nothing found or error occurred'
fi
I think the clearest solution is using regex:
if [[ "$line" =~ ^$ ]]; then
echo "line empty"
else
echo "line not empty"
fi
If you want to use PHP with this,
$path_to_file='path/to/your/file';
$line = trim(shell_exec("grep 'mum' $path_to_file |wc -l"));
if($line==1){
echo 'mum is not here';
}
else{
echo 'mum is here';
}
Related
I wanna write a script which check variables of this script.
I have tried some, but it isn't working. The idea is:
If on of the parameters is a number, print that it is number
If on of the parameters is a character, print that it is character
If 'man parameter' is executable, print that it is might be a function
Script I have tried:
#!/bin/bash
echo Hello $LOGNAME'!'
test $# -eq 0 && echo 'Try again, no parameters were entered' || echo 'Num of parameters is '$#
re='^[0-9]+$'
for i in $*
do
if ![["$i" =~ $re]];then
echo 'Parameter '$i' is alphabetical'
else
if [["$i" =~ $re]];then
echo 'Parameter '$i' is digital'
else
if [ $i];then
echo $i' might be a function. Try to use man of --help'
fi
fi
fi
done
#!/bin/bash
echo "Hello ${LOGNAME}!"
[ "$#" -eq 0 ] && { echo 'Try again, no parameters were entered'; exit 1; }
echo 'Num of parameters is '$#
re='^[0-9]+$'
for i in "$#"
do
if ! [[ "$i" =~ $re ]];then
echo "Parameter '$i' is alphabetical"
man "$i" > /dev/null 2> /dev/null
if [ "$?" -eq 0 ];then
echo "$i might be a function. Try to use man of --help"
fi
else
echo "Parameter '$i' is digital"
fi
done;
When you write a test you need spaces around your brackets.
You can easily find and fix those bugs if you use shellcheck
I am trying to search content of file 1 into file 2 and if the content is found then store in found.csv file or store in notfound.csv file
Below is my code,
cd /mnt/data/dobiminer/scripts
usage="Usage:sh scriptname.sh 'ToSearchFile' 'MainSearchFile' 'CR' "
Date=`date +%m%d%y%H%M%S`
File=$(<$2)
echo "File Input $2"
echo $File
if [ $# != 3 ]
then
echo $usage
exit 1
else
echo > "$3-Found-$Date.csv"
echo > "$3-NotFound-$Date.csv"
for MasterClip in `cat $1`
do
echo $MasterClip
String=$(echo "$File" | grep -x $MasterClip)
echo $String
if [ -z $String ];
then
echo "NotFound"
echo $MasterClip >> "$3-NotFound-$Date.csv"
else
echo "Found"
echo $MasterClip >> "$3-Found-$Date.csv"
fi
done
fi
My guess is that the below line of code is not working, as whenever I am running the code, the string value is empty only. It is not catching the search value into it.
String=$(echo "$File" | grep -x $MasterClip)
echo $String
I tried multiple things but not sure where I am going wrong.
THanks for helping me out
I want an if/then statement in Bash and I can't seem to get it to work. I would like to say "If the line begins with > character, then do this, else do something else".
I have:
while IFS= read -r line
do
if [[$line == ">"*]]
then
echo $line'first'
else
echo $line'second'
fi
done
But it isn't working.
I also tried to escape the ">" by saying:
if [[$line == ^\>*]]
Which didn't work either.
Both ways I am getting this error:
line 27: [[>blah: command not found
Suggestions?
Spaces are needed inside [[ and ]] as follows:
if [[ "$line" == ">"* ]]; then
echo "found"
else
echo "not found"
fi
This attempt attempt uses a regex:
line="> line"
if [[ $line =~ ^\> ]] ; then
echo "found"
else
echo "not found"
fi
This one uses a glob pattern:
line="> line"
if [[ $line == \>* ]] ; then
echo "found"
else
echo "not found"
fi
Spacing is important.
$ [[ ">test" == ">"* ]]; echo $?
0
$ [[ "test" == ">"* ]]; echo $?
1
if grep -q '>' <<<$line; then
..
else
..
fi
using grep is much better :)
I have a shell script variable
$a = "Hello i am pass"
now i want to search for "pass" in variable $a.
if ["$a" == "pass"]; then
echo "`Success`"
else
echo "`fail`"
fi
Please give me shell script for searching pass keyword to use in above code.
Try with this,
#!/bin/bash
a="Hello i am pass";
if [ `echo $a | grep -c "pass" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
flag=`echo $a|awk '{print match($0,"pass")}'`;
if [ $flag -gt 0 ];then
echo "Success";
else
echo "fail";
fi
Here is a more compact way to write this:
echo "$a" | grep "pass" && echo "Found." || echo "Not found."
You can use braces to put in multiple instructions instead of a simple echo:
echo "$a" | grep "pass" && {echo "Found.";exit 0} || {echo "Not found.";exit 1}
today i used extended choice parameter and using execute shell for the string match.
if someone will find same problem anyone can use this
if [ `echo $test| grep -c "abc" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
Try this
a="Hello I am Pass";
a1="Hello, Passed my First Attempt"
a2="Passed in First Attempt"
if [[ ${a,,} =~ 'pass' ]]; then echo 'Success'; else echo 'First Attempt in Learning'; fi
Short command:
OPC="test"
PROD="This is a test"
[[ ${PROD,,} =~ $OPC ]] && echo -n 1 || echo -n 0
The answer will be "1" when find "OPC" in "PROD".
I'm trying to check if a file exists using bash. This is my code
if [-e file.txt]; then
echo "file exists"
else
echo "file doesn't exist"
fi
But when I run it I get:
./test.sh: line 3: [-e: command not found
What am I doing wrong?
[ is not a special token in Bash; it's just that the word [ is a builtin command (just like echo). So you need a space after it. And, similarly, you need a space before ]:
if [ -e file.txt ] ; then
That said, I recommend [[ ]] instead — it's safer in a few ways (though it still requires the spaces):
if [[ -e file.txt ]] ; then
if [ -e file.txt ]; then
You need spaces.
[ and ] are regular programs.
Woops, turns out I needed a space between [ and -e. Like this:
if [ -e file.txt ]; then
echo "file exists"
else
echo "file doesn't exist"
fi
the '[' and ']' needs to be 'on--their-own', i.e. surrounded by spaces.
if [ -e file.txt ] *emphasized text*; then
echo "file exists"
else
echo "file doesn't exist"
fi