./write_eeprom.sh: line 29: unexpected EOF while looking for matching `"' - bash

Error: unexpected EOF while looking for matching `"'
Code:
#!/bin/bash
qr_heisys=$1
testpath='/home/home/Heisys/tester/app/dut/variant'
if [[ $testdesc == "-h" ]]
then
echo "script to write data to Board EEPROM of Heisys"
echo "eeprom is at address 0x55"
echo "first argument is testdesc"
echo "second argument is qr"
echo "usage: write_eeprom.sh "write to eeprom" ""$qr_heisys"""
exit 0
fi
# teststep "${testdesc}" TSN: not needed
echo "qr_heisys = ""$qr_heisys"
echo "${testpath}${qr2eeprom}qr2eeprom.py "$qr_heisys"""
if [[? -ne "0"]]
then
return $EXIT_FAIL "Error writing QR Code to eeprom"
else
return $EXIT_PASS "Write QR to Boardeeprom was successfully PASS"
fi
I try to execute the code using ./write_eeprom.sh command on the terminal.
When googled the issue, i was mentioned about parsing error or double quote. I tried , but I am still not getting what is the issue.
Please can someone advice.
I used shellcheck.net too

Related

Bash script returning syntax error: syntax error in expression (error token is...)

I want to start a particular set of services (in the example below, the service is called users), and in my script, that is working. However, I see the following error when running the command:
./services.sh: line 40: [[: start users: syntax error in expression (error token is "users")
I am calling this command using a case statement in my script, that looks for the start parameter:
case "$1" in
(-h)
display_help
exit 0
;;
(start)
start_services "$#"
;;
My function for start_services is as follows:
start_services()
{
# Check to see if there are any arguments, if not, start all services
if [[ $# -eq 0 ]]; then
echo
echo "Starting all services:"
echo "======================"
for svc in $services
do
echo " Starting the $svc service... "
$SVCPATH/bin/$svc &
done
else
shift;
echo
for var in "$#"
do
echo "Starting the $var service... "
$SVCPATH/bin/$var & > /dev/null
done
fi
}
So, the failure is occurring at this line, line 40:
if [[ $# -eq 0 ]]; then
As I mentioned, the script is doing what I want, but I keep getting the syntax expression error.
Any ideas as to why this may be happening? Thanks in advance!
$# seems to contain your services names, but in if you try to count them. Use $# instead of $#.
Try:
if [[ -z "$#" ]]
-z: check for none zero string; faster than counting how many items there

In Mac terminal, why does this code give me "command not found", "syntax error near unexpected token", and " 'then'?

read command;
if[ $command = "make"]
then
echo "Hello"
elif[ $command = "make run"]
then
echo "Goodbye"
fi
I have looked at similar questions and their solutions are not helping here.
You need to respect spaces in bash scripts like #Jonny Henly said. Here is a modified version of your code, try it and see if it works.
read command;
if [ $command = "make" ]
then
echo "Hello"
elif [ $command = "make run" ]
then
echo "Goodbye"
fi
Hope it helps.

Bash pulling elements from a function

I'm curious to know if you can pull lines from a function in bash. Say I have this function:
error_fn()
{
echo "You need at least 2 command line arguments!"
echo "Program existing because you said you had a typo, please try agian"
echo "Sorry, one or both of the files that you entered was a directory, please try agian"
echo "Sorry, one or both files were not located, please try again"
}
Is there a way to pull the first echo statement (echo "You need at least 2 command line arguments!") from this array?
I have tried using:
error_fn $1
error_fn ("$1")
but this seems to just output all the echo statements in the function. Any ideas?
You could do your error_fn() similar to the following and pass the index value idx (zero-based, e.g. 0 - 3) as the argument to error_fn()to have the corresponding error output.
error_fn() {
array=( "You need at least 2 command line arguments!"
"Program existing because you said you had a typo, please try agian"
"Sorry, one or both of the files that you entered was a directory, please try agian"
"Sorry, one or both files were not located, please try again" )
idx=$(($1))
if [ '0' -le "$idx" -a "$idx" -le '3' ]
then
printf "error: %s\n" "${array[idx]}"
else
printf "error: function index '%d' out of range.\n" "$idx"
fi
}
Look it over and let me know if you have questions.
As for a general error/usage function, I generally prefer as much of the boiler-plate as possible being in a heredoc while having the ability to pass an error message as well through $1 for the funciton. Example:
usage() {
local ecode=${2:-0} ## set exit code '0' by default
test -n "$1" && printf "\n %s\n" "$1" >&2 ## print error msg (if any)
## heredoc of usage information
cat >&2 << USG
usage: ${0//*\//} _required_input_, etc..
This script ... (give description)
Options:
-h | --help program help (this file)
USG
exit $ecode;
}
It use would be to display usage information by default, display an error in addition to the usage if an error string is given as agrument 1, and finally to set the exit code for the program 0 by default but it will set any numeric argument as the exit code if passed as the second argument. Examples
[ -z "$1" ] && usage "warning: insufficient input." # General usage use.
[ -d "neededdir" ] || mkdir -p neededdir # error on exit and set ecode 2
[ -d "neededdir" } || usage "error: unable to create 'neededdir'." 2
The function is just another command creating output. You may parse it.
error_fn | head -n 1
However, if you rewrite the function to take an argument and to display an error message relating to that argument, it will probably do what you intend it to do:
function error_fn {
err="$1"
case "$err" in
EREFP) echo "Error: refining particles failed" >&2 ;;
ECYCE) echo "Error: number of cycles exceeded" >&2 ;;
EHUPM) echo "Error: hangup misdirected, dangling receiver" >&2 ;;
*) echo "Error: something's wrong (code:$err)" >&2 ;;
esac
}
if ! refine_particles; then
error_fn EREFP
else if ! hang_up_projections; then
error_fn EHUPM
fi

Bash Script: syntax error near unexpected token `else'

I'm trying to create simple script to check whether my external ip address has changed. However, I keep getting the following error:
syntax error near unexpected token `else'
This is my code:
#!/bin/bash
DESTDIR=exip.txt
PREVIP=$(<$DESTDIR)
EXIP=$(dig +short myip.opendns.com #resolver1.opendns.com)
echo "External ip checker schript V1.0"
echo
echo
echo "Previous ip: $PREVIP"
echo
echo "Your current external ip is $EXIP"
if [ "$PREVIP" == "$EXIP"]:then
echo "Both IP-adresses are the same"
else
echo "The IP addresses are diffrent"
echo "Sending autoremote message.."
curl "http://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEAg6VebS03KS$
echo "$EXIP" > "$DESTDIR"
fi
I've looked ad other topics about this error but i just can't figure it out.
The error is here:
if [ "$PREVIP" == "$EXIP"]:then
^^
you need to add spaces around [ and ] and finally use semicolon:
if [ "$PREVIP" == "$EXIP" ]; then
^^^
More generally, you can always use a tool called Shellcheck to check if your script has these common errors. It is both a command and a website, so you can paste your script in http://www.shellcheck.net/.

if block without else shell script in %preun scriptlet

I have the following script in my spec file %preun tag:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
fi
I get "syntax error near unexpected token `fi'" error
but when I add else block it runs fine:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
else
echo "I dont need else block"
fi
But I don't need an else block! How do I get rid of it?
PS: I tried removing the semi colon after the condition and still didn't help.
This is a shell syntax error in the line ". /etc/init.d/blahforever stop".
There is no file to be sourced (i.e. read into the current shell)
called "stop".

Resources