I'm trying to use an if statement with grep in order to check if a string exists in some files. Now, the grep statement work by itself, but when I run it as part of the if statement the output is:
line 6: [: too many arguments
My Code:
#!/bin/bash
if [ $(grep -c "OutOfMemory" /my/path/to/domains/*/*/subdomains/*/logs/*.*) -ne 0 ];
then
echo "String found"
else
echo "String not found"
fi
If tried using a shorter path but it didn't help.
Any suggestion will help.
Thank you,
The problem is that your grep -c does not produce the correct output.
e.g, you could get multiple files:
$ grep -c "OutOfMemory" /my/path/to/domains/*/*/subdomains/*/logs/*.*
/my/path/to/domains/a/b/subdomains/c/logs/my.log:1
/my/path/to/domains/a/b/subdomains/c/logs/another.log:2
Your if statement cannot handle the multiple lines returned by grep, so it fails with too many arguments.
If you want to see if there is any file containing the string "OutOfMemory", do this instead:
if grep -q "OutOfMemory" /my/path/to/domains/*/*/subdomains/*/logs/*.*
then
...
Related
I have this shall script, basically I need to print all <sst> result only when <pr> is found.
Probably I have some syntax error so when I run the script I receive a message "Display all possibilities" basically the grep does not work.
Could you please help me out to understand what is the problem here?
declare -a arr=(
"123"
"345"
)
for i in "${arr[#]}"
do
echo "$i"
if [grep -q "<pr>$i</pr>" ./archiv]
then
grep -r "<sst>" ./archiv
fi
done
There is very likely no command named [grep. Drop the [
if grep -q "<pr>$i</pr>" ./archiv; then ...
[ is not and has never been a part of the shell grammar. It is a command, just like echo or test or grep. The value returned by that command is used to determine whether or not to execute the clause of the if statement.
I'm writing a script to run another script with parameters and store the output into a variable.
The output has multiple lines but I only need one single line containing one of four specific strings and do something based on which string was found.
I want to store the output from the command into $OUTPUT but unable to parse and get the required lines to run an additional script.
OUTPUT="$(script.php $HOST $PARAMETER)"
Tried a simple if statement below but i'm already failing with:
RESULT=$(grep "TEST" $OUTPUT)
if [ $? -eq 0 ]; then
printf '%s\n' "$RESULT"
else
printf '%s\n' "No Match"
fi
This is what I'm getting, where '-p' is the $PARAMETER when executing the script:
grep: invalid option -- 'p'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Where I'm getting the correct output with:
printf '%s\n' "$OUTPUT"
The problem is, that value of $OUTPUT are interpreted as shell command line parameters in this line:
RESULT=$(grep "TEST" $OUTPUT)
try:
RESULT=$(echo "$OUTPUT" | grep "TEST")
The simplest way to avoid this error and do not thread strings like -p as parameters is to rewrite your grep line like this:
RESULT=$(grep -- "TEST" $OUTPUT)
Double dash tell grep this is the end of parameter and all the rest is threaded as data
I have a list of names in a list (say name.txt) which has the set of name lists one by one.
name.txt
babu
praveen
kamal
sneha
This name will be passed as run time argument $1 in my bash script.
Now I have to do a match to check if the inputted name is in my list or not.
If it's not there then I will print saying invalid name and exit. Can you help me with this?
I have tried this with
if [[ "$1" != "babu" || "$1" != "praveen" || "$1" != "kamal" ... ]]; then
exit
fi
but this doesn't look good professionally.
Is there any other simple and decent way to achieve this?
I guess you could use grep:
if ! grep -qFxf name.txt <<<"$1"; then
exit
fi
-q is "quiet mode" - the exit status indicates a match
-F matches fixed strings, rather than regular expressions
-x is a full line match, so names that are substrings won't match
-f means that the list of patterns are contained within a file
The string contained within the first argument is passed to grep using a here string. This is a bash feature, so if you're using another shell, you could go with something like if ! echo "$1" | grep -qFxf name.txt instead (but the bash way is preferred as it saves invoking a subprocess).
If you want to ensure that any error output from grep is suppressed, you can add 2>/dev/null to the command, as suggested in the comments. This prevents any messages sent to standard error from being displayed.
I have to look if a file has the work England. I have used grep
This is what I have so far
#!/bin/bash
cd ~/Desktop/htmlFiles/
echo "Starting"
if `grep -inE "england" maps.html` ; then
echo "yaaaay"
fi
But the output is:
Starting
./trial.sh: line 4: 22:: command not found
Why doesn't it return yaaaay ?
#!/bin/bash
cd ~/Desktop/htmlFiles/
echo "Starting"
if grep -qinE "england" maps.html ; then
echo "yaaaay"
fi
Gave me the required output. Is that a good practise?
Backtick substitution will return the output of the command. In this case, you are outputting the line number with grep's output, following by the matching line. Hence, there appears to be something on line 22, but the shell tries to execute the command 22:, which then fails.
The variant you have posted as a followup answer will work, as grep returns 0 on a successful match, but 1 if the match fails.
So I am writing a script that will curl a site I've written that will return a string. I am using it to compare a value that is saved within a file. I am having trouble getting my conditional statement to return true for matching values.
Here is a snippet of my bash code
var1=$(curl -s -w %{http_code}\\n www.somesite.com)
var2=$(cat somefile)
if [[ "$var1" = "$var2" ]]; then
echo "TRUE"
else
echo "FALSE"
fi
I have manually looked at both the strings and they seem to be identical. I've done wc with all applicable options with it. I've copy and pasted both the values into Notepad++ and did a find for the expected string and it said that both values matched the find command.
Obviously, if I manually put the values in the condition it returns true, so I know its not my conditional statement.
My guess is there is some type of hidden character on the end of curl...('\r' or '\n' or something that I am unaware)...or maybe on the end of the file that I am unaware of. Is this a known issue when trying to compare curl output and file content?
This may be a dumb question, but for the life of me I cannot seem to get these strings to match doing it dynamically instead of hardcoding the values in the condition.
Any suggestions would be great. Thanks
$ diff -q -b <(echo $'abc\ndef') <(echo $'abc\r\ndef') > /dev/null ; echo $?
0
$ diff -q -b <(echo $'abc\ndef') <(echo $'abc\r\nde') > /dev/null ; echo $?
1