I use this script in CygWin:
#!/bin/sh
rm -f nplist.txt
find . -name "*.html"| while read file; do
awk '
/titleTable/ { if (NR==53) match1=1 }
/id="maincontainer"/ { if (NR==169) match2=1 }
{ if (match1 && match2) exit 69 }
' file
if test $? -eq 69; then
echo $file
sed -i '53,121d; 166,168d' $file
else
echo $file >>nplist.txt
fi
done
..and terminal tell me:
/cygdrive/c/1/test.sh: line 14: syntax error near unexpected token `done'
/cygdrive/c/1/test.sh: line 14: `done'
Why? Please, anybody help me!
Bash 4.2.45 on Linux has no problem executing your script. Please check that the script text in the post matches your file. Check if there are any special characters in the file that get lost when transferring to the post text. Try simplifying your script, removing commands one by one and checking if it starts working.
Otherwise, I see one problem with your script: awk receives literal string file as the file to operate on, instead of $file variable value.
Related
I have this small part of bash script that gets output and writes/echos the output, but it states
./test.sh: line 24: : No such file or directory
while IFS= read -r line; do
cat << EOF
- request:
path: $line
method: GET
response:
raw_body: $res
status: $code
EOF
done < "$1"
Why is that?
Instead of launching sh test.sh, you should launch sh test.sh <input_file>, like this, your input parameter $1 will be filled in.
I am trying to do something similar to "try.. catch" with bash, for which I read from this post that a good option to somehow replicate try/catch is to use the || and && operators.
In my case, I have a piece of code supposed to zip some files, but the zip is actually empty, so it throw an error:
zip -v OUTPUT.zip file1 file2 >> $LOGFILE 2>&1
Then in the LOGFILE, I see:
zip error: Nothing to do! (OUTPUT.zip)
zip warning: OUTPUT.zip not found or empty
...(continued error message)
So I do this instead, to "catch the error"
{ zip -v OUTPUT.zip file1 file2 >> $LOGFILE 2>&1 } || { printf "Error while zipping!" >> $LOGFILE && exit 1 }
..which partially works. It does exit the code, but doesn't do the printf command (or at least I can't see it in the LOGFILE).
Also, although it encounters the "exit 1" command (on line 115), I also have the message (line 120 is my last line):
line 120: syntax error: unexpected end of file
What is wrong in my command? Why doesn't it do the print, and why the "unexpected end of file" message? Sorry if this question is more related to general bash programming than to the use of || &&, but I didn't know how to categorize this otherwise.
Thanks a lot!
Within braces, you must terminate all commands with a semi-colon, as per the following transcript (you can see on the last line bash is waiting for more command to be typed in):
pax> { false; } || echo x;
x
pax> { false } || echo x;
+++> _
You also don't need to "brace up" a simple command, so the correct thing in your case would be:
zip -v OUTPUT.zip file1 file2 >> $LOGFILE 2>&1 || { echo "Error while zipping!" >> $LOGFILE ; exit 1 }
I've also used echo rather than printf, on the assumption you'll want a newline character at the end of the file, and made the exit unconditional on the success or otherwise of printf/echo.
I want to check if .todo.txt is empty or not.
If is is empty, I want to add the echo in the file, and if it already has content, I want to do something else.
I tried:
hasData()
{
echo "Hello"
}
isEmpty()
{
echo -e "$n\t$comment\t$dueD/$dueM/$dueY " >> .todo.txt
}
if [ -s $file ] ; then
hasData()
else
isEmpty()
fi
When I run above code, I get the following:
./todo.sh: line 25: syntax error near unexpected token `else'
./todo.sh: line 25: ` else'
when i run my code i get the following ./todo.sh: line 25: syntax error near unexpected token else' ./todo.sh: line 25: else'
You must use parentheses to define a shell function, but they have no part in calling one. A shell function is invoked just like any other command:
if [ -s $file ] ; then
hasData
else
isEmpty
fi
If your functions took arguments, then you would list them after the function name -- again, just like for any other command.
I've got a a bash script with two functions, one is the main function which includes a case command, that case command will then call a second function and pass a specific argument.
In the second function I have a command that works on the command line, but when run as part of the bash script I get the following error:
: line 57: syntax error near unexpected token `('
: line 57: ` local DATE=`echo $URL|sed -r 's/.*____([0-9]{1,2})_([0-9]{1,2})_([0-9]{1,2}).*/20\3-\1-\2/;s/-([0-9]{1})-/-0\1-/;s/-([0-9]{1})$/-0\1/'`'
the function is,
dlshow ()
{
local URL=$1
echo "URL: "$URL
local DATE=`echo $URL|sed -r 's/.*____([0-9]{1,2})_([0-9]{1,2})_([0-9]{1,2}).*/20\3-\1-\2/;s/-([0-9]{1})-/-0\1-/;s/-([0-9]{1})$/-0\1/'`
I can't figure out why as a bash command I get the error.
Try this:
echo $URL | sed -e '...' | { read DATE ; ... ; }
I am new to shell scripting and created a script to check arguments are passed or not using if else condition . But it is always giving a error 'syntax error near unexpected token `fi' '
is it always required to use ';' after if condition brackets.
I runned it online on http://www.compileonline.com/execute_bash_online.php it is working well but not on my system(Centos 6.2). Am i missing something in env settings or is it something else.
Code
#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
echo "Argument are passed to shell script"
else
echo "No arguments are passed to shell script"
fi
Error message
[root#local shell_scripts]# sh test.sh 12 13
test.sh: line 7: syntax error near unexpected token `fi'
test.sh: line 7: `fi'
[root#local shell_scripts]#
my env details
[root#local shell_scripts]# env
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.1.17 49656 22
PERL5LIB=/home/bharat/perl5/lib/perl5/x86_64-linux-thread-multi:/home/bharat/perl5/lib/perl5
PERL_MB_OPT=--install_base /home/bharat/perl5
SSH_TTY=/dev/pts/6
USER=bharat
PATH=/home/bharat/perl5/bin:/usr/kerberos/sbin:/home/bharat/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/bharat/bin
MAIL=/var/spool/mail/bharat
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=4
PERL_LOCAL_LIB_ROOT=/home/bharat/perl5
LOGNAME=bharat
CVS_RSH=ssh
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
PERL_MM_OPT=INSTALL_BASE=/home/bharat/perl5
_=/bin/env
[root#local shell_scripts]#
Answering the first of your questions: ; is not required after if condition. Syntactically, then should go to a separate line. If then is in the 1st line, you use ;. The following code is correct:
if [ $# -eq 1 ]
then
echo ok
fi
Also, these brackets are not actually if condition brackets. If condition is any valid bash command, and brackets are equivalent to test, see man test for ref.
The following are valid if statement beginnings:
if cp a.txt b.txt ; then ...
if test $# -eq 1 ; then ...
The second one is equivalent to your if statement.
Works for me:
$ cat > f.sh
#!/bin/bash
echo "print a message"
if [ $# -eq 1 ]; then
echo "Argument are passed to shell script"
else
echo "No arguments are passed to shell script"
fi
$ bash f.sh
print a message
No arguments are passed to shell script