Mistake in while loop? bash script - linux

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

Related

Need to verify every file passed as an argument on the command line exists using a shell script

I am looking to create a shell script that reads command line arguments, then concatenates the contents of those files and print it to stdout. I need to verify the files passed to the command line exist.
I have written some code so far, but the script only works if only one command line argument is passed. If passing more than one argument, the error checking I have tried does not work.
#!/bin/bash
if [ $# -eq 0 ]; then
echo -e "Usage: concat FILE ... \nDescription: Concatenates FILE(s)
to standard output separating them with divider -----."
exit 1
fi
for var in "$#"
do
if [[ ! -e $# ]]; then
echo "One or more files does not exist"
exit 1
fi
done
for var in "$#"
do
if [ -f $var ]; then
cat $var
echo "-----"
exit 0
fi
done
I need to fix the error checking on this so that every command line argument is checked to be an existing file. If a file does not exist, the error must be printed to stderr and nothing should be printed to stdout.
You have a bug in line 11:
if [[ ! -e $# ]]; then
You do need to check for a given file here using $var like that:
if [[ ! -e "$var" ]]; then
And you exit prematurely in line 23 - you will always print only a
single file. And remember to always quote your variable because
otherwise your script would not run correctly on files that have a whitespaces in the name, for example:
$ echo a line > 'a b'
$ cat 'a b'
a line
$ ./concat.sh 'a b'
cat: a: No such file or directory
cat: b: No such file or directory
-----.
You said:
if a file does not exist, the error must be printed to stderr and
nothing should be printed to stdout.
You aren't printing anything to stderr at the moment, if you want to
you should do:
echo ... >&2
And you should use printf instead of echo as it's more portable
even though you're using Bash.
All in all, your script could look like this:
#!/bin/bash
if [ $# -eq 0 ]; then
printf "Usage: concat FILE ... \nDescription: Concatenates FILE(s) to standard output separating them with divider -----.\n" >&2
exit 1
fi
for var in "$#"
do
if [[ ! -e "$var" ]]; then
printf "One or more files does not exist\n" >&2
exit 1
fi
done
for var in "$#"
do
if [ -f "$var" ]; then
cat "$var"
printf -- "-----\n"
fi
done
exit 0

What can I do to fix an error inside of case?

My code is:
-cname)
changeName="{2}"
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r $changeName
echo "What would you like to change the name to?"
read -r changeNewName
if ! [ -z "${3}" ]; then
echo "The name has to have no spaces."
exit 1
fi
if [ -f /usr/share/cnote/notes/$changeNewName ]; then
echo "That note name is already used. Please choose a new one."
exit 1
fi
cp "/usr/share/cnote/notes/${changeName}" "/usr/share/cnote/notes/${changeNewName}"
;;
If I remove this part of my case statement it works again but with that part I get that error:
root#minibian:~/cnote# ./cnote -cname
./cnote: line 73: syntax error near unexpected token ;;'
./cnote: line 73: ;;'
Your first if block is not closed. and you should not put the $ symbol ahead of changeName. So the first part must be
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r changeName
echo "What would you like to change the name to?"
read -r changeNewName
fi

Linux script errors- syntax error near unexpected token fi

I have been working on this script for hours trying to find out why it doesn't run,
it keeps spitting out :
"program.sh: line 23: syntax error near unexpected tokenfi'
program.sh: line 23:fi
here is a copy of the script :
#!/bin/bash
#this is the program men
if [ $CHOICE = "1" ]; then
echo "removing old backup folder"
rm -rf ./AllBackUp
fi
echo "Backing up all files to ./Allbackup"
cp $PWD/* $PWD/AllBackUp
elif [ $CHOICE = "2" ]; then
if [ -d SelectBackup ];
rm -rf ./SelectBackup
fi
for f in $PWD; do
cp $PWD/$PATTERN $PWD/SelectBackup
done
help, I'm in a hole here!
Your second if statement is missing a then, so the fi is, indeed, unexpected. (Bash is still expecting a then.)

How to write shell script?

I want to write a shell that runs until something is written to a file (by another process). I have written this:
PID_FILE=log.txt
DONE=0
while [$DONE -eq 0]
do
cat $PID_FILE | while read LINE
do
if [$LINE -neq ""]; then
echo "Do stuff here"
$DONE=1
fi
done
done
echo "DONE"
echo "">$PID_FILE
but I get
test.sh: 3: test.sh: [0: not found
DONE
This line:
while [$DONE -eq 0]
Needs spaces around the square brackets:
while [ $DONE -eq 0 ]
As does this one:
if [$LINE -neq ""]; then
Like this:
if [ $LINE -neq "" ]; then
It helps when you know that \[ is a command. See Why should be there a space after '[' and before ']' in the Bash Script for an explanation.

/etc/init.d/openibd: line 147: syntax error near unexpected token `;&'

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.

Resources