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

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?

Related

How can i run a curl command in bash without syntax errors?

I am running the following in bash:
read -p "Server: " SERVER
read -p "Username: " NAME
read -p "UserPassword: " USERPASSWORD
curl -s -u ${NAME}:${USERPASSWORD} http://<REMOTE_SERVER_DETAILS>
if [ $http_response != "200" ] then
echo "Error"
else
echo "Created Successfully."
fi
The curl command runs successfully . All i want to output to the user is the status , ie the http response header , but it gives an error
syntax error near unexpected token `else'
Any ideas as to why this is giving syntax error?
Also can i output the error ?
Two solutions: extra semicolon or line break afer ].
if [ "$http_response" != "200" ]; then
if [ "$http_response" != "200" ]
then

looping until the output of a command produces an stderr

Normally, in bash, if I wanted to loop until a command produced a certain output I'd do this:
while : ; do
status=$(mycommand)
[ "$status" = "whatever" ] && break
printf "."
sleep 10
done
But how would I do that I wanted to loop until I got stderr output? I thought maybe I could do this:
while : ; do
status=$(mycommand 2>&1)
[ "$status" = "whatever" ] && break
printf "."
sleep 10
done
But when I do that I get this error:
-bash: syntax error near unexpected token `2' error.
Any ideas?

bash. Grep text file looking for lines from other file [duplicate]

This question already has answers here:
Intersection of files
(4 answers)
Closed 3 years ago.
Is it possible to analize text file let's say log.txt looking for lines from the other file - let's say config.txt. I tried to do following thing:
while read -r line;
do
if grep -q $line $log_path; then
echo "Found line: $line" >> $out_log
break
else echo "Line not found: $line" >> $out_log
fi
done < $conf_path
$conf_path is a path like /path/to/config.txt
$log_path is a path like /path/to/log.txt
and the $out_log is the /path/to/output_log.txt which is used to test if script works properly.
Example $line is "ERROR: MESSAGE: \[Library not found \!\]". I tested grep instruction outside the script and it found that line properly, so I do not think that grep is the problem here. Every time else statement is executed.
Thank you, Community, for any suggestions,
Best regards, Max
Try below
Code:
while read -r line
do if grep -q "${line}" $log_path
then echo "Found line: $line" >> $out_log
else echo "Line not found: $line" >> $out_log
fi
done < $conf_path
Example:
bash-3.2$ echo $conf_path ; cat $conf_path
config.txt
ERROR: MESSAGE: \[Library not found \!\]
ERROR: MESSAGE: \[File not found \!\]
ERROR: MESSAGE: \[Path not found \!\]
ERROR: MESSAGE: \[Permission denied \!\]
bash-3.2$
bash-3.2$ echo $log_path; cat $log_path
log.txt
INFO: MESSAGE: Script Starts
INFO: MESSAGE: Some command output
ERROR: MESSAGE: [Library not found !]
INFO: MESSAGE: Some other command output
ERROR: MESSAGE: [Path not found !]
INFO: MESSAGE: Exit script with 2
bash-3.2$
bash-3.2$ while read -r line; do if grep -q "${line}" $log_path; then echo "Found line: $line" >> $out_log; else echo "Line not found: $line" >> $out_log; fi; done < $conf_path
bash-3.2$
bash-3.2$ echo $out_log; cat $out_log
output_log.txt
Found line: ERROR: MESSAGE: \[Library not found \!\]
Line not found: ERROR: MESSAGE: \[File not found \!\]
Found line: ERROR: MESSAGE: \[Path not found \!\]
Line not found: ERROR: MESSAGE: \[Permission denied \!\]
bash-3.2$
Hope this helps. Good Luck !!

bash: echo in and also out of program

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.

bash script - unable to evaluate variable to string correctly

Trying to debug my bash script. What's wrong with my syntax here? I'm trying to evaluate a parameter entered by the user and based on that run one of my IF-THEN statements. However, I'm getting a command not found.
Here's my script thus far:
if [[ $# != 4 ]]; then
echo "Usage: ./test.sh <ABC|XYZ> <owner> <db> <TARGETHOST>" 2>&1
exit 1
fi
case $1 in
ABC|XYZ)
filename="get-$1.sql"
;;
*)echo "Must enter ABC or XYZ"
exit 1
;;
esac
export OWNER=$2
export DB=$3
export HOST_NM=$4
export PORT=5432
export LOG="test-$1.log"
PSQL=`which psql`
if [[$1=="ABC"]]; then
RUNCLI=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -F $'\t' --no-align -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
if [[$1=="XYZ"]]; then
RUNCLI2=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -a -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
Error:
./test.sh: line 41: [[XYZ==ABC]]: command not found
Error running report ...
./test.sh: line 51: [[XYZ==XYZ]]: command not found
Error running report ...
Although the question is already answered in the comment section I want to give an answer and share some knowledge which is not obvious (at least it was not for me).
The if in bash just checks the return code of the following command, which means that instead of if [ condition ]... or if [[ condition ]]... you could also write if ./configure && make....
[ and [[ are commands or shell-built-ins, respectively, as well and not part of the if syntax. An which [ for instance returns /bin/[.
At this point it is obvious that you need spaces between the brackets and the condition since it is just just a set of parameters passed to a command.
If you have this in mind, you will never forget the spaces again.

Resources