Linux Shell Script error: 'Uexpected Redirection' - linux

I have a Linux system running OpenWRT. I am trying to get a shell script to post to a remote server and then split the return string into an array. I get the following error when I try to run it: "line 18: syntax error: unexpected redirection" I know this has been asked before but all the solutions pointed to it being a bash syntax issue. So I changed "#!/bin/sh" to "#!/bin/ash" and have been unable to fix it. I am new to OpenWrt.
Script:
#!/bin/ash
#SetupScript
LAN_IP="$(ifconfig | awk 'FNR==31 {print $2}')"
SerialNo="$(cat /etc/config/example/ID/device_ID.txt)"
curl --request POST 'https://example.com/op_scripts/SetupRequest.php' --data "my_key=mykey" --data "serial=$SerialNo" --data "lan_ip=$LAN_IP"
SetupReply="$(curl http://example.com/resource)"
if [[ "$SetupReply" = *"false" ]]
then
echo 'setup_failure'
else
OIFS="$IFS"
IFS=':'
read -r -a SetupVals <<< "${SetupReply}"
echo ${SetupVals[0]} > /etc/config/FilterWatch/network/curr_http_port
echo ${SetupVals[1]} > /etc/config/FilterWatch/RunTime/RemoteDatabases/db_name
echo ${SetupVals[2]} > /etc/config/FilterWatch/RunTime/RemoteDatabases/db_username
echo ${SetupVals[3]} > /etc/config/FilterWatch/RunTime/RemoteDatabases/db_password
echo 'completed'
IFS="$OIFS"
fi

Related

Multithreading in bash scripting

I run a bash script, and looping as much line in text file. to cURL the site listed in the txt file.
here is my script :
SECRET_KEY='zuhahaha'
FILE_NAME=""
case "$1" in
"sma")
FILE_NAME="sma.txt"
;;
"smk")
FILE_NAME="smk.txt"
;;
"smp")
FILE_NAME="smp.txt"
;;
"sd")
FILE_NAME="sd.txt"
;;
*)
echo "not in case !"
;;
esac
function save_log()
{
printf '%s\n' \
"Header Code : $1" \
"Executed at : $(date)" \
"Response Body : $2" \
"====================================================================================================="$'\r\n\n' >> output.log
}
while IFS= read -r line;
do
HTTP_RESPONSE=$(curl -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1) &
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g') &
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') &
save_log "$HTTP_STATUS" "$HTTP_BODY" &
done < $FILE_NAME
how i can run threading or make the loop fast in bash ?
You should be able to do this relatively easily. Don't try to background each command, but instead put the body of your while loop into a subshell and background that. That way, your commands (which clearly depend on each other) run sequentially, but all the lines in the file can be process in parallel.
while IFS= read -r line;
do
(
HTTP_RESPONSE=$(curl -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1)
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
save_log "$HTTP_STATUS" "$HTTP_BODY" ) &
done < $FILE_NAME
My favourite was to do this is generate a file that lists all the commands you wish to perform. If you have a script that performs your operations create a file like:
$ cat commands.txt
echo 1
echo 2
echo $[12+3]
....
For example this could be hundreds of commands long.
To execute each line in parallel, use the parallel command with, say, at most 3 jobs running in parallel at any time.
$ cat commands.txt | parallel -j
1
2
15
For your curl example you could generate thousands of curl commands, execute them say 30 in parallel at any one time.

syntax error near unexpected token `$'in\r'

when i build shell script am getting the below error, since am new to CentOS am not able to find out the cause can anyone help me regarding this ??
I guess we need to pass command line parameter while executing am not sure what has to be passed as parameter here.
Shell-script :
#!/bin/sh
# Default place to look for apr source. Can be overridden with
# --with-apr=[directory]
apr_src_dir=../apr
while test $# -gt 0
do
# Normalize
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case "$1" in
--with-apr=*)
apr_src_dir=$optarg
;;
esac
shift
done
if test -d "$apr_src_dir"
then
echo ""
echo "Looking for apr source in $apr_src_dir"
else
echo ""
echo "Problem finding apr source in $apr_src_dir."
echo "Use:"
echo " --with-apr=[directory]"
exit 1
fi
# Remove some files, then copy them from apr source tree
rm -f build/apr_common.m4 build/find_apr.m4 build/install.sh \
build/config.guess build/config.sub build/mkdir.sh \
build/make_exports.awk build/make_var_export.awk \
build/get-version.sh
cp $apr_src_dir/build/apr_common.m4 $apr_src_dir/build/find_apr.m4 \
$apr_src_dir/build/install.sh $apr_src_dir/build/config.guess \
$apr_src_dir/build/config.sub $apr_src_dir/build/mkdir.sh \
$apr_src_dir/build/make_exports.awk $apr_src_dir/build/make_var_export.awk \
$apr_src_dir/build/get-version.sh \
build
Error obtained :
[root#localhost apr-iconv]# sh buildconf
buildconf: line 2: $'\r': command not found
buildconf: line 6: $'\r': command not found
buildconf: line 10: syntax error near unexpected token `$'in\r''
'uildconf: line 10: ` case "$1" in
Thank you :)
This error got cleared once shell-script is changed to Unix format. That conversion you can do by executing the command dos2unix <file-name>. If have to install dos2unix to avoid error 'dos2unix: command not found'.

Bash Script Conditions

I'm building a bash script to send an email based off the last command. I seem to be having difficulties. Outside of a script the command works fine but when putting it in script it doesn't give the desired outcome.
Here is snippet of script:
grep -vFxf /path/to/first/file /path/to/second/file > /path/to/output/file.txt
if [ -s file.txt ] || echo "file is empty";
then
swaks -t "1#email.com" -f "norply#email.com" --header "Subject: sample" --body "Empty"
else
swaks -t "1#email.com" -f "norply#email.com" --header "subject: sample" --body "Not Empty"
fi
I ran the commands outside of script and I can see that there is data but when I add the commands within script I get the empty output. Please advise . Thank you in advance .
Your condition will always be true, because if [ -s file.txt ] fails, the exit status of the ||-list is the exit status of echo, which is almost guaranteed to be 0. You want to move the echo out of the condition and into the body of the if statement. (And to simplify further, just set the body to a variable and call swaks after the if completes.
if [ -s file.txt ];
then
body="Not Empty"
else
echo "file is empty"
body="Empty"
fi
swaks -t "1#email.com" -f "norply#email.com" --header "subject: sample" --body "$body"
If the only reason you create file.txt is to check if it is empty or not, you can just put the grep command directly in the if condition:
if grep -vFxfq /atph/to/first/file /path/to/second/file; then
body="Not Empty"
else
echo "No output"
body="Empty"
fi
swaks -t "1#email.com" -f "norply#email.com" --header "subject: sample" --body "$body"

using curl with -d param1=value1 seems not work properly

I have a shell Unix running every hour (crontab on CentOS 7).
Inside that shell, a loop read and proceed treatment for all new files find in a defined folder.
At the end of each files's treatment a CURL command is send with some parameters, for example :
curl https://aaaaaa.com/website -d param1=value1 -d param2=value2 ....
Each time the shell is run by crontab, the 1st CURL is correctly converted to a true URL and received by Apache/Tomcat, but all the others are bad. In fact the 2nd and the following CURLs seem not converted in the correct format like
https://aaaaaa.com/website?param1=value1&param2=value2
but they are sent like
https://aaaaaa.com/website -d param1=value1 -d param2=value2
So the website is unable to treat the parameters properly.
Why the 1st command is correctly converted to a correct URL format and not the following ?
EDIT - EDIT
The part of shell :
#!/bin/bash
...
#======================================================
# FUNCTIONS
#======================================================
UpdateStatus () {
CMD_CURL="${URL_WEBSITE} -d client=CLIENT -d site=TEST -d produit=MEDIASFILES -d action=update"
CMD_CURL="${CMD_CURL} -d codecmd=UPDATE_MEDIA_STATUS"
CMD_CURL="${CMD_CURL} -d idmedia=$4"
CMD_CURL="${CMD_CURL} -d idbatch=$3"
CMD_CURL="${CMD_CURL} -d statusmedia=$2"
if [[ ! -z "$5" ]]; then
CMD_CURL="${CMD_CURL} -d filename=$5"
fi
echo " ${CMD_CURL}" >> $1
CURL_RESULT=`curl -k ${CMD_CURL}`
CURL_RESULT=`echo ${CURL_RESULT} | tr -d ' '`
echo " Result CURL = ${CURL_RESULT}" >> $1
if [ "${CURL_RESULT}" = "OK" ]; then
return 0
fi
return 1
}
#======================================================
# MAIN PROGRAM
#======================================================
echo "----- Batch in progress : `date '+%d/%m/%y - %H:%M:%S'` -----"
for file in $( ls ${DIR_FACTORY_BATCHFILES}/*.batch )
do
...
old_IFS=$IFS
while IFS=';' read <&3 F_STATUS F_FILEIN F_TYPE F_CODE F_ID F_IDPARENT F_TAGID3 F_PROF F_YEARMEDIA F_DATECOURS F_TIMEBEGINCOURS F_LANG || [[ -n "$F_STATUS $F_FILEIN $F_TYPE $F_CODE $F_ID $F_IDPARENT $F_TAGID3 $F_PROF $F_YEARMEDIA $F_DATECOURS $F_TIMEBEGINCOURS $F_LANG" && $F_STATUS ]];
do
...
UpdateStatus ${LOG_FILENAME} ${STATUS_ERROR} ${F_ID} ${F_IDPARENT}
...
done 3< $file
IFS=$Old_IFS
...
done
You need to provide the "-d" flags and values before the URL so:
curl -d param1=value1 -d param2=value2 https://aaaaaa.com/website
Moreover, this command is going to send the parameters/values as POST parameters, not query parameters. You can use the "-G" flag, possibly combined with "--url-encode" to send as query parameters, see:
https://unix.stackexchange.com/questions/86729/any-way-to-encode-the-url-in-curl-command

"sytax error : unterminated quoted string " error when i run bash script.sh

I have a situation where, I need to call a script bash c++ which is in turn called inside another bash script:
EPSILONS=(0.1)
INPUT="/home/garrett/dev/nearness/data/objectDescriptions"
FEATURES="18"
DISTANCE_MEASURE="sgmd"
OUTPUT_PREFIX="output_5_old_bulk"
TIME_OUTPUT=$OUTPUT_PREFIX"_time"
BIN="/home/garrett/dev/nearness/recursive/bin/nearness"
echo "$INPUT"
for e in "${EPSILONS[#]}"
do
echo "$e"
echo "$e" >> "$TIME_OUTPUT"
echo "$BIN"
/usr/bin/time -v "$BIN" -d "$DISTANCE_MEASURE" -e "$e" -f "$FEATURES" -o "$OUTPUT_PREFIX"_e"$e" "$INPUT"
echo
echo >> "$TIME_OUTPUT"
echo "Saved to $OUTPUT_PREFIX"_e"$e"
done
when I run bashscript.sh, below is the error I face:
Syntax error: Unterminated quoted string

Resources