why getting Syntax error: "do" unexpected? - syntax

select f in example*; do
echo "You selected $f"
break
done
i am getting Syntax error on do? what could be possibly wrong with this bash?
Thanks

Related

Bash Script Won't Run - Word unexpected and unexpected token

So I have 1 bash scripts,
findFungible.sh
#!/bin/bash
for file in $*;
for word in $(cat $file);
if [ $word == Fungible ];
echo Fungible found
fi
done
done
Which should be checking files if they contain the word fungible.
It's pretty much verbatim out of my lecture example.
So if I run it with bash findFungible.sh
I get:
findFungible.sh: line 2: syntax error near unexpected token "$\r''
'indFungible.sh: line 2: 'for file in $*;
So I think it has something to do with windows putting in extra line \r characters or something. As there is a \r after $.
Then if I run it with sh findFungible.sh
I get:
findFungible.sh: 2: findFungible.sh Syntax error: word unexpected (expecting "do")
Any help would be great thanks.
As someone mentioned in a reply, you have syntax errors in this, meaning, you are missing "do's" and "then's".
#!/bin/bash
for file in $*; do
for word in $(cat $file); do
if [ $word == Fungible ]; then
echo Fungible found
fi
done
done
And yes, like mentioned in the reply, bash is very sensitive to white spaces, new lines and quotes, I'm not getting into too much detail there.

Error in simple shell script prorgam

I am new to shell scripting and stuck in some syntax error in a simple program. I read an integer and compare it with some value to display the result
Please tell me how to rectify it.
#! /bin/bash
read n
if [ "$n" -le 12 ]
then
echo "a kid"
elif[ "$n" -lt 18 ]
then
echo "a teen"
else
echo "an adult"
fi
The error was:
./hello.sh: line 8: syntax error near unexpected token `then'
./hello.sh: line 8: `then'
You're missing a space between elif and [, which causes a parsing error later on.
For future reference, the shellcheck tool is a good way to diagnose errors like this one.

invalid arithmetic operator (error token is " ") with for loop

Following script
read n
for (( c=1; c<=$n; c++ ))
do
echo "HI"
done
gives error solution.sh: line 2: ((: c<=1: syntax error: invalid arithmetic operator (error token is "")
I am using BASH. What is wrong with the for loop?
edit:
I am working on the BASH hackerrank IDE and although this code is not directly related to the problem in this link, I am getting this error.
I've reproduced this error message by pressing Ctrl-E after 1. It looked like this:
$ ./1.sh
1^E
./1.sh: line 3: ((: c<=1: syntax error: invalid arithmetic operator (error token is "")
So make sure you are not pressing some strange combination of keys before enter.
You need to add this line
#!/bin/bash
at the top of solution.sh.
(if your bash is at a different location, do, in a terminal,
which bash
to determine its location)
#picasso13 just a wild guess cause it got me(and yield the same mysterious error when I tried to loop with array constructed from the input). There are 2 inputs on hackerRank(the first one is actually the size of the second). It solved my problem when I threw away the first and make sure my iteration was working on the list of numbers:
freq=()
for i in {1..100}; do
freq[$i]=0
done
read ignore
read inputs
IFS=', ' eval 'array=($inputs)'
for i in "${array[#]}"; do
(( freq[$i]++ ))
done
for i in "${!freq[#]}"; do
if [[ freq[$i] -eq 1 ]]; then
echo $i
fi
done
if you comment out my read ignore you'll reproduce the issue.

Script to match regex fails with a syntax error on a specific version of bash

I have a script to match a pattern, and if it matches, I used the match to append to a variable.
My script works on Bash v3.2.57 and fails on v4.3.30.
Could someone tell me what is wrong with the second ifcondition that matches a pattern here?
#!/bin/sh
if [ -f .file-to-read ]; then
while read p; do
echo "yes"
if [[ $p =~ "#user/"(.+)"#"[0-9]+"."[0-9]+"."[0-9]+ ]]
then
var="$var<#user/${BASH_REMATCH[1]}|$p>\\n"
fi
done < .file-to-read
fi
The error message is
Syntax error: "(" unexpected (expecting "then")
I figured out the issue. The problem was the file was being executed in #!/bin/sh when it should have been #!/bin/bash

Test : Argument Expected

I am working on a shell script and I am getting an error saying Test : Argument expected. Its basically a sed command followed by checking if there were any errors
Please find the below
sed "s|${var1}|${var2}|g" $FILE_PATH$FILE_NAME > /tmp/$FILE_NAME
if [ "$command_error" != 0 ] ; then
date
echo "Error $command_error reading file $WS_FILE"
echo "File Does not exist or is not readable"
exit 30
fi
What shell are you using?
I think you are missing a line:
command_error=$?
between the two blocks.
Be aware that many (all?) commands (eg echo $?) will actually change it's value. Therefore, it's a good idea to assign $? to a temporary variable like this.

Resources