If condition error-please debug my script

Hi All,
I have a script which is based on calculating current time

The script looks like this:

whenever i try to run the script it gives me error:Please advice wherther i have made mistake with if condition or with for loop??

The code :

 
set -x
#!/usr/bin/ksh
set -x
############# Srcipt to format master file to clreate directories ###############
d0=`date +%Y%m%d.%H%M`
d1=`date +%Y%m%d.%H`
d4=`date +%M`
d2=`echo $d4 1| awk '{print $1-$2}'`
d3=$d1$d2
for d in $d3 $d0
do
for file in `ls -lrt /logs/MMD/appservers/Master*.$d|cut -f18 -d " "|awk -F" " '{print $NF}'`
do
t=`cat $file|head -1`
server=`cat $file|head -2|tail -1`
if [ -e "/logs/MMD/appservers/vmstat/$server/mem_util.$d" ] ; then
exit 1
else
 
 
some code here
 
 
fi
done
done

Also please advice is there something wrong with

 
awk -F" " '{print $NF}'

Thanks
U

  • It would be very helpful if you post the error.
  • The set -x before the shebang should be redundant and can be left out. Shebang should be 1st in your script and afterwards you can tell the shell what to do.
  • There is a typo in your comment: "clreate" should be "create" I guess.
  • For the awk question, the basic field separator is any number of any kind of concatenated spaces. So a single blank should be in most cases just not needed to be specified as field separator. If it produces what you want, it is ok. Can't see any syntax error.
  • Good you use CODE tags but intendation would be nice too :wink:
  • In the lines where you use the variable $d you might want to write ${d} since it's position is close to other characters. For better readability and for protection against neighbouring characters.
  • Things like "cat $file|head -1" are unnecessary since you could just write "head -1 $file" and leave the cat out.

I don't see more atm since it's early in the morning. Maybe fix the one or other thing mentioned above and show the errors you get please.

Ughh... as zaxxon mentioned, let the cat out of the bag.
And if you want to print just the 2nd line of a file, then:

sed -n 2p <your_file>

tyler_durden

Hey thanks zaxxon and tyler.
I will correct the script and let you ppl know.
One doubt
here inside if condition i am using -e means whether the file is previously present or not,right??
correct me if i am wrong.

also i am using double quotes inside condition for filename.is it ok??

i have given exit 1 to break out the current iteration but it exiting the entire progream..????

exit will end the whole script - to break out of the loop use "break" or if you want to move on to the next element use "continue".
If you want substitution, double quotes is ok.
You can test -e easily yourself on the shell. -e evaluates to true ie. "0" if the file exists.

Hey thanks,Zaxxon Got it..!!

With respect to if condition only i need one more help like

i have a script which copies files to server form some IP address

#!/usr/bin/ksh
d=`date +%Y%m%d.%H%M`
d1=`date +%Y%m%d.%H`
d4=`date +%M`
d2=`echo $d4 1| awk '{print $1-$2}'`
d3=$d1$d2
#file=Master.$d
cd /logs/MMD/appservers
HOST='148.171.28.230'
USER='user
PASSWD='password'
ftp -nv <<EOF
open $HOST
user $USER $PASSWD
cd /tmp/MMD/appserver
prompt
mget Master*.$d*
mget Master*.$d3*
done
EOF
/logs/MMD/format_E3_app_Master.ksh

Now it is completely dependant on time.
It will take current minute and previous minute file and run another code inside.

Now if ftp fails we will not get the file on the server.

How to incorporate an if condition that if ftp of at anyone file fails it should transfer again??

ftp will not have a different exit code you could check it it encounters a file transfer error. It will always be a 0 since ftp itself worked fine. You could use verbose output and parse the 3 digit ftp codes etc. but that's somewhat cimbersome.
So maybe it is an option to use scp instead? It would be encrypted and easier to get it's exit codes etc.

Hi zaxxon,
Thanks for letting me know
But can you explain what you have said...verbose output and parse 3 digit ftp codes...Couldn't really get that.

Also how to use scp...
what are necessary conditions like user id/paawd having access to both servers or where we are transferring the files ...having access to just that is enough??