Azure hdinsight action script not running (works locally) [duplicate] - linux

My script is taking the command line argument and it needs to pass this to curl command in double quotes. This is the simplified version of what I tried so far:
json=$1;
echo $json;
curl -X POST -d '{"asin":\"$json\", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL
But its not working. Please help

$-variables in single quoted strings don't get expanded. The -d argument needs be in double quotes, or at least the $json part needs to be:
curl -X POST -d '{"asin":"'"$json"'", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL
'-terminates the single-quoted string, then "$json" follows, and then ' starts an adjacent single quoted string.
The "$json" variable shouldn't expand to a string containing unescaped double quotes or the resulting json will be broken.

Related

How do I send a message with textbelt with user input using bash?

I want to send sms messages with a bash script using textbelt.
When I use read varnumber to save the number, textbelt only sends the first word in the message. Because there is a space in the message, the code ignores the rest of the message.
echo What number would you like to message?
read varnumber
echo What would you like to say?
read varmessage
curl -X POST https://textbelt.com/text \
--data-urlencode phone=$varnumber \
--data-urlencode message=$varmessage \
-d key=textbelt
General rule:
Quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.
So putting the variables in your curl query in quotes, should solve the problem.
curl -X POST https://textbelt.com/text \
--data-urlencode phone="$varnumber" \
--data-urlencode message="$varmessage" \
-d key=textbelt
Also, you may want to put your console output into quotes:
echo "Whatever you want to write here"
Or to combine output/input you could use read -p:
read -p "What is your phonenumber? " varnumber

Python 2.7 - optimised way to replace only a part of entire command line

I have a text file say 1.txt from where I am fetching some keywords like "sent", "inbox", "outbox" etc. for it to be replaced in a command line.
Below is how the command line looks:
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/mainlist/oldtext.txt" -T newtext.txt
I am trying to replace the "mainlist" with the data i fetched from 1.txt file, e.g.
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/sent/oldtext.txt" -T newtext.txt
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/inbox/oldtext.txt" -T newtext.txt
I have tried by adding this by creating two strings and concatenating it but looks like I am going wrong.
ltppath=r"/home/Desktop/1.txt"
with open((ltppath),'r') as fh:
ls=fh.readlines()
for line in ls:
string1="curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/"+line.strip()
string2="/oldtext.txt" -T newtext.txt"
conc=string1+string2
cmd=os.system(conc)
Can someone please help me in finding solution for this i am very new to python.
It doesn't make a lot of sense to me to use Python to generate command lines that you then pass back to the shell to execute. You could use a bash script for that, that would be a lot simpler than Python:
while read line; do
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/$line/oldtext.txt" -T newtext.txt
done </home/Desktop/1.txt
If you're doing it with Python, do it with Python. A nice library to do HTTP is called requests (docs).
import requests
upload_headers = {
'pqr': 'thisisalsolink'
}
with open('/home/Desktop/1.txt', 'r') as fh:
for line in fh:
url = 'https://example.com/artifactory/xyz/' + line.strip() + '/oldtext.txt'
with open('/home/Desktop/newtext.txt') as new_txt:
requests.put(url, headers=upload_headers, data=new_txt)

Linux print file and use it in for example a curl request

This might be a complete noob question, but I would like to know a simple oneliner to solve my problem.
Imagine following: I have 5 files, could be .txt, .sql .anything.
root#DESKTOP:/$ ls
test.sql test1.sql test2.sql test3.sql
Imagine I would like to use for example test.sql in a curl request as part of a json parameter.
root#DESKTOP:/$ cat test.sql |
curl -H "Accept: application/json"
-H "Content-type: application/json"
-X POST -d "{ \"sqlcommand\" :\"??????" }"
http://localhost:9999/api/testsql
How can I put the output of the cat command in this request at the place of the questionmark? $0, $1 etc are not sufficient. I know how to do it with a for loop. And I could write a shell script that takes an input parameter that I could paste in the command. But. I would like to do it in a simple oneliner and moreover I'd like to learn how I can get the output of the previous command when I need to use it combined with other data OR when it is bad practice i'd like to know the best practice.
Thank you in advance!
This should do what you need:
curl -X POST -d "{ \"sqlcommand\" :\"$(cat test.sql)\" }"
$(cmd) substitutes the result of cmd as a string

Get and use a password with special characters in Bash shell

I'm getting some troubles to use a password with special characters such as $ in a bash shell script.
My shell script is :
read -s -p "Password : " bindDNPass
ldapadd -H ldap://localhost -x -w $bindDNPass -D "dn=cn=Admin" -f /tmp/file.ldif
And the password could be something like $Something18$.
Well, the command
ldapadd -H ldap://localhost -x -W -D "dn=cn=Admin" -f /tmp/file.ldif`
asks for my $Something18$, and works fine.
But if I try
ldapadd -H ldap://localhost -x -w $Something18$ -D "dn=cn=Admin" -f /tmp/file.ldif
it doesn't work. I guess it's trying to resolve the variable $Something18, so I tried with \$Something18$, \$Something18\$, \\\$Something18$, ... but it keeps on failing...
How can I do? (Without changing my password...)
Put it in double-quotes and escape the $ symbol avoid special interpretation from the shell,
ldapadd -H ldap://localhost -x -w "\$Something18\$" -D "dn=cn=Admin" -f /tmp/file.ldif
(or) [more recommended]
Enclose it within single-quote to let the shell treat it as a literal string without expanding it,
ldapadd -H ldap://localhost -x -w '$Something18$' -D "dn=cn=Admin" -f /tmp/file.ldif
From the man bash page,
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, , \, and, when
history expansion is enabled, !. The
characters $ and retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or . A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.
I see two potential problems with how you're reading and using the password:
When you use the read command without the -r option, it'll try to interpret escape (backslash) sequences, which may cause trouble.
When you use a variable without wrapping it in double-quotes, it'll try to split the value into separate words and also try to expand any wildcards into a list of matching filenames. This can cause massive confusion, so you should almost always double-quote variable references.
Fixing these potential problems gives this script snippet:
read -rs -p "Password : " bindDNPass
ldapadd -H ldap://localhost -x -w "$bindDNPass" -D "dn=cn=Admin" -f /tmp/file.ldif
...But, while you should do both of these mods to make your script more robust, neither of these will change how it handles the password $Something18$. In fact, when I tried your original snippet with that password, it got passed to ldapadd correctly. If your actual password has some other special characters in it (or you've played with the value of IFS), these might help; otherwise, there's something else going on.
If your password still doesn't work after these fixes, try putting set -x before the ldapadd command (and set +x after) so it'll print what's actually being passed to ldapadd. Well, it'll print it in a possibly confusing form: it'll print an equivalent command to what's actually being executed, which means it'll add quotes and/or escapes to the password parameter as necessary so that you could run that command and it'll do the same thing. When I tried it with $Something18$, it printed:
+ ldapadd -H ldap://localhost -x -w '$Something18$' -D dn=cn=Admin -f /tmp/file.ldif
...where the single-quotes mean that what's inside them is passed directly, with no parsing. It could also have printed any of the following equivalent commands:
+ ldapadd -H ldap://localhost -x -w \$Something18\$ -D dn=cn=Admin -f /tmp/file.ldif
+ ldapadd -H ldap://localhost -x -w "\$Something18\$" -D dn=cn=Admin -f /tmp/file.ldif
+ ldapadd -H ldap://localhost -x -w $'$Something18$' -D dn=cn=Admin -f /tmp/file.ldif
so you have to take what it prints, and figure out how that'd be parsed by bash, in order to figure out what's actually being passed to ldapadd. But at least it'll give you some information about what's actually happening.
Oh, and you may notice that the DN argument isn't being double-quoted. That's because it doesn't contain any special characters, so the double-quotes aren't doing anything, so it just left them off.

Using multiple layers of quotes in bash

I'm trying to write a bash script, and I'm running into a quoting problem.
The end result I'm after is for my script to call:
lwp-request -U -e -H "Range: bytes=20-30"
My script file looks like:
CLIENT=lwp-request
REQ_HDRS=-U
RSP_HDRS=-e
RANGE="-H "Range: bytes=20-30"" # Obviously can't do nested quotes here
${CLIENT} ${REQ_HDRS} ${RSP_HDRS} ${RANGE}
I know I can't use nested-quotes. But how can I accomplish this?
Normally, you could escape the inner quotes with \:
RANGE="-H \"Range: bytes=20-30\""
But this won't work when running a command – unless you put eval before the whole thing:
RANGE="-H \"Range: bytes=20-30\""
eval $CLIENT $REQ_HDRS $RSP_HDRS $RANGE
However, since you're using bash, not sh, you can put separate arguments in arrays:
RANGE=(-H "Range: bytes=20-30")
$CLIENT $REQ_HDRS $RSP_HDRS "${RANGE[#]}"
This can be extended to:
ARGS=(
-U # Request headers
-e # Response headers
-H "Range: bytes=20-30" # Range
)
$CLIENT "${ARGS[#]}"
try this:
RANGE='\"-H \"Range: bytes=20-30\"\"
you can espape using '' and \"
no_error=''errors=\"0\"'';
You can use the fact that both '' and "" can be used for strings.
So you can do things like this:
x='Say "hi"'
y="What's up?"

Resources