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.
Related
ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn -D
cn=user,ou=resources,o=otherresource,c=xx -w server101 -b ou=Non-
Staff,ou=people,o=test,c=us '(&(objectClass=inetOrgPerson)
(createTimestamp<=$month_8)(!(nEApps=*))(nEDHHSNFAccNbr=\00)
(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))' dn
I can only use my variables if the filters are surrounded by double quotes
and I can only use the not filter if they are surround by single quotes is there a way to make this work in the same query?
! is a special character (it does a command history substitution) in interactive mode, but not in a script. Is this supposed to be something you'll type in interactively, or something embedded in a script? If the latter, you can just use double-quotes, and the ! won't be a problem.
If you're using this interactively, there are a coupe of options. One is to mix quoting modes in a single argument , as in "double-quoted-section"'single-quoted-section'"another-quoted-section". This looks weird, but works fine. Something like this:
ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn \
-D cn=user,ou=resources,o=otherresource,c=xx -w server101 \
-b ou=Non-Staff,ou=people,o=test,c=us \
"(&(objectClass=inetOrgPerson)(createTimestamp<=$month_8)("'!'"(nEApps=*))(nEDHHSNFAccNbr=\00)(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))" dn
^^^^^
Alternately, you could define a variable as ! (using single-quotes), then use that variable inside double-quotes:
exclamation='!'
ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn \
-D cn=user,ou=resources,o=otherresource,c=xx -w server101 \
-b ou=Non-Staff,ou=people,o=test,c=us \
"(&(objectClass=inetOrgPerson)(createTimestamp<=$month_8)($exclamation(nEApps=*))(nEDHHSNFAccNbr=\00)(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))" dn
^^^^^^^^^^^^
The ! filter only 'needs' single quotes because of the *, which the shell will attempt to expand itself as a wildcard.
Solution: escape it.
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'
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.
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.