How to optimize this 2 lines of bash code - linux

I want to optimize a bash code using 1 line instead of two
this is the lines i want to optimize:
grep -E "$name" /etc/passwd
if [ $? -eq 0 ]
#...
so the if will test the exit of the last command (grep), i want to merge "grep -E "$name" /etc/passwd" in the if statement to have something like:
if [ ##### -eq 0 ]
thanks for your help :)

Without if:
grep "name" file && do_something

You can use the exit code of the command directly in the if statement:
if grep ....
then
echo "found"
else
echo "not found"
fi
If you want to silence the output of the grep command you can add the -q option, and if you want to quit after the first match (saves time on large files) you can use -m 1.

Related

LINUX script bash [duplicate]

I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What's wrong in my code?
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
You don't need [[ ]] here. Just run the command directly. Add -q option when you don't need the string displayed when it was found.
The grep command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.
$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0
You can specify commands as an condition of if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
As you can see you run here the programs directly. No additional [] or [[]].
In case if you want to check whether file does not contain a specific string, you can do it as follows.
if ! grep -q SomeString "$File"; then
Some Actions # SomeString was not found
fi
In addition to other answers, which told you how to do what you wanted, I try to explain what was wrong (which is what you wanted.
In Bash, if is to be followed with a command. If the exit code of this command is equal to 0, then the then part is executed, else the else part if any is executed.
You can do that with any command as explained in other answers: if /bin/true; then ...; fi
[[ is an internal bash command dedicated to some tests, like file existence, variable comparisons. Similarly [ is an external command (it is located typically in /usr/bin/[) that performs roughly the same tests but needs ] as a final argument, which is why ] must be padded with a space on the left, which is not the case with ]].
Here you needn't [[ nor [.
Another thing is the way you quote things. In bash, there is only one case where pairs of quotes do nest, it is "$(command "argument")". But in 'grep 'SomeString' $File' you have only one word, because 'grep ' is a quoted unit, which is concatenated with SomeString and then again concatenated with ' $File'. The variable $File is not even replaced with its value because of the use of single quotes. The proper way to do that is grep 'SomeString' "$File".
Shortest (correct) version:
grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"
can be also written as
grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"
but you dont need to explicitly test it in this case, so the same with:
grep -q "something" file && echo "yes" || echo "no"
##To check for a particular string in a file
cd PATH_TO_YOUR_DIRECTORY #Changing directory to your working directory
File=YOUR_FILENAME
if grep -q STRING_YOU_ARE_CHECKING_FOR "$File"; ##note the space after the string you are searching for
then
echo "Hooray!!It's available"
else
echo "Oops!!Not available"
fi
grep -q [PATTERN] [FILE] && echo $?
The exit status is 0 (true) if the pattern was found; otherwise blankstring.
if grep -q [string] [filename]
then
[whatever action]
fi
Example
if grep -q 'my cat is in a tree' /tmp/cat.txt
then
mkdir cat
fi
In case you want to checkif the string matches the whole line and if it is a fixed string, You can do it this way
grep -Fxq [String] [filePath]
example
searchString="Hello World"
file="./test.log"
if grep -Fxq "$searchString" $file
then
echo "String found in $file"
else
echo "String not found in $file"
fi
From the man file:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of
which is to be matched.
(-F is specified by POSIX.)
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by
POSIX.)
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero
status if any match is
found, even if an error was detected. Also see the -s or --no-messages
option. (-q is specified by
POSIX.)
Try this:
if [[ $(grep "SomeString" $File) ]] ; then
echo "Found"
else
echo "Not Found"
fi
I done this, seems to work fine
if grep $SearchTerm $FileToSearch; then
echo "$SearchTerm found OK"
else
echo "$SearchTerm not found"
fi
grep -q "something" file
[[ !? -eq 0 ]] && echo "yes" || echo "no"

How can I use GREP to decide if every line in a file matches my pattern?

I am trying to check if every line in a file matches my pattern (4 characters followed by 4 digits). I tried using GREP with -x -P -v -q options so it returns 1 if my file doesn't match the requirements. I expect it to return nothing in case the file is correct, but it returns nothing even if the file has an error.
$4 is my input file.
My code is:
if [ -f $4 ] && [ `grep -q -P -x -v [[a-z]x{4}/[\dx{4}] $4` ]
then
echo "error"
exit 1
fi
input example:
bmkj2132
ahgc3478
(no uppercase)
Don't check the output of grep, check its exit code.
if [ -f "$4" ] && grep -q ...
Note the double quotes around $4 - otherwise a file name containing whitespace will break the script.
Also, single quote the regex. Square brackets are special in bash and you don't want the regex to suddenly change (expand) when a random filename exists.
Also note that x doesn't mean "times" (under -e, -E, -P neither). The quantifier just follows the quantified with no operator in between:
echo abcd1234 | grep -xP '[a-z]{4}\d{4}'
So, the full condition should be
if [ -f "$4" ] && grep -qvxP '[a-z]{4}\d{4}' "$4" ; then
echo Error >&2
exit 1
fi
Are you sure you want to continue if the file doesn't exist? If not, change the condition to
if ! [ -f "$4" ] || grep ...
BTW, you don't really need the PCRE expression here. If you replace \d by [0-9] or [[:digit:]], you can switch to -E (extended regular expression).
The point about using the exit code of grep is that it does not match the required condition.
Try the following:
if [ -f "$4" ] ; then
wronglines=$(grep -v pattern "$4")
if [ "$wronglines" = "" ] ; then
echo "all is well"
else
echo "a wrong line in the file"
fi
else
echo "cannot even find the file"
fi
This answer describes how we can use grep to search for any line not matching a particular regex pattern. We can modify this to provide a one-liner.
grep -Evq "[1-2]" file.txt && echo "error" && exit 1 || true
On the following file.txt, the error message and exit code will be triggered:
1
2
3
Without || true, this will always have an false return code. Additionally, this only works for the specified use case; modifying exit 1 to something like true will break the one-liner.
The regex pattern included within this example should be modified to the desired pattern.

Bash scripting using an exit status in an if statement

May be a novice question but anyways in my intro to linux/unix class were touching on bash scripting and in one of the problems I got the it tasked me with making a script so if the user searched to a name in a file that wasn't there it would output a messaged saying 'your_input is not in the directory'
It says to use if statements and the exit status $?.
So far I got the input portion but I'm not sure how to properly use the $? in a if statement if its possible.
#!/bin/bash
name=$1
if [ "$name" = "" ]
then echo -n "Enter a name to search for: "
read name
fi
grep -i $name ~uli101/2014c/phonebook
if [ "$?" < "0" ]
then echo "error"
fi
I get the error:
./phone4: line 14: 0: No such file or directory
My question is: How can I use the $? with and if statement, and If I can't, can you explain me how to use the $? in this problem?
Note: I did use echo $? to see how $? gave a 0 if grep worked and a 1 if it didn't.
There's two bugs in it. The one you already see is that in the [] expression, the < is interpreted not as "less than" but as stream redirection operator. The reason is that [ is just another program (an alias for test), so [ "$?" < "0" ] is similar to cat < filename. The other error is that you don't want to check for "less than" but for "not equal". In sum:
if [ "$?" < "0" ]
should be
if [ "$?" -ne "0" ]
Or you could write
if ! grep "$name" ~uli101/2014c/phonebook
...because if interprets a return code of zero as true and everything else as false.
never mind one of my friends pushed me in the right direction:
all I had to do is:
if [ "$?" = "1" ]
then echo "error"
fi
pretty much I was over thinking it, I just needed to say if $? = 1 then error, because in the readings it said $? can be greater then 1 I was trying to compensate for that.
It should be
if [ "$?" -gt 0 ]
The symbol '<' is a redirection operator, and it's not a Python or C - everything in a shell script is a command, including the text after 'if', and you are executing a command named '[' here, you may find it at the location /usr/bin/[, and this command uses -gt and -lt parameters to compare numbers, instead of '>' and '<', which are special shell operators.
You can rewrite this code like this:
if grep -i "$name" ~uli101/2014c/phonebook
then true # 'true' is also a command, which does nothing and returns success
else echo "Error"
fi
or even like this, using '||' operator, which will execute following command only if previous command returned an error:
grep -i "$name" ~uli101/2014c/phonebook || echo "Error"
The "$?" doesn't need quotes, as it is a number really. If you want better script, check on existance of the Phonebook file, and exit before asking the Name input if the file is missing. Also, if you reply nothing (enter only) on the READ command, you may need to do something.
#!/bin/bash
name=$1
phonebook=~/phonebook
if [ "$name" = "" ]
then
echo -n "Enter a name to search for: "
read name
fi
grep -i "$name" $phonebook
if [ $? -gt 0 ]
then
echo "error, no \"$name\" in $phonebook"
fi

bash: grep wildcard start and end with

i have a bash script like this:
TABLE_TO_IGNORE=$(mysql -u $DBUSER -p$DBPASS -h $DBHOST -N <<< "show tables from $DBNAME" | grep "^$i" | xargs);
currently i only able to grep the text starting with. How to write the code that to determine the text ending with?
let say 1:
my $i is:
test1_*
tb2_*
tb3_*
with the * at the back part, it will grep as text starting with those value
let say 2:
my $i is:
*_sometext1
*sometext2
with the * at the front, it will grep as text ending with those value.
i know this:
grep '^sometext1' files = 'sometext1' at the start of a line
grep 'sometext2$' files = 'sometext2' at the end of a line
question is: how do i write the if else to my bash code identify the * is in front or back?
Note: You can ignore my bash code, i just need the if else condition to determine the "*" is in front or at the back of the string.
Any help would be great.
Thanks
You can try this code.
#!/bin/bash
stringToTest="Hello World!*"
echo $stringToTest | grep "^\*.*" > /dev/null
if [ $? -eq 0 ]; then
echo "Asterisk is at the front"
fi
echo $stringToTest | grep "^.*\*$" > /dev/null
if [ $? -eq 0 ]; then
echo "Asterisk is at the back"
fi
As shown in this code, I made use of exit code ($?) to determine whether the regular expression matches the string. As shown in man grep:
Normally, exit status is 0 if selected lines are found and 1
otherwise.
Hope this helps.

Check whether a certain file type/extension exists in directory [duplicate]

This question already has answers here:
Test whether a glob has any matches in Bash
(22 answers)
Closed 1 year ago.
How would you go about telling whether files of a specific extension are present in a directory, with bash?
Something like
if [ -e *.flac ]; then
echo true;
fi
#!/bin/bash
count=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $count != 0 ]
then
echo true
fi
#/bin/bash
myarray=(`find ./ -maxdepth 1 -name "*.py"`)
if [ ${#myarray[#]} -gt 0 ]; then
echo true
else
echo false
fi
This uses ls(1), if no flac files exist, ls reports error and the script exits; othewise the script continues and the files may be be processed
#! /bin/sh
ls *.flac >/dev/null || exit
## Do something with flac files here
shopt -s nullglob
if [[ -n $(echo *.flac) ]] # or [ -n "$(echo *.flac)" ]
then
echo true
fi
#!/bin/bash
files=$(ls /home/somedir/*.flac 2> /dev/null | wc -l)
if [ "$files" != "0" ]
then
echo "Some files exists."
else
echo "No files with that extension."
fi
You need to be carful which flag you throw into your if statement, and how it relates to the outcome you want.
If you want to check for only regular files and not other types of file system entries then you'll want to change your code skeleton to:
if [ -f file ]; then
echo true;
fi
The use of the -f restricts the if to regular files, whereas -e is more expansive and will match all types of filesystem entries. There are of course other options like -d for directories, etc. See http://tldp.org/LDP/abs/html/fto.html for a good listing.
As pointed out by #msw, test (i.e. [) will choke if you try and feed it more than one argument. This might happen in your case if the glob for *.flac returned more than one file. In that case try wrapping your if test in a loop like:
for file in ./*.pdf
do
if [ -f "${file}" ]; then
echo 'true';
break
fi
done
This way you break on the first instance of the file extension you want and can keep on going with the rest of the script.
The top solution (if [ -e *.flac ];) did not work for me, giving: [: too many arguments
if ls *.flac >/dev/null 2>&1; then it will work.
You can use -f to check whether files of a specific type exist:
#!/bin/bash
if [ -f *.flac ] ; then
echo true
fi
bash only:
any_with_ext () (
ext="$1"
any=false
shopt -s nullglob
for f in *."$ext"; do
any=true
break
done
echo $any
)
if $( any_with_ext flac ); then
echo "have some flac"
else
echo "dir is flac-free"
fi
I use parentheses instead of braces to ensure a subshell is used (don't want to clobber your current nullglob setting).
shopt -s nullglob
set -- $(echo *.ext)
if [ "${#}" -gt 0 ];then
echo "got file"
fi
For completion, with zsh:
if [[ -n *.flac(#qN) ]]; then
echo true
fi
This is listed at the end of the Conditional Expressions section in the zsh manual. Since [[ disables filename globbing, we need to force filename generation using (#q) at the end of the globbing string, then the N flag (NULL_GLOB option) to force the generated string to be empty in case there’s no match.
Here is a solution using no external commands (i.e. no ls), but a shell function instead. Tested in bash:
shopt -s nullglob
function have_any() {
[ $# -gt 0 ]
}
if have_any ./*.flac; then
echo true
fi
The function have_any uses $# to count its arguments, and [ $# -gt 0 ] then tests whether there is at least one argument. The use of ./*.flac instead of just *.flac in the call to have_any is to avoid problems caused by files with names like --help.
Here's a fairly simple solution:
if [ "$(ls -A | grep -i \\.flac\$)" ]; then echo true; fi
As you can see, this is only one line of code, but it works well enough. It should work with both bash, and a posix-compliant shell like dash. It's also case-insensitive, and doesn't care what type of files (regular, symlink, directory, etc.) are present, which could be useful if you have some symlinks, or something.
I tried this:
if [ -f *.html ]; then
echo "html files exist"
else
echo "html files dont exist"
fi
I used this piece of code without any problem for other files, but for html files I received an error:
[: too many arguments
I then tried #JeremyWeir's count solution, which worked for me:
count=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $count != 0 ]
then
echo true
fi
Keep in mind you'll have to reset the count if you're doing this in a loop:
count=$((0))
This should work in any borne-like shell out there:
if [ "$(find . -maxdepth 1 -type f | grep -i '.*\.flac$')" ]; then
echo true
fi
This also works with the GNU find, but IDK if this is compatible with other implementations of find:
if [ "$(find . -maxdepth 1 -type f -iname \*.flac)" ]; then
echo true
fi

Resources