how to use grep command in .log file if it is not empty [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a log file. When the log file is not empty then i should grep command for policy and work flow. i have tried this code, but how can I initialize the log file and use this command?
cmd=`grep -c "POLICY" file`
if [[ $(grep -c "POLICY" file) -gt 0 ]]
then
echo "POLICY are present"
else
echo "POLICY not present"
fi

You can use grep -q. It will return 0 (success) if any match is found, otherwise (even if the files does not exist) it will return 1 (failure).
if grep -q POLICY file; then
echo "POLICY are present"
else
echo "POLICY not present"
fi

Related

How to debug the argument problem in Linux at the bash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The question is contents of the variable $TEST are equal to the string “success, or the
number 5 or the contents of the variable $RESULT
The code that I write is [ $TEST=”success” -o $TEST -eq ”5” -o $TEST = “$RESULT” ], but when in the linux there saying that bash: [ too many argument. Can someone help me
You will need || for this, to represent or and so:
if [[ "$TEST"==”success” || "$TEST"==”5” || "$TEST" == “$RESULT” ]]
then
echo "match"
else
echo "No match"
fi

File exists in shell script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to check file exists in a folder. I have a test.sh and test.json files in a folder. In the test.sh file I have scripts for checking whether the test.json file exists. The below code i have used for checking the existance. I have used the ls command and it shows the file exists but the code I have used is not finding the file and it prints file not exists.
can you pls help me whether I have missed anything here.
ls
File=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
echo "file exists"
else
echo "file not exists"
fi
bash is case sensitive, you need to use same case in declaration and usage.
e.g.
FILE=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
...

Using single quotes still giving "event not found" in bash [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am using single quotes but shell is interpreting it as a history expansion.
#!/bin/bash
if [ -z `$1` ] || [ -z `$2` ]
then
echo `Wrong ID` >&2
exit 1
fi
You should use double quotes " " instead of backticks ` `.
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]
then
echo "Wrong ID" >&2
exit 1
fi

Show file contents after searching word Done [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to display the file contents after searching for a word. If the word is found, display the file.
My code is below:
GNU nano 2.2.6 File: work
#!/bin/bash
while read -p "Welcome what would you like to do (S) to search or (Q) to quit " option
do
case $option in
"S") echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep -q $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
cat $file
else
echo "$word NOT found in $file"
fi
;;
"Q") echo "Goodbye!"
exit ;;
*) echo "invalid option" ;;
esac
done
Replace
echo $file
with
cat $file
I believe you are looking for command cat $file. Stick it inside of your if block.
I need to load up what a file says with out loading up the file.
There is no way to access the contents of the file without accessing the file.
grep -l word file | xargs -r cat
shows file content if word is found. This also shows name of file
grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"

Check if a file exists in a linux script [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 4 years ago.
i made a linux script which receives as first argument a path to a directory. I don't know the path. And i want to check if "file.txt" exists at that certain path . For example :
if [ -e $1/file.txt ];then
echo HAHA
fi
if [[ -e "$1/file.txt" ]]; then
echo "It exists"
fi

Resources