bash: echo in and also out of program - bash

damn I'm getting sick of these titles.
I was getting:
(standard_in) 1: syntax error
(standard_in) 1: illegal character: S
(standard_in) 1: syntax error
(standard_in) 1: syntax error
types of errors.
I was convinced that this has to be caused by an unbalanced echo, or something, but I have 100s in my script so its hard to debug. so I made a function to debug the output:
#!/bin/bash
myecho ()
{
echo -e $1;
if [ $? != 0 ]
then
dev=$(ps | awk '{print $2}')
dev="/dev/$dev" # get the... whatever /dev/pts/2 is
echo "echo -e \x22last echo failed:\\x60\\x27$1\\x27\\x60\x22 > $dev" | sh # format to escape the string so I can see it
exit 2;
fi
}
and then I would change all the "echo -e"'s to "myecho". to debug. It didn't work. I'm wondering if this is possible, or even a possible problem, and if so how I can debug with a debug function.

Related

compute exit code based on text output

I have a program which returns always zero exit code, even in case of an internal error. In case of an error there is additional output to standard output like:
# always-zero-exit
Error: Line: 1, Column: 1
some further scary error description....
Can someone please advice how to make a short bash workaround like the one below:
# always-zero-exit > search-for-string-'Error'-and-returns-non-zero-if-found
As Biffen suggested in the comment, use ! grep ERROR_PATTERN.
Example:
# Tiny test command that exit with status 0 and
# prints errors into STDOUT:
echo 'Error: Line: 1, Column: 1' > out.log
! grep -q 'Error' out.log
echo $?
# Prints: 1
# Same, no errors:
echo 'Line: 1, Column: 1' > out.log
! grep -q 'Error' out.log
echo $?
# Prints: 0
You may want to use more complex error message processing, for example:
! grep -Pqi '(error|fail|exception)' out.log
Here, grep uses the following options:
-P : Use Perl regexes.
-i : Use case-insensitive matching.
-q : Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found.
SEE ALSO:
grep manual

bash file gives error "syntax error near unexpected token `fi'"

When trying to execute my script below, I get this error:
syntax error near unexpected token `fi'
file:Ccamcheck.sh is in usr/script
My script:
#!/bin/sh
if ps x |grep -v grep |grep -c CCcam >/dev/null
then
echo "cccam... ok"
echo && date >> /var/log/CCcam.ok
else
echo "cccam... restarting"
echo && date >> /var/log/CCcam.check
CCcam
fi
Does anyone know why I am receiving this error?

Bash Script error using grep

I have the following script
var1=$(pwd)
echo $var1
if [ -e $var1 ]
then
echo present directory exists
if [ grep ^d\* | $(ls -al) ]
then
echo these are the directories in $var1
fi
else
echo failed
fi
Running this script gives the following output with errors
/home/user1/Desktop/workspace
present directory exists
ifelse.sh: 6: [: missing ]
ifelse.sh: 6: ifelse.sh: total: not found
Please explain the errors.
If possible give a resource where I can learn about about Bash script errors completely. Thanq
You have a syntax error in your grep.
You must interpolate (execute) the grep and pipe then evaluate the exit code of that command overall:
if [ $(grep ^d\* | $(ls -al)) ]

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.

Resources