I have my bash code.
#!/bin/bash
read message
echo $message | tr "A-Za-z" "N-ZA-Mn-za-m"
if [ $message == false ]; then
fi
done
It works but I get syntax errors after,
rot13.sh: line 6: syntax error near unexpected token `fi'
rot13.sh: line 6: `fi'
Now I don't expect a solution in this thread but I'm just wondering where should I go and that should I do when I get these errors? Is there any commands I can use in the compiler or documentation/checklists that check for syntax errors.
I am new to bash code so being able to check and understand there syntax error would help a lot.
Thank you.
#!/bin/bash
read message
echo "$message" | tr "A-Za-z" "N-ZA-Mn-za-m"
if [ "$message" = false ]; then
echo 'false'
fi
You need something between then and fi.
Before asking human help, always test your code on http://www.shellcheck.net/ before
And if you need to store the output of the tr command :
message="$(echo "$message" | tr "A-Za-z" "N-ZA-Mn-za-m")"
Last thing, you have a lost done statement at the end of the script
Related
Am new to unix,While executing below shell, am getting error as "unexpected token `fi'". am not sure why this error encounter though syntax are correct.
can any one help on this?
code:
#!bin/bash -xv
echo "this is test"
a=10
echo $a
if [a -gt 5]
than
echo "print"
echo $a+10
fi
O/P:
$ sh newsh.sh
this is test
10
newsh.sh: line 9: syntax error near unexpected token `fi'
newsh.sh: line 9: `fi'
The shell tokenization is white-space sensitive. You must use
if [ $a -gt 5 ]; then
do_something
echo $((a + 10))
fi
And it's #!/bin/sh; count your slashes...
a simple shell script:
#!/bin/sh
str="hello"
while [ -n $str ]; do
#do noting, just loop infinitely
done
this script throws an error, like this:
./while.sh: 5: ./while.sh: Syntax error: "done" unexpected
why, and what is the correct way ?
You need to do something! The shortest thing you can do is this:
#!/bin/sh
str="hello"
while [ -n "$str" ]; do
:
done
:, a shorthand for true, does nothing and returns success.
By the way, I quoted your string $str, as this is something that you should always do.
rather noobie question here.. i think. but i cant get this script to work. It is going to be included inside a script that i asked about here a few days ago (BASH output column formatting). basically i want to be able to scrape a site for a portion of text and return a ONLINE/OFFLINE answer. I apologize for poor formatting and weird variable names. Thanks for taking a look and helping me out!
#!/bin/bash
printf "" > /Users/USER12/Desktop/domainQueryString_output.txt
domainCurlRequest="curl https://www.google.com/?gws_rd=ssl"
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
echo $($domainCurlRequest) >> /Users/USER12/Desktop/domainQueryString_output.txt
if [ $ifStatementConditional -eq 2 ] ;
then second_check="online"
else second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
i keep getting the following error when trying to run this script
/Users/USER12/Desktop/domain_status8working.sh: line 6: [: too many arguments
i tried to rewrite another way but got same errors so my logic or syntax or something is off.
Thanks again for taking a look and helping me out!!!
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
This is a string assignment. You probably want backticks, or the $() construct. Otherwise, $ifStatementConditional will never equal 2
if [ $ifStatementConditional -eq 2 ] ;
This is expanded as:
if [ grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l -eq 2 ] ;
Which explains your error.
I think you meant that:
#!/bin/bash
curl "https://www.google.com/?gws_rd=ssl" > /Users/USER12/Desktop/domainQueryString_output.txt
ifStatementConditional=$("grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l")
if [ $ifStatementConditional -eq 2 ] ; then
second_check="online"
else
second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
No need to do printf "" > somefile.txt when you do a curl after, and you append to that file
$() is to capture subshell output. That what was missing here.
I need to write a shell script that for each file from the command line will output the number of words that are longer than a number k read from keyboard and he output must be ordered by the number of words. I don't know if what I've written so far solves the problem because I get these errors and I don't know how to fix them:
./b.sh: command substitution: line 19: unexpected EOF while looking for matching `''
./b.sh: command substitution: line 20: syntax error: unexpected end of file
./b.sh: command substitution: line 19: unexpected EOF while looking for matching `''
./b.sh: command substitution: line 20: syntax error: unexpected end of file
./b.sh: line 19: 5: command not found
#!/bin/bash
case $# in
0)
echo "The script cannot be executed due to missing arguments!"
exit 1
;;
*)
read -p "Enter the length: " k
n=$k
for file in $#; do
if [ `file $file | egrep "exec|data|empty|reloc|cannot open" > /dev$
continue
else
var=`tr ' ' '\n' < $file | grep -c '^.\{`expr $n`\}$'`
echo $file": "$var
fi
done | sort -n
;;
esac
Like the error message tells you, you are missing the pair for a ` delimiter. Also, you have attempted to nest backticks, but done it incorrectly.
In more detail,
#!/bin/bash
case $# in
0)
echo "The script cannot be executed due to missing arguments!"
exit 1
;;
(Strictly, error messages should go to stderr; echo "error" >&2. Also, it is good form to include $0 in the error message; echo "$0: error" >&2 so that you can tell from nested scripts which one is having a problem.)
*)
read -p "Enter the length: " k
n=$k
(Why do you need to copy the value to n?)
for file in $#; do
(This should properly use "$#" in order for file names with spaces etc to work correctly.)
if [ `file $file | egrep "exec|data|empty|reloc|cannot open" > /dev$
continue
You are mixing syntax here. The general syntax is if command; then action; fi. One commonly used command is [ in which case its argument list must end with a closing ], but here, it seems that if should simply examine the exit code from grep. Also, /dev$ looks like an erroneous transcription. Assuming this was meant to be /dev/null, better simply use grep -q.
Also, this is where you have a backtick which starts a command sequence, but no closing backtick to end it.
Because "cannot open" is an error message, it wil not be in standard output. Either copy stderr to stdout, or handle separately. One option is to invert the logic, so that both a missing file and the absence of the strings you are looking for translates to failure.
So,
if ! file "$file" | grep -vEq "exec|data|empty|reloc"; then
continue
Notice also how "$file" needs to be double quoted, again to cope correctly with file names containing wildcard characters, whitespace, etc.
else
var=`tr ' ' '\n' < $file | grep -c '^.\{`expr $n`\}$'`
This is where the nesting is supposed to happen, but your syntax is incorrect. echo `echo foo`echo `echo bar` evaluates to echo fooecho bar, and nested evaluation would be echo `echo foo\`echo \`echo bar` -- or more readably and portably, use echo $(echo foo$(echo )echo bar) where the nesting is obvious and unambiguous. But anyway, your expr is completely superfluous; just interpolate the value if $n (or $k!) directly. So;
var=$(tr ' ' '\n` <"$file" | grep -c "^\{$k\}")
The rest if the script looks correct.
echo $file": "$var
fi
done | sort -n
;;
esac
In BASH,
I should note that the variables $Lambda0_List etc, are read from an input file earlier in the code.
PARAM_ARRAY=("Lambda0" "N" "M" "Sigma")
for i in "${PARAM_ARRAY[#]}"
do
List="$i"_List
Vary="$i"_Vary
Use_Range="$i"_Use_Range
Initial_Str="$i"_Initial
Final_Str="$i"_Final
Step_Str="$i"_Step
Initial=${!Initial_Str}
Step=${!Step_Str}
Final=${!Final_Str}
if [ "${!Vary}" == "T" ]
then
if [ "${!Use_Range}" == "T" ]
then
eval "$List=(`seq $Initial $Step $Final `)"
echo "$i : vary, use_range"
else
echo "$i: vary, use list"
fi
fi
done
Throws a syntax error
syntax error near unexpected token `('
Normally I'm able to interpret the error and find a solution, but I don't understand why the "(" is an unexpected token.
edit:
I've noticed that this line works if I run it in shell, but not in my script,
edit:
Fiddling around with the problematic line, I found that I get a syntax error even when its commented out!
/test.sh: line 266: syntax error near unexpected token `('
./test.sh: line 266: ######## eval "$List=(seq $Initial $Step $Final `)"'
After !Final you have a ) instead of a }
After sifting through some earlier code, I fixed some issue with ' vs ", and this error stopped coming. I'm new to BASH so I didn't expect that an error message with ')' to be caused by a quote 100 lines above.
why dont use elif or case ?
eval "$List=(seq $Initial $Step $Final)"
instead of
eval "${List=(seq $Initial $Step $Final)}" or eval "${List=seq $Initial $Step $Final}"