I am very new to Linux/Asterisk. I am trying to write a script but, when I execute it, I see the error shown below.
The script is as follows:
#!/bin/bash
asteriskbin=`which asterisk`
interval=10
trunk=”<test>”
run=true
while [[ "$run" == "true" ]]; do
checktrunk=`$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK`
if [[ $checktrunk == 0 ]]; then
echo “<TEST Trunk Down>”
else
echo “SIP trunk registration OK.”
fi
sleep $interval
exit 1
Debug error is as following:
bash -x trunks.sh
++ which asterisk
+ asteriskbin=/usr/sbin/asterisk
+ interval=10
+ trunk=$'\342\200\235test\342\200\235'
+ run=true
trunks.sh: line 15: syntax error: unexpected end of file
This might be a duplicate post but, from my search, the given answers such as chmod 755 script.sh, exit 0, exit 1, or dos2unix script.sh did not work.
Aren't you missing a done to terminate the while block? I assume a line before exit 1.
Related
The parallel execution is working properly as expected. But i need to get the states of each file. Here I need to check it by using the if condition. This is just a sample and i have many scenarios like this in my code.
I'm a beginner at #help shell scripts & linux but actually i love linux now.
I'm expecting an alternative method or a help to resolve this. Thank you everyone.
( echo $FILE_PATH/import_csv_location_data_into_hive.sh; echo $FILE_PATH/import_csv_order_data_into_hive.sh; echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh; ) | parallel bash
wait
IMPORTED_LOCATIONS_STATUS=$? IMPORTED_ORDERS_STATUS=$? IMPORTED_ORDERS_WITH_DELIVERY_INFO_STATUS=$?
if [[ "$IMPORTED_LOCATIONS_STATUS" == "0" && "$IMPORTED_ORDERS_STATUS" == "0" && "$IMPORTED_ORDERS_WITH_DELIVERY_INFO_STATUS" == "0" ]];
then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
Sample code
enter image description here
(echo Your-Special-Header; \
echo whoami; \
echo cat nothere \
) | parallel --header : --results all.csv bash -c
The file nothere does not exist. Here is the csv output:
Seq,Host,Starttime,JobRuntime,Send,Receive,Exitval,Signal,Command,Your-Special-Header,Stdout,Stderr
1,:,1662823208.481,0.001,0,5,0,0,"bash -c whoami",whoami,"root
",
2,:,1662823208.481,0.003,0,0,1,0,"bash -c cat\ nothere","cat nothere",,"cat: nothere: No such file or directory
"
The 7th column is Exitval.
If you only need to know if all finished with no errors:
(
echo $FILE_PATH/import_csv_location_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh;
) | parallel bash
if [[ "$?" == "0" ]]; then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
Shorter, but less readable IMO:
(
echo $FILE_PATH/import_csv_location_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh;
) | if parallel bash ; then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
I have tried all the solutions available on stack overflow, but when I use if condition with with it always results true.
I need to find a line in the file and see if it doesn't exit then insert the line in that file, but it always results that the line already exists.
Here is my script
isInFile=$(grep -q '^export' /etc/bashrc)
if [[ $isInFile == 0 ]];
then
echo "line is not present";
echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
source /etc/bashrc;
else
echo "line is in the file";
fi
It always says that
line is in the file
The if statement branches based on the exit status of the command it's given. [[ is just one command you can use, it's not mandatory syntax. At an interactive prompt, enter help if
Do this:
if grep -q '^export' /etc/bashrc
then
# exit status of grep is zero: the pattern DOES MATCH the file
echo "line is in the file";
else
# exit status of grep is non-zero: the pattern DOES NOT MATCH the file
echo "line is not present";
echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
source /etc/bashrc;
fi
I see 2 issues in your code:
if [[ $isInFile == 0 ]]; --If condition should not terminate with ;. Remove that.
The expression you are checking is always an empty string. Try echo $isInFile. What you are checking is output of the command, not its return value. Instead, you should remove -q from your grep expression and check if the output is empty or not.
Following code should work:
isInFile=$(grep '^export' /etc/bashrc)
if [ -z "$isInFile" ]
then
echo "line is not present";
echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
source /etc/bashrc;
else
echo "line is in the file";
fi
-z check for emptiness of variable.
I have 2 scripts . I'm invoking one script from the other for capturing the exit status.
Import.sh
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
temp.sh
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
schema_remap_AANV02_UAT2.txt
AANVPXE;Arju4578;PANVPXE
AANVSL;Arj0098;PANVSL
AANV;Arju1345;PANV
the .txt file does not have read permission(make sure that you do not give read permission), so the script should fail by returning the exit status as exit $? .
Below is the output after i run temp.sh
+ cat schema_remap_AANV02_UAT2.txt
+ egrep -w 'PANV|PANVPXE'
cat: schema_remap_AANV02_UAT2.txt: Permission denied
+ exit 1
Return code =
Internal scripts is exiting with exit 1(since cat command is failing) , but inside temp.sh i'm not getting the expected value while capturing the return code.
I want to make sure that whichever command fails in import.sh , the script should return with appropriate exit status.
To get the exit code of your script Import.sh instead of its output, change the script temp.sh to
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
RC=$?
echo "Return code = $RC"
or simply
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
echo "Return code = $?"
See the comments for hints how to fix/improve your scripts.
I tried to understand the way you invoke your child script ( Import ) into the parent script ( temp.sh ). Well let me show you what is happening
Import Script
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
This script will exit with something different than 0 when a problem with the grep occurs, so if the pattern you are looking for it is not there, it will fail.
$ echo "hello" | egrep -i "bye"
$ echo $?
1
Then you are running this script from other program
Launcher
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
Here is where you have the problem. You are calling the script like it was a function and expecting a result. The variable RC is getting whatever output your script is sending to the STDOUT , nothing else. RC will be always empty, because your script does not send anything to the STDOUT. So, you must understand the difference between getting the result from the child and evaluating what return code produced the child program.
Let me show you an example of what I just explained to you using my own scripts. I have two scripts: the child.sh is just a sqlplus to Oracle. the parent invokes the child the same way you do.
$ more child.sh
#/bin/bash
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" << eof
whenever sqlerror exit failure;
select * from dual ;
eof
if [[ $? -eq 0 ]];
then
exit 0;
else
exit 99;
fi
$ more parent.sh
#!/bin/bash
# run child
var=`/orabatch/ftpcpl/log/child.sh`
echo $var
$ ./child.sh
D
-
X
$ echo $?
0
$ ./parent.sh
D - X
$ echo $?
0
As you see, my parent is getting whatever the child script is sending to the STDOUT. Now let's force an error in the child script to verify that my parent script is still exiting as ok:
$ ./child.sh
select * from dual0
*
ERROR at line 1:
ORA-00942: table or view does not exist
$ ./parent.sh
ERROR at line 1:
ORA-00942: table or view does not exist
$ echo $?
0
As you can see, the output of my operation is the error in the first, however not as an error, but as an output. my parent has ended ok, even you can see that there was an error.
I would rewrite the script as follows:
#/bin/bash
my_path=/app/arjun/scripts
$my_path/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
result=$?
if [ ${result} -ne 0 ];
then
echo "error"
exit 2;
fi
My Code:
#!/bin/bash
rm screenlog.0
screen -X stuff 'X21'$(printf \\r)
while :
do
grep -i "T" $screenlog.0
if [ $? -eq 0 ];
then
FILE=/etc/passwd
VAR=`head -n 1 $FILE`
echo $VAR
rm screenlog.0
break
done
This script is to delete the file "screenlog.0" send a command (X21) to an screen interface.
Thats the first part and it works.
The second Part is the Problem:
That should test the content of "screenlog.0", is there an something with a "T" inside save the contant into a variable.
The error:
line 11: syntax error near unexpected token `done'
line 11: `done'
To the "screen": Its an screen of an usb device that recive radio messages like this:
T350B00A66E2
H34D04DE4254
The script have to scan for the incomming messages with "T" at the beginning (The first letter is a Type field behind this a hex code.
Some ideas to correct or other solutions?
I corrected my code a bit:
#!/bin/bash
>screenlog.0
screen -X stuff 'X21'$(printf \\r)
while :
do
sleep 2
grep -i "T" $screenlog.0
if [ $? -eq 0 ];
then
screenlog.0=/etc/passwd
VAR=`head -n 1 $screenlog.0`
echo $VAR
break
fi
done
The new error is:
grep: .0: No such file or directory
All 5 seconds....
The file screenlog.0 exist .. :(
oh...you missed fi in your script :). Like syntax as follows if [ condition ];then #dosomething fi
For your script
if [ $? -eq 0 ];then
FILE=/etc/passwd
VAR=`head -n 1 $FILE`
echo $VAR
rm screenlog.0
break
fi
Hi I am trying to install a fairly lengthy script to install infiniband and the OFED stack on rocks cluster 6.0
here is what i try to run
user#cluster # /etc/init.d/openibd restart
/etc/init.d/openibd: line 147: syntax error near unexpected token `;&'
/etc/init.d/openibd: line 147: `if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then'
can any one share with me a fix or can identify a way to fix the error in this script?
in the file /etc/init.d/openibd
here is the part of the script which contains the error on the indicated line.
CONFIG="/etc/infiniband/openib.conf"
if [ ! -f $CONFIG ]; then
echo No InfiniBand configuration found
exit 0
fi
. $CONFIG
CWD=`pwd`
cd /etc/infiniband
WD=`pwd`
PATH=$PATH:/sbin:/usr/bin
if [ -e /etc/profile.d/ofed.sh ]; then
. /etc/profile.d/ofed.sh
fi
# Only use ONBOOT option if called by a runlevel directory.
# Therefore determine the base, follow a runlevel link name ...
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
# ... and compare them
if [ $link == $base ] ; then
RUNMODE=manual
ONBOOT=yes
else
RUNMODE=auto
fi
ACTION=$1
shift
RESTART=0
max_ports_num_in_hca=0
# Check if OpenIB configured to start automatically
if [ "X${ONBOOT}" != "Xyes" ]; then
exit 0
fi
### ERROR ON FOLLOWING LINE ###
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
if [ -n "$INIT_VERSION" ] ; then
# MODE=onboot
if LANG=C egrep -L "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ${CONFIG} > /dev/null
; then
exit 0
fi
fi
fi
You've got some HTML encoding going on their you need to fix.
Replace > with >, and replace & with &.
Your script somehow had all of its > replaced with > (and & replaced by &, etc)
if ( grep -i 'SuSE Linux' /etc/issue >/dev/null 2>&1 ); then
^^
This is a syntax error because there is no command between the semi-colon that terminates the preceding command and the ampersand. The HTML encoding of certain symbols is confusing the bash parser as a result.