Using variable in bash command and collecting output in a variable is not working - linux

I have the following command which, if fired with a hardcoded IP works fine -
ad_request_output="$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H <Some private IP> -u 'http://<Some private IP>/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0')"
echo $ad_request_output gives expected output -
HTTP OK: HTTP/1.1 200 OK - 217 bytes in 0.055 second response time |time=0.054961s;0.180000;0.250000;0.000000 size=217B;;;0
But, using a variable IP gives a different output -
private_ip=<Some private IP>
ad_request_output=$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H $private_ip -u 'http://$private_ip/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0')
echo $ad_request_output gives -
HTTP WARNING: HTTP/1.1 400 Bad Request - 311 bytes in 0.001 second response time |time=0.000703s;0.180000;0.250000;0.000000 size=311B;;;0
Tried with this format of putting variable ${private_ip} as well, but got same output -
ad_request_output=`/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H ${private_ip} -u 'http://${private_ip}/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0'`
I checked some related questions Bash - Using variable inside command not working but got no clue what am I doing wrong.
I have working code to use variable in a command -
php /var/cake_1.2.0.6311-beta/beforeInstall.php ${OUTPUT}
But, not sure how to do it when the output needs to be collected in a variable.

In bash all characters inside ' are preserved. That means that $<variable> is not expanded. See this:
$ something=value
$ echo $something
value
$ echo "$something"
value
$ echo '$something'
$something
In your specific case $private_ip would not be expanded to the value of private_ip. Relevant section from man bash:
Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.

The private_ip variable doesn't get expanded if you use single quote. You should use double quotes:
ad_request_output=$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H ${private_ip} -u "http://${private_ip}/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0")

Oliv already answered your question correctly. But I would like to add some help so that you can find the answer yourself in the future.
Given your script to begin with (I simplified the command):
#!/bin/bash
private_ip=127.0.0.1
ad_request_output=$(check_http -H $private_ip -u 'http://$private_ip/fam/postGetAd.php')
echo $ad_request_output;
You could simply call bash with the parameter -x, which prints every command before it is executed
bash -x test.sh
Output:
adiesner#local /tmp> bash -x test.sh
+ private_ip=127.0.0.1
++ check_http -H 127.0.0.1 -u 'http://$private_ip/fam/postGetAd.php'
t.sh: Line 4: check_http: Command not found
[...]
You can clearly see that $private_ip was replaced once, but not the second time.
Another way is to simply output the command by placing "echo" in front of it.
#!/bin/bash
private_ip=127.0.0.1
ad_request_output=$(echo check_http -H $private_ip -u 'http://$private_ip/fam/postGetAd.php')
echo $ad_request_output;
Output:
adiesner#local /tmp> ./test.sh
check_http -H 127.0.0.1 -u http://$private_ip/fam/postGetAd.php
As soon as you know what is going on it should be easy to enter the correct search words.

Related

Different file indirection at every watch execution

I'm trying to collect some data at every second to different file(preferably timed name file). I'm trying to use watch command but it's not behaving as per expectation.
watch -p -n 1 "curl -s http://127.0.0.1:9273/metrics > `date +'%H-%M-%S'`.txt"
Only 1 file is created and data is being directed to it. I was expecting it to write to different files. I'm not looking to alternative methods. Can it be modified to achieve said task?
quote it with single quote
or wrap the command line passed to watch , with bash -c
pay attention to the quotes i used, they can not be swapped
both following command works for one second per file
watch -p -n 1 'curl -s http://127.0.0.1:9273/metrics > `date +'%H-%M-%S'`.txt'
watch -p -n 1 'bash -c "curl -s http://127.0.0.1:9273/metrics > `date +'%H-%M-%S'`.txt"'

Open new gnome-terminal from scripts and input vars from present script.

#!/bin/bash
Dpath=/home/$USER/Docker/
IP=`sed -n 1p /home/medma/.medmadoc`
DockerMachine=`sed -n 2p /home/$USER/.medmadoc`
DockerPort=`sed -n 5p /home/$USER/.medmadoc`
DockerUser=`sed -n 3p /home/$USER/.medmadoc`
DockerPass=`sed -n 4p /home/$USER/.medmadoc`
if [ ! -d $Dpath ] ; then
mkdir -p $Dpath
else
stat=`wget -O ".dockerid" http://$IP/DOCKER-STAT.txt`
for ids in `cat .dockerid`
do
if [ "$ids" == "$DockerMachine" ] ; then
gnome-terminal -x sh -c 'sshfs -p$DockerPort $DockerUser#$IP:/var/www/html $Dpath ; bash '
nautilus $Dpath
zenity --info --text "Mounted $DockerMachine"
exit
else
:
fi
done
zenity --info --text "No Such ID:$DockerMachine"
fi
gnome-terminal -x sh -c 'sshfs -p$DockerPort $DockerUser#$IP:/var/www/html $Dpath ; bash '
this command opens up a new terminal but the problem is that it does not load vars like $DockerPort $DockerUser $IP $Dpath from this script.
How do I input the values in these vars from this script to the newly opened terminal ?
Thanks !
As indicated before, you could try to use double quotes instead of single quotes around the sshfs invocation.
Single quotes in Bash are used to delimit verbatim text, in which variables are not expanded. Double quotes, in contrast, allow for variables expansion and command substitution ($(...)) to take place.
If you do use double quotes, beware of unintended side-effects (your username may contain a space, a dollar, a semicolon, or any other shell-special character). A cleaner approach would be to export the variables to the environment before calling gnome-terminal (and not forgetting to add double quotes around your variables inside the single-quotes), so that your code looks like :
export Docker{Port,User} IP Dpath
gnome-terminal -x sh -c 'sshfs -p"$DockerPort" "$DockerUser#$IP":/var/www/html "$Dpath" ; bash'
You may not want to pollute the environment with variables that will only be used once. If that is the case, instead of exporting them, you can use Bash's declare -p feature to serialize variables before loading them into a new environment (in my opinion, this is the cleanest approach). Here is what it looks like :
set_vars="$(declare -p Docker{Port,User} IP Dpath)"
gnome-terminal -x bash -c "$set_vars;"'sshfs ....'
Using this latest method, the variables are only visible to the shell process that runs the sshfs command, not gnome-terminal itself nor any sub-process run thereafter.
PS: you could read all your variables at once from the ~/.medmadoc file by using the following code instead of repeated sed invocations :
for var in IP Docker{Machine,User,Pass,Port}; do
read $var
done < ~/.medmadoc
This code makes use of the read builtin, that reads a line of input into a variable (in its simplest form).
PPS: That stat variable probably won't contain any useful information, since the output of wget was redirected by the -O flag. Perhaps you meant to store the result code of wget into stat, in which case what you meant was :
wget -O .dockerid ...
stat=$?

Why bash converting double quotes to single quotes?

I wrote a simple test script having the below code
#!/bin/bash
CHM=test
NAME="user#test"
if ! grep -q test /etc/passwd
then
useradd -s /bin/bash -m -d /home/test -c "${CHM} : ${NAME}" test
fi
but I'm having problem with bash converting my " to ' example output
+ CHM=test
+ NAME=user#test
+ grep -q test /etc/passwd
+ useradd -s /bin/bash -m -d /home/test -c 'test : user#test' test
useradd: invalid comment 'test : user#test'
I can't figure how to fix this any input is welcome, try to search but does not yield any preferred result
OS: RHEL6
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu
Thanks
/etc/passwd is delimited by : character and hence useradd is rejecting the comment. Try removing : in your comment.
Bash doesn't convert any quotes, this is purely an output artifact of set -x. As you can see, the ${VARIABLES} have been expanded in the set -x output. After that the quote type no longer matters and the bash maintainer chose to display all words containing blanks in single quotes to make the word boundaries clear.
The actual problem, as codegrep_admin correctly identified, is the impossibility to use a colon : for any field in /etc/passwd. E.g. you can't have a user named foo:bar and you can't have a comment saying test:user#test and you can't have a shell named ba:sh.

Execute a find command with expression from a shell script [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 6 years ago.
I'm trying to write a database call from within a bash script and I'm having problems with a sub-shell stripping my quotes away.
This is the bones of what I am doing.
#---------------------------------------------
#! /bin/bash
export COMMAND='psql ${DB_NAME} -F , -t --no-align -c "${SQL}" -o ${EXPORT_FILE} 2>&1'
PSQL_RETURN=`${COMMAND}`
#---------------------------------------------
If I use an 'echo' to print out the ${COMMAND} variable the output looks fine:
echo ${COMMAND}
screen output:-
#---------------
psql drupal7 -F , -t --no-align -c "SELECT DISTINCT hostname FROM accesslog;" -o /DRUPAL/INTERFACES/EXPORTS/ip_list.dat 2>&1
#---------------
Also if I cut and paste this screen output it executes just fine.
However, when I try to execute the command as a variable within a sub-shell call, it gives an error message.
The error is from the psql client to the effect that the quotes have been removed from around the ${SQL} string.
The error suggests psql is trying to interpret the terms in the sql string as parameters.
So it seems the string and quotes are composed correctly but the quotes around the ${SQL} variable/string are being interpreted by the sub-shell during the execution call from the main script.
I've tried to escape them using various methods: \", \\", \\\", "", \"" '"', \'"\', ... ...
As you can see from my 'try it all' approach I am no expert and it's driving me mad.
Any help would be greatly appreciated.
Charlie101
Instead of storing command in a string var better to use BASH array here:
cmd=(psql ${DB_NAME} -F , -t --no-align -c "${SQL}" -o "${EXPORT_FILE}")
PSQL_RETURN=$( "${cmd[#]}" 2>&1 )
Rather than evaluating the contents of a string, why not use a function?
call_psql() {
# optional, if variables are already defined in global scope
DB_NAME="$1"
SQL="$2"
EXPORT_FILE="$3"
psql "$DB_NAME" -F , -t --no-align -c "$SQL" -o "$EXPORT_FILE" 2>&1
}
then you can just call your function like:
PSQL_RETURN=$(call_psql "$DB_NAME" "$SQL" "$EXPORT_FILE")
It's entirely up to you how elaborate you make the function. You might like to check for the correct number of arguments (using something like (( $# == 3 ))) before calling the psql command.
Alternatively, perhaps you'd prefer just to make it as short as possible:
call_psql() { psql "$1" -F , -t --no-align -c "$2" -o "$3" 2>&1; }
In order to capture the command that is being executed for debugging purposes, you can use set -x in your script. This will the contents of the function including the expanded variables when the function (or any other command) is called. You can switch this behaviour off using set +x, or if you want it on for the whole duration of the script you can change the shebang to #!/bin/bash -x. This saves you explicitly echoing throughout your script to find out what commands are being run; you can just turn on set -x for a section.
A very simple example script using the shebang method:
#!/bin/bash -x
ec() {
echo "$1"
}
var=$(ec 2)
Running this script, either directly after making it executable or calling it with bash -x, gives:
++ ec 2
++ echo 2
+ var=2
Removing the -x from the shebang or the invocation results in the script running silently.

.bash_profile ldapsearch function not outputting to terminal [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I have a bash function in my .bash_profile that is not returning results to the terminal.
When I run the command as normal via the CLI results are returned.
ldap_check_cleaup ()
{
ldapsearch -LLL -h itdsvbms.SomeDomain.org -p 389 \
-D "uid=SomeUser,o=SomeDomain.org" -w SomePassWord -b "ou=People,o=SomeDomain.org" \
-s sub '(&(ReservedRMAliases=$1)(!(RMid=*))(RMAliasUpdateDate=12/01/2012 19:02:00)(RMAliasStatus=IN)(status=IN))' | \
tee /dev/tty
}
running ldap_check_clenaup TestRecord returns no output when executed from the bash prompt. TestRecord does exist and when the following command is run from the CLI, the correct record is returned:
ldapsearch -LLL -h itdsvbms.SomeDomain.org -p 389 -D "uid=SomeUser,o=SomeDomain.org" \
-w SomePassWord -b "ou=People,o=SomeDomain.org" \
-s sub '(&(ReservedRMAliases=TestRecord)(!(RMid=***))(RMAliasUpdateDate=12/01/2012 19:02:00)(RMAliasStatus=IN)(status=IN))' | \
tee /dev/tty`
The lack of out put only happens when I try to use this ldapsearch and the arguments as a bash function.
I think this may be related to using ' instead of " for the attribute (!(RMid=*)) but I am unsure, please help.
You need to use double-quotes around the argument that contains $1. Variable interpolation is not performed inside single-quoted strings.

Resources