This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 1 year ago.
I have problem with this script
The Output is always "Your string has been found" even if there isn't anything in /etc/hosts.
Do you have any ideas how to solve this problem or is it even possible to do this?
Any feedback is appreciated. I'm beginning to learn bash so feel free to critize me.
Thank you.
#!/bin/bash
google= dig +noall +answer www.google.com | awk '{ print $1, $NF }'
hosts= cat /etc/hosts | grep "www.google.com"
if [ $google == $hosts ] ; then
echo "Your string has been found"
else
echo "Your string has not been found"
fi
Related
This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?
This question already has answers here:
bash replace variable name in string with variable value
(2 answers)
Closed 2 years ago.
I have a file 'test.txt', the contents is "SomeText_$(date '+%Y%m%d')".
When reading this into a variable with:
txt=`cat test.txt`
Then I try to print with
echo $txt
This prints: "SomeText_$(date '+%Y%m%d')"
How do I print this so I receive "SomeText_20200904"
The posted echo will display the content from $txt but not execute anything else.
The second line here with eval will read and process what follows then execute the result as a shell command
txt=`cat test.txt`
eval echo $txt
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
im new to bash scripting, appreciate if you can help.
Im trying to write a script to compare the lines in file with integer argument.
Here is what i've got so far but i make some mistakes and get error.
#!/bin/bash
a="$1"
b="wc -l < /filepath/filename.txt"
if (( $a < $b )); then
echo "file has more lines than integer"
else
echo "file has less lines than integer"
fi
Appreciate if you can point to where i make mistake.
b="wc -l < /filepath/filename.txt"
should instead be:
b=$(wc -l < /filepath/filename.txt)
...if you want to run that command and store its output in the variable.
This question already has answers here:
Bash script prints "Command Not Found" on empty lines
(17 answers)
Closed 6 years ago.
I am trying to learn more shell scripting. The available shells on this machine are /bin/sh, /bin/csh, and /bin/tcsh with sh being default and used here. OS is FreeBSD 9.1-RELEASE.
My current project needs to check whether a process updated the database yesterday. The first two echoes are just there for the moment verifying the variables have what I think they do.
#!/bin/sh
lastcheck=$(mysql -h dbserver.mysite.com -u myuser -pmypass mydb -se "SELECT MAX(DATE_FORMAT(datetime_sent_to_fulfiller,'%Y%m%d')) FROM print_mailing_request;"|cut -f1)
yesterday=$(echo -e "$(TZ=GMT+30 date +%Y%m%d)\n$(TZ=GMT+20 date +%Y%m%d)" | grep -v $(date +%Y-%m-%d) | tail -1)
echo "previous day was $yesterday"
echo "we last checked on $lastcheck"
if [ "$lastcheck" -eq "$yesterday" ]; then
echo "cool"
else
echo "uncool"
fi;
One question is why the : not found: output is showing up and how do I prevent it?
Another question is why both 'cool' and 'uncool' are being echoed?
Last question is why 'else' is being echoed?
$ /bin/sh pmr.cron.sh
: not found:
previous day was 20160602
we last checked on 20160602
: not found:
: not found:
cool
: not found: else
uncool
: not found:
You have carriage returns in your script; that generates the "not found" messages and is probably why both branches of your if are getting generated.
Your dates are comparable as strings, no need to use -eq to compare them as numbers.
This question already has answers here:
What is the difference between "echo" and "echo -n"?
(2 answers)
Closed 6 years ago.
I need to know the meaning of echo -n and this is example: echo -n "" > file.txt. What is the difference between echo and echo -n ?
I read man echo but need more info.
From here:
-n : Do not output a trailing newline.
The > means write it to a file named file.txt (create if doesn't exist).