rather noobie question here.. i think. but i cant get this script to work. It is going to be included inside a script that i asked about here a few days ago (BASH output column formatting). basically i want to be able to scrape a site for a portion of text and return a ONLINE/OFFLINE answer. I apologize for poor formatting and weird variable names. Thanks for taking a look and helping me out!
#!/bin/bash
printf "" > /Users/USER12/Desktop/domainQueryString_output.txt
domainCurlRequest="curl https://www.google.com/?gws_rd=ssl"
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
echo $($domainCurlRequest) >> /Users/USER12/Desktop/domainQueryString_output.txt
if [ $ifStatementConditional -eq 2 ] ;
then second_check="online"
else second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
i keep getting the following error when trying to run this script
/Users/USER12/Desktop/domain_status8working.sh: line 6: [: too many arguments
i tried to rewrite another way but got same errors so my logic or syntax or something is off.
Thanks again for taking a look and helping me out!!!
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
This is a string assignment. You probably want backticks, or the $() construct. Otherwise, $ifStatementConditional will never equal 2
if [ $ifStatementConditional -eq 2 ] ;
This is expanded as:
if [ grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l -eq 2 ] ;
Which explains your error.
I think you meant that:
#!/bin/bash
curl "https://www.google.com/?gws_rd=ssl" > /Users/USER12/Desktop/domainQueryString_output.txt
ifStatementConditional=$("grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l")
if [ $ifStatementConditional -eq 2 ] ; then
second_check="online"
else
second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
No need to do printf "" > somefile.txt when you do a curl after, and you append to that file
$() is to capture subshell output. That what was missing here.
Related
I think I'm missing something with variable types...why the following script, that is supposed to read a number of lines and compare this number to 1 always enters the if even though it returns exactly 1?
status() {
lines=`ps aux | grep myprocess | wc -l` #returns 1
if [ $lines -gt 1 ]; then
echo "Process is up"
else
echo "Process is down"
fi
}
Solved by using melpomene's suggestion to use egrep instead of grep.
Strangely enough that command had several lines printed when ran as init script.
I believe my error is in do if ($i -gt 100) area but I havent been able to figure it out.
My input is:
for i in `ps | cut -d ' ' -f1`; do if ($i -gt 100); then echo $i; fi; done
My output is this where the process IDs have been taken as commands.
bash: 13968: command not found
bash: 21732: command not found
bash: 21733: command not found
bash: 21734: command not found
How can I fix this and what is the relevant man page that I should read up on? Thank you.
if ($i -gt 100)
should be changed to
if [ $i -gt 100 ]
Note that there is a space before and after [], this is neccessary, otherwise you will get a syntax error (its because [ is a link to test in /usr/bin).
The relevant manapge would be man test, as [ is test.
Also, but this has nothing to do with the question, I recommend switchting from
`command`
to
$(command)
in bash.
It's not clear what your script is trying to do (the posted answers produce no output on my system) but if you want to print all PIDs that are greater than 100, here's how you'd do that:
$ ps | awk '$1 > 100{print $1}'
PID
314024
217880
230804
217084
263048
260788
218016
313464
201556
200732
just add spaces after and before brackets and the expression
for i in `ps | cut -d ' ' -f1`; do if [ $i -gt 100 ]; then echo $i; fi; done
I have a command similar to this:
LIST=$(git log $LAST_REVISION..$HEAD --format="%s" | egrep -o "[A-Z]-[0-9]{1,4}" | sort -u)
Now, I need to do something if $LIST returned zero or more lines. Here's what I've tried:
if [ ! $($LIST | wc -l) -eq 0 ]; then
echo ">0 lines returned"
else
echo "0 lines returned"
fi
But it throws me an error. What's the correct syntax of doing this (with some details on the syntax used, if possible)?
To check whether a variable is empty, use test -z, which can be written several ways:
test -z "$LIST"
[ -z "$LIST" ]
with bash (or many other "modern" shells):
[[ -z $LIST ]]
I prefer the last one, as long as you're using bash.
Note that what you are doing: $($LIST | ...) is to execute $LIST as a command. That is almost certain to create an error, and guaranteed to do so if $LIST is empty.
all,
I am checking the error info in a file in the last line, I would like to have a "yes" result if there is an "Error". My shell script is like below:
[ $(tail -1 error.log | grep -E "Error") ] && echo "yes"
then I got the error like above in the title:
-bash: [: #: binary operator expected
The error message in the last line is like below:
[aaa,bbb,ccc, Error.ddd # ]
I think that is because of the the Error message, which has [ # ] format content caused this error. But I do not know how to fix it. Is there any one knows how to process this [ # ] problem. Thanks very much
#csiu, thanks very much for your quick reply.
The trick here is to use double "[" as below:
[[ $(tail -1 error.log | grep -E "Error") ]] && echo "yes"
well since my comment works, might as well post it in the answer section ;)
Use double "[["
[[ $(tail -1 error.log | grep -E "Error") ]] && echo "yes"
Related posts:
How to use double or single bracket, parentheses, curly braces
Meaning of double square brackets in bash
Additionally to #csiu's answer, don't need the test command at all. You can operate based on grep's exit status:
tail -1 error.log | grep -qE "Error" && echo yes
Use -q to silence the output from grep. It's also more efficient because grep will exit immediately once the pattern is found.
Since we only have one line of input, we don't even need grep:
[[ $(tail -1 error.log) == *Error* ]] && echo yes
I'm working with bash and I'm trying to do something like this:
A=1
while [ $A=1 ]; do
read line
echo $line | grep test >/dev/null
A=$?
echo $A
done
This script never ends even when the grep finishes successfully and A is set to 0. What am I missing here? Below is a sample of the output.
$ ./test.sh
asdf
1
test
0
hm...
1
You need to use the correct comparison operator. Bash has different operators for integer and string comparison.
In addition, you need the correct spacing in the comparison expression.
You need
while [ $A -eq 1 ]; do
See here for more
I find Bash's syntax pretty awful - have you tried something like:
while [ $A -eq 1 ] ... ?
It may be trying to re-assign 1 to $A or something strange like that.
Try:
while [ $A -eq 1 ]; do
Most of the answers have focused on the integer/string and spacing problem, which is fine, but your code looks so unidiomatic that IMO it should be completely re-factored. Let's say the idea is to process lines until one line matches the regex 'test':
while read line; do
if [[ "$line" =~ test ]] && break
# do something with $line
done
Of course this can be simplified further if you take advantage of text processing tools like sed:
sed -e '/test/,$d'
you can do this instead. No need to call external grep.
while true; do
read line
case "$line" in
*test* ) break;;
esac
done
echo $line
Have you not tried this
while [ $A == "1" ]
....
done
Edit: Whoops since Dan mentioned my error I graciously admit my mistake and have edited this accordingly - Thanks Dan for the heads up...
while [ $A -eq 1 ]
....
done
Sorry! :(
Hope this helps,
Best regards,
Tom.
All of your answers are in the Advanced Bash-Scripting guide. It is awesome.