Shell script file, checks if text file is empty or not - shell

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.

Related

Syntax error near unexpected token 'elif' shell script

I am facing the syntax error near unexpected token,elif error for the following code.
if [ "$1" == "abc" ]; then
echo "abc"
elif [ "$1" == "xyz" ]; then
echo "xyz"
else
echo "Unkown parameter"
exit 0
fi
Error is:
abc.sh: line 28: syntax error near unexpected token elif'
abc.sh: line 28:elif [ "$1" == "xyz" ]; then
Code seems alright but only point I would like to add is the OS on which you are running. The == works fine with Linux RedHat/Suse or some solaris machine but on some OS like Hpux or AIX it don't work. You should use = which is correct as you are comparing strings.

bash syntax error in sed command inside function, works fine on command line

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 ; ... ; }

syntax error near unexpected token `fi' while checking no of arguments passed

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

Syntax error near unexpected token 'done' in CygWin

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.

-bash: command substitution: line XX: syntax error: unexpected end of file

Why does the following code give me the error in the title?
_say_hey()
{
echo "hey"
}
echo "$(_say_hey())"
When calling a function, call it like an ordinary command (leave off the brackets):
echo "$(_say_hey)"
Note that the echo here is redundant; you could just write _say_hey on a line by itself for the same effect.

Resources