I am making a simple bash script but it seems that i'm having trouble using the let statement... getting some errors... Some help?
#!/bin/bash
echo -n "Enter the first number:"
read var1
echo -n "Enter the second nubmber:"
read var2
declare -i var3
echo ----------------------
echo "$var1 + $var2 = $(($var1+$var2))"
let res=$var1*$var2
echo "$var1 * $var2"=$res"
I'm getting the following errors:
./1.sh: line 10: unexpected EOF while looking for matching `"'
./1.sh: line 11: syntax error: unexpected end of file
They're caused by the last character of the script: " which starts a new string literal that never ends.
Related
I am to write a basg=h script that displays a message if you input the correct number (i.e. 1 = function f1, 2 = function f2, and 3 = function f3)
My code is as follow
#!/bin/bash
function f1
{
echo "This message is from function 1"
}
function f2
{
echo "This messge is from function 2"
}
function f3
{
echo "This message is from function 3"
}
function=$(typeset -F)
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
read -p "Enter a number (1, 2, or 3): " number
if ! [[ "$number" =~ ^[0-9]+$ ]]
then
echo "Not a valid number"
exit
fi
flag=0
for element in "${myarr[#]}
do
if echo "$element" | grep -q "$num"; then
$element
flag=1
fi
done
if [ "$flag" -eq 0 ]; then
echo "No function matches number $num"
fi
Now when I run the code I obtain the error
q6: line 43: unexpected EOF while looking for matching `"'
q6: line 45: syntax error: unexpected end of file
Any help sourcing the errors?
for element in "${myarr[#]}
Missing the end quote. You can catch errors like this by seeing where the syntax highlighting goes wonky. Notice how much of the script following this line is incorrectly colored red?
Even better, you can use ShellCheck:
Line 32:
for element in "${myarr[#]}
^-- SC1009: The mentioned syntax error was in this for loop.
^-- SC1078: Did you forget to close this double quoted string?
Fix that and you get:
Line 1:
#!/bin/bash
^-- SC1114: Remove leading spaces before the shebang.
Line 20:
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).
^-- SC2006: Use $(..) instead of legacy `..`.
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
Line 22:
read -p "Enter a number (1, 2, or 3): " number
^-- SC2162: read without -r will mangle backslashes.
Line 35:
if echo "$element" | grep -q "$num"; then
^-- SC2154: num is referenced but not assigned.
Kindly anyone help me out in doing this (as mentioned in the subject line).
Tried various links on this website and on Google, but nothing is working.
So, I want to store the output of C++ executable to a shell variable. Trying to do this like this:
printf '\n'
printf '\n'
printf '<h3>[Test case]</h3>'
printf '<p> Input: %s</p>' "$p"
#out=`echo $p | ./Main << EOF `
#out=`echo tamu 1 algo clrs 1234 5 1234 4 Q | ./Main`
out = "$(echo ./Main <<EOF
tamu
1
algo
clrs
1234
5
1234
4
Q
EOF)"
printf '<p> Your output: %s</p>' "$out".
But getting following error:
$ASNLIB/run_submission.sh: line 27: unexpected EOF while looking for matching `)'
$ASNLIB/run_submission.sh: line 90: syntax error: unexpected end of file.
However, indentation and braces everything is correct. Also, just doing this:
./Main << EOF
tamu
1
algo
clrs
1234
5
1234
4
Q
EOF
gives me output but I want to store this output in a shell variable for further computation. Kindly anyone let me know how to do this.
So, here is the full file:
#!/bin/bash
trap cleanup 1 2 3 6 9
cleanup()
{
echo "Critial error occurred. Program failed to run."
exit 1
}
rm -f summary.txt
let "total_score=0"
passfail="PASSED"
let "Outcome=PASSED"
printf '\n'
printf '\n'
out =`./Main <<EOF
tamu
1
algo
clrs
1234
5
1234
4
Q
EOF`
echo "Your output: "
echo $out
printf '<p>====================================================================================================</p>'
printf '\n'
printf '\n'
ref =`./Main_ref <<EOF
tamu
1
algo
clrs
1234
5
1234
4
Q
EOF`
echo "Expected output: "
echo $ref
printf '<p>====================================================================================================</p>'
python $ASNLIB/compare_string.py "$out" "$ref" > /dev/null
if [ "$?" != "0" ]; then
printf '<p style="color:red;">[FAILED.]</p>'
Outcome="FAILED"
let "total_score=0"
printf '<p><b> Outcome: %s</b></p>' "Please follow all the instructions as mentioned in the lab-work. It seems that you have missed any of the following instructions. 1) Might you have not returned the value in Library.cpp file using reference. 2) Please be ensure that you also need to change the declaration of this Library::getBook() in Library.h file."
printf '<p><b> Total Score: %s</b></p>' $total_score
else
printf '<p style="color:green;">[PASSED.]</p>'
Outcome="PASSED"
let "total_score=1"
printf '<p><b> Outcome: %s</b></p>' "Complete"
printf '<p><b> Total Score: %s</b></p>' $total_score
fi
echo "Correctness, $Outcome, $total_score, 1" >> summary.txt
I can see errors after doing changes as what is mentioned in the first comment which are as follows:
$ASNLIB/run_submission.sh: line 39: warning: here-document at line 29 delimited by end-of-file (wanted `EOF')
$ASNLIB/run_submission.sh: line 29: out: command not found
$ASNLIB/run_submission.sh: line 59: warning: here-document at line 49 delimited by end-of-file (wanted `EOF')
$ASNLIB/run_submission.sh: line 49: ref: command not found
And the output of variables - "out" and "ref" is empty.
What about
out=`./Main <<EOF
tamu
1
algo
clrs
1234
5
1234
4
Q
EOF`
EDIT
Remove the space before the equal sign:
out=`./Main <<EOF
# ...
ref=`./Main_ref <<EOF
Check this answer for more information about the use of spaces around the equal sign in Bash.
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...
I have seen the concept of process substitution. But the following code still give me the syntax error
script.sh: syntax error at line 44: `<' unexpected
script.sh: syntax error at line 44: `<' unexpected
Here is the code:
#!/bin/bash
count=1
FILENAME=$1
JUDGE="YATES"
echo "VALUE OF JUDGE IS $JUDGE"
STATUS=""
#The file is read using while loop , file being supplied as cmd line arg , file simply contains the list of courts .
#cat $FILENAME | while read LINE
while read LINE
do
#Selecting the filepath here $LINE contains the court every time it iterates
FILEPATH=/elFZ/dZcollection/$LINE/DETER_JUDGE
#Checking whether the DETER_JUDGE exists or not ,
cat $FILEPATH >> yatisawhney.txt 2>> yati_errors.txt
#if the DETER_JUDGE file exists then
if [ $? = 0 ]
then
echo "INSIDE IF"
STATUS="Yes"
#cat $FILEPATH | while read -r JUDGELINE
#open the DETER JUDGE file and read the values and updating the JUDGE variable.
while read JUDGELINE
do
line_length=$JUDGELINE
JUDGE=$JUDGE$line_length"||||||"
#JUDGE=1000
done < < ( $FILEPATH )
echo "Value of judge is $JUDGE"
else
FILEPATH="N.A."
STATUS="No"
JUDGE="N.A."
fi
#here I am not getting the updated value
echo $JUDGE >> JUDGE_NAME
echo $count","$LINE","$STATUS","$JUDGE","$FILEPATH >> judgeData.csv
count=`expr "$count" + 1`
JUDGE=""
done < < ( $FILENAME )
I am unable to the fetch the values from the inner while loop. However I am able to fetch them inside the loop, once I get outside the values get lost.
The process substitution uses the <(...) construct, there should be no space between the < and the left parenthesis.
To read from a file, you don't need process substitution at all:
done < "$FILENAME"
On AIX (Korn Shell), how could I achieve dynamic variable name generation and assignment?
I basically have a string as "LINE 1 LINE 2 LINE 3 LINE 4 LINE 5" and I want this long string to be split into multiple lines (each 7 Characters Long) and assign those to dynamically generated variables such as msg_txt_line_1, msg_txt_line_2 and so on.
I looked for information on Internet and using some help from Building Dynamic Variable Names in KornShell i built this snippet so far but it gives errors.
foo.sh
TEXT='LINE 1 LINE 2 LINE 3 LINE 4 LINE 5'
counter=1
echo $TEXT | fmt -7 | while read line ; do eval msg_txt_line_$counter=$line;counter=$(( counter += 1 )) ; done
echo $msg_txt_line_1
echo $msg_txt_line_2
echo $msg_txt_line_3
echo $msg_txt_line_4
echo $msg_txt_line_5
The error is
AIX:>foo.sh
foo.sh[4]: 1: not found.
foo.sh[4]: 2: not found.
foo.sh[4]: 3: not found.
foo.sh[4]: 4: not found.
foo.sh[4]: 5: not found.
Thanks for your guidance.
I have been working on this and with the comments from JS, I have managed to write the following script which works fine. This can still be improved though for example if the long line contains characters such as `, ", ', and Shell special characters? Appreciate if someone could help me improve this snippet.
x=1
TEXT="No one is going to hand me success. I must go out & get it myself. That's why I'm here. To dominate. To conquer. Both the world, and myself."
echo "$TEXT" | fmt -30 | while IFS=$'\n' read -r line; do export msg_txt_line_$x="$line"; let "x=x+1";done
echo "$msg_txt_line_1"
echo "$msg_txt_line_2"
echo "$msg_txt_line_3"
echo "$msg_txt_line_4"
echo "$msg_txt_line_5"
You can create an array and then assign values. Something like:
$ TEXT='LINE 1 LINE 2 LINE 3 LINE 4 LINE 5'
$ echo "$TEXT" | fmt -w7 > myfile
$ while IFS=$'\n' read -r line; do export msg_txt_line_$((++x))="$line"; done <myfile
$ echo "$msg_txt_line_1"
LINE 1
Update:
$ TEXT='LINE 1 LINE 2 LINE 3 LINE 4 LINE 5'
$ echo "$TEXT" | fmt -w7 > myfile
$ while IFS=$'\n' read -r line; do export msg_txt_line_$((++x))="$line"; done <myfile
$ echo "$msg_txt_line_1"
LINE 1