How to use grep in if statement in shell - bash

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.

Related

How to parse stored output in variable, store result and trigger another script based on the result

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

How to make a bash script fail based on grep regex?

I have the following bash script.
The problem I try to solve is really easy. When a commit in Git no starts with gh-1234 ... it should fail.
What is wrong with this bash script?
commit_regex='(gh-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a Github Issue ('gh-1111') or 'Merge'."
if ! grep -q "$commit_regex" <<< "$1"; then
echo "$error_msg" >&2
exit 1
fi
As mentioned in the comments, you need to do grep -E "$commit_regex"
From the man grep page-
-E, --extended-
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
That should solve your problem since it forces grep to expand the variable.
Also another way to achieve the same without grep :
if [[ $git_message=~ $commit_regex ]]
then
echo "$error_msg"
exit 1;
fi
Also : If the commit it "gh-1234" is mandated to be at the start of the message , you should add ^ in your regex.

overwrite a file then append

I have a loop in my script that will append a list of email address's to a file "$CRN". If this script is executed again, it will append to this old list. I want it to overwrite with the new list rather then appending to the old list. I can submit my whole script if needed. I know I could test if "$CRN" exists then remove file, but I'm interested in some other suggestions? Thanks.
for arg in "$#"; do
if ls /students | grep -q "$arg"; then
echo "${arg}#mail.ccsf.edu">>$CRN
((students++))
elif ls /users | grep -q "$arg$"; then
echo "${arg}#ccsf.edu">>$CRN
((faculty++))
fi
Better do this :
CRN="/path/to/file"
:> "$CRN"
for arg; do
if printf '%s\n' /students/* | grep -q "$arg"; then
echo "${arg}#mail.ccsf.edu" >> "$CRN"
((students++))
elif printf '%s\n'/users/* | grep -q "${arg}$"; then
echo "${arg}#ccsf.edu" >> "$CRN"
((faculty++))
fi
done
don't parse ls output ! use bash glob instead. ls is a tool for interactively looking at file information. Its output is formatted for humans and will cause bugs in scripts. Use globs or find instead. Understand why: http://mywiki.wooledge.org/ParsingLs
"Double quote" every expansion, and anything that could contain a special character, eg. "$var", "$#", "${array[#]}", "$(command)". See http://mywiki.wooledge.org/Quotes http://mywiki.wooledge.org/Arguments and http://wiki.bash-hackers.org/syntax/words
take care to false positives like arg=foo and glob : foobar, that will match. You need grep -qw then if you want word boundaries. UP2U

Too Many Arguments If Statement Bash

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
...

Bash script comparing curl results with file

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

Resources