This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 5 years ago.
#!/bin/bash
# Connection to CR
read -p "CRIP: " varCRIP
read -p "CRUSER: " varCRUSER
read -p "CRPASS: " varCRPASS
echo curl -k -X GET “http\://$varCRIP/cgi-bin/CGILink\?cmd=validate\&user=$varCRUSER\&passwd=$varCRPASS”
#curl -k -X GET “http\://$varCRIP/cgi-bin/CGILink\?cmd=validate\&user=$varCRUSER\&passwd=$varCRPASS"
btude#DESKTOP-SQI0GTI:~$ ./ccr.sh
': not a valid identifiervarCRIP
': not a valid identifiervarCRUSER
': not a valid identifiervarCRPASS
curl -k -X GET “http:///cgi-bin/CGILink?cmd=validate&user=&passwd=”
HELP!
I am trying to input data in a var then insert it in a curl command to run
Your quotes ” look weird. This could mean you have a non-breakable space in your thing because you copy/pasted from MS-Word or something.
Try this (retyped the space manually):
read -p "CRIP:" varCRIP
read -p "CRUSER:" varCRUSER
read -p "CRPASS:" varCRPASS
Then change your quotes:
curl -k -X GET 'http\://$varCRIP/cgi-bin/CGILink\?cmd=validate\&user=$varCRUSER\&passwd=$varCRPASS'
Related
Within a bash script, I am trying to append a command string that is single and double quoted to a file (.profile).
I would like to use echo and then >> the command to .profile. Of course, I am open to any solution that works.
The command I would like to use is echo "curl -X POST -H "Content-Type: application/json" -d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null" >> .profile but clearly this doesn't work within my bash script.
I am not clear on how printf %q works and don't understand how to apply it to my problem.
I have tried this
`CMDSTRING='curl -X POST -H "Content-Type: application/json" -d '`
`CMDSTRING=${CMDSTRING}"'"`
`CMDSTRING=${CMDSTRING}'{"value1":"+PHONENUMBER","value2":"MESSAGE"}'`
`CMDSTRING=${CMDSTRING}"'"`
`CMDSTRING=${CMDSTRING}' https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null'`
`echo $CMDSTRING`
Using printf '%q' to generate .profile content looks something like the following:
{
printf '%q ' \
curl -X POST -H "Content-Type: application/json" \
-d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' \
https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY
printf '%s\n' "&>/dev/null"
} >> .profile
Note that you cannot use the %q format string if you want &>/dev/null to be parsed as syntax, since by its very nature it formats everything it's passed to be parsed as data.
Thus, we use printf '%q ' "command name" "first argument" ... for the actual command itself, and format the redirection out-of-band.
That said, note that there's value to the above only if you're substituting variables from an untrusted source (rather than hardcoding them as in the example), and are worried about invalid values being abused for command injection. If you're truly just appending a constant string to the end of a file, a quoted heredoc will let you build more natural-looking shell quoting manually (indeed, as you've already done!), and pass it through verbatim:
cat >>.profile <<'EOF'
curl -X POST -H "Content-Type: application/json" \
-d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' \
https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null
EOF
Here, everything between the <<'EOF' and the EOF are passed through exactly-as-given, including quotes and parameter expansions the shell might otherwise try to interpret.
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 6 years ago.
I'm trying to execute a ldapsearch command inside a script.
Following does not work
ADMIN_USER="$(whoami)";
ldapmodify -h myldapserver -p 23223 -D 'uid=$ADMIN_USER,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;
The above code gets executed with following error ldap_bind: Invalid credentials (49)
If hardcode the value like below, then it works.
ADMIN_USER="$(whoami)";
ldapmodify -h myldapserver -p 23223 -D 'uid=adminuser,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;
Is there a sepcific reason for this? how will i be able to fix this?
Bash isn't expanding the variable because you enclosed it in single quotes. Change the string to use double quotes:
ADMIN_USER="$(whoami)";
ldapmodify -h myldapserver -p 23223 -D "uid=${ADMIN_USER},ou=people,ou=company,dc=corpcom,dc=com" -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;
See also this answer.
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.
I use the following line to create database:
curl -X POST 'http://10.1.1.1:8086/db?u=root&p=root' -d '{name: test1}
if i try to do it from shell script:
ip=10.1.1.1
curl -X POST 'http://$ip:8086/db?u=root&p=root' -d '{name: test1}'
i have a problem with shell variable substitution within single quotas, if i try to use them within double quotas:
curl -X POST "http://$ip:8086/db?u=root&p=root" -d '{name: test1}'
variable is expanded to the right value, printing in terminal
curl -X POST "http://10.1.21.1:8086/db?u=root&p=root" -d '{name: test1}': **No such file or directory**
What would be the right solution to this problem?
Try this:
ip=10.1.1.1
curl -X POST 'http://'"$ip"':8086/db?u=root&p=root' -d '{name: test1}'
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.