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

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'.

Related

How to validate if the file not passed in

So I have script that takes file from user with option -y or -n and -d is required
script runs ./example.sh -d file -y or -n
variable
file=""
I need to do validation on if the user did not pass file in for example
./example.sh -y --- it should give error "must provide file"`
./example.sh -n --- it should give error "must provide file"`
./example.sh -d --- it should give error "must provide file"
./example.sh -y -n --- it should give error "must provide file"
i came up with this but its not working
#checking if the file is provided
if [ $file -eq 1 ]; then
echo "No file provided"
exit 1
fi
# Does the file exist
which works for checking if the file exists but it only check if the file in there
if [[ ! -f $file ]]; then
echo
echo "Error: The file $file does not exist."
exit 1
fi
Why not simply loop over the arguments and test the -d, -n and -y options in a case statement. The filename must follow the -d option as I understand it. When the -d option is processed in your case statement check that the next argument holds a filename that exists on the system and is non-empty, if so, set the filename, e.g. filename="${argv[i+1]}".
When you exit the option processing loop, simply test if filename is set and you have your answer.
A short example could be:
#!/bin/bash
argc=$# ## argument count
argv=("$#") ## array holding arguments (argument vector)
filename= ## filename is empty
## loop over all arguments, filename must follow -d
for ((i = 0; i < argc; i++)); do
case "${argv[i]}" in
## file exists and non-empty, set filename to argument
-d ) [ -s "${argv[i+1]}" ] && { filename="${argv[i+1]}"; };;
-n ) # do whatever for option n
;;
-y ) # do whatever for option y
;;
esac
done
[ -n "$filename" ] || { ## check filename set or exit
printf "error: file empty or not readable.\n" >&2
exit 1
}
## valid filename received
printf "file '%s' found - script running.\n" "$filename"
Only if a valid filename is given after the -d option will the script run. Otherwise it will give an error message and exit.
Examples
$ ./process-opts.sh -y -n
error: file empty or not readable.
or
$ ./process-opts.sh -y -d -n
error: file empty or not readable.
Finally
$ ./process-opts.sh -y -d README -n
file 'README' found - script running.
or in any order:
$ ./process-opts.sh -y -n -d README
file 'README' found - script running.

Linux Shell Script error: 'Uexpected Redirection'

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

Shell: Differences from bash 3.2.57 to 4.3.41 in read -t option?

I've been running the following code on my laptop (OSX 10.11.6) and it worked as I wanted (bash version 3.2.57):
dir=""
thread=1
an="bla"
usage() { echo "Usage: Script -d <homedir> -t <threads> -a <bla or blubb>"
echo " "
echo "Options:"
echo "-h|?|--help show brief help"
1>&2;
exit 1;
}
while getopts ":d:t:a:" o; do
case "${o}" in
d)
d=${OPTARG}
dir=$d;;
t)
t=${OPTARG}
thread=$t;;
a)
a=${OPTARG}
***(($a == "bla" || $a == "blubb")) || usage***
an=$a;;
:)
echo "Missing option argument for -$OPTARG" >&2; exit 1;;
*)
usage;;
esac
done
shift $((OPTIND-1))
if [ -z "${d}" ] || [ -z "${t}" ] || [ -z "${a}" ]; then
usage
fi
***read -t 10 -p "Every parameter set as you wanted? Enter Y/y, space or ENTER! -n 1 -r REPLY***
echo "" # (optional) move to a new line
case $REPLY in
[yY] | "")
echo "correct";;
*)
echo "Terminating"
exit 1;;
esac
Trying to let it run on a linux server (Ubuntu 16.04) with the bash version 4.3.41 gave me these 3 errors (lines marked above with ***) and I don't know why:
sh testing_stuff.sh -d /home/ -t 2 -a bla
testing_stuff.sh: 29: testing_stuff.sh: [[: not found
testing_stuff.sh: 29: testing_stuff.sh: bla==blubb: not found
testing_stuff.sh: 45: read: Illegal option -t
I've changed one of the marked lines already to:
a)
a=${OPTARG}
***[[$a == "bla" || $a == "blubb"]] && usage***
an=$a;;
But this didn't change anything. I actually don't know why this worked on my laptop without any mistake, but not on the server.
Plz let me know where I've made the mistake.
On Ubuntu, the default shell is dash. Your script however uses bash features. To fix the problem, replace:
sh testing_stuff.sh -d /home/ -t 2 -a bla
With:
bash testing_stuff.sh -d /home/ -t 2 -a bla
On Ubuntu and other debian-like systems, the default shell, /bin/sh, is dash. Dash is POSIX-compliant, lightweight, and fast, but it does not support all of the features that the heavier bash supports. If you script needs bash, and yours does, run it explicitly under bash.

Shell Script syntax error: unexpected end of file (Tried Several Ans')

I am very new to Linux/Asterisk. I am trying to write a script but, when I execute it, I see the error shown below.
The script is as follows:
#!/bin/bash
asteriskbin=`which asterisk`
interval=10
trunk=”<test>”
run=true
while [[ "$run" == "true" ]]; do
checktrunk=`$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK`
if [[ $checktrunk == 0 ]]; then
echo “<TEST Trunk Down>”
else
echo “SIP trunk registration OK.”
fi
sleep $interval
exit 1
Debug error is as following:
bash -x trunks.sh
++ which asterisk
+ asteriskbin=/usr/sbin/asterisk
+ interval=10
+ trunk=$'\342\200\235test\342\200\235'
+ run=true
trunks.sh: line 15: syntax error: unexpected end of file
This might be a duplicate post but, from my search, the given answers such as chmod 755 script.sh, exit 0, exit 1, or dos2unix script.sh did not work.
Aren't you missing a done to terminate the while block? I assume a line before exit 1.

/etc/init.d/openibd: line 147: syntax error near unexpected token `;&'

Hi I am trying to install a fairly lengthy script to install infiniband and the OFED stack on rocks cluster 6.0
here is what i try to run
user#cluster # /etc/init.d/openibd restart
/etc/init.d/openibd: line 147: syntax error near unexpected token `;&'
/etc/init.d/openibd: line 147: `if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then'
can any one share with me a fix or can identify a way to fix the error in this script?
in the file /etc/init.d/openibd
here is the part of the script which contains the error on the indicated line.
CONFIG="/etc/infiniband/openib.conf"
if [ ! -f $CONFIG ]; then
echo No InfiniBand configuration found
exit 0
fi
. $CONFIG
CWD=`pwd`
cd /etc/infiniband
WD=`pwd`
PATH=$PATH:/sbin:/usr/bin
if [ -e /etc/profile.d/ofed.sh ]; then
. /etc/profile.d/ofed.sh
fi
# Only use ONBOOT option if called by a runlevel directory.
# Therefore determine the base, follow a runlevel link name ...
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
# ... and compare them
if [ $link == $base ] ; then
RUNMODE=manual
ONBOOT=yes
else
RUNMODE=auto
fi
ACTION=$1
shift
RESTART=0
max_ports_num_in_hca=0
# Check if OpenIB configured to start automatically
if [ "X${ONBOOT}" != "Xyes" ]; then
exit 0
fi
### ERROR ON FOLLOWING LINE ###
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
if [ -n "$INIT_VERSION" ] ; then
# MODE=onboot
if LANG=C egrep -L "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ${CONFIG} > /dev/null
; then
exit 0
fi
fi
fi
You've got some HTML encoding going on their you need to fix.
Replace > with >, and replace & with &.
Your script somehow had all of its > replaced with > (and & replaced by &, etc)
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
^^
This is a syntax error because there is no command between the semi-colon that terminates the preceding command and the ampersand. The HTML encoding of certain symbols is confusing the bash parser as a result.

Resources