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.
Related
I'm in the process (v.painful) of learning the bash language and I'm fighting with the code below. Any explanation for it's current behaviour would be greatly appreciated.
code
#!/usr/bin/env bash
function useless
{
echo "$1"
return 0
}
function call-and-report
{
TEST_CRITERIA="$1"
if $TEST_CRITERIA
then
echo "passed"
else
echo "failed"
fi
}
call-and-report $(useless "Hello World")
issue
I'm expecting the following console output:
Hello World
passed
Instead I'm seeing the following output:
./../test.sh: line 13: Hello: command not found
failed
$(command) executes command and then substitutes its standard output into the command line. So whatever useless prints is not printed on the terminal, it's substituted back into the calling command line.
So your last command becomes:
call-and-report Hello World
Then inside call-and-report, $1 is Hello, and you assign that to TEST_CRITERIA. When you do
if $TEST_CRITERIA
is equivalent to
if Hello
This tries to execute Hello as a command. Since there's no such command, you get an error.
If you want to test if there's anything in $TEST_CRITERIA, you need to use the test command, also available as [.
if [ -n "$TEST_CRITERIA" ]
Note that since you didn't quote $(useless "Hello World") each word that it outputs becomes a separate argument to call-and-report.
If you want the value of $TEST_CRITERIA printed on the terminal, you need to do that explicitly in call-and-report.
function useless
{
echo "$1"
return 0
}
function call-and-report
{
TEST_CRITERIA="$1"
echo "$TEST_CRITERIA"
if [ -n "$TEST_CRITERIA" ]
then
echo "passed"
else
echo "failed"
fi
}
call-and-report "$(useless "Hello World")"
In your example, $(useless "Hello World") will return
Hello World
So you are calling your call-and-report function with 2 arguments:
call-and-report Hello World
And in that function, you take the first one, Hello into your variable.
Then you do if Hello, which generates the error because indeed, Hello is not a command, so it neither succeeds nor fails.
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
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.