If condition error-please debug my script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition error-please debug my script
# 1  
Old 05-14-2009
Question 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 :

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

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


Thanks
U
# 2  
Old 05-14-2009
  • 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 Smilie
  • 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.
# 3  
Old 05-14-2009
Quote:
Originally Posted by usha rao
..
Code:
 
...
t=`cat $file|head -1`
server=`cat $file|head -2|tail -1`
...

...
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:

Code:
sed -n 2p <your_file>

tyler_durden
# 4  
Old 05-14-2009
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..????
# 5  
Old 05-14-2009
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.
# 6  
Old 05-14-2009
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

Code:
#!/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??
# 7  
Old 05-14-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Debug script

How can I debug this script? I want to know what it is doing or not doing? #!/bin/bash # # if ; then # Do the thing you want before suspend here echo "we are suspending." > /tmp/systemd_suspend_test.txt elif ; then # Do the thing you want after resume here echo "and we are... (21 Replies)
Discussion started by: drew77
21 Replies

2. Programming

Can't debug: assert error with gdb (no problem without)

I'm sorry if the title is really criptic, but I don't know how to phrase my problem. I know I can't really ask for a solution, and I normally wouldn't but this is really escaping my abilities. Antefacts. I developed a program using the zeromq messaging library. I got to a point where the... (11 Replies)
Discussion started by: erupter
11 Replies

3. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Shell Programming and Scripting

Debug perl script

Hi, I am trying to get squid up and running using a redirector process, and every time I try to load a web page, squid fails miserably. Can some one with perl and squid knowledge take a look at these codes and tell if something is wrong here. #!/usr/bin/perl # $| = 1; @endings = qw/... (0 Replies)
Discussion started by: jamie_123
0 Replies

5. Shell Programming and Scripting

Error while using sqlplus command inside 'if' condition in an unix shell script

Hi all, I am using the below given sqlplus command in my unix script to invoke a stored procedure which returns a value .It works fine. RET_CODE=$(/opt/oracle/product/10.2.0.4.CL/bin/sqlplus -S $USER/$PASSWD@$DB_NAME <<EOF EXEC MY_PKG.MY_SP (:COUNT); PRINT COUNT; commit; ... (6 Replies)
Discussion started by: Shri123
6 Replies

6. Shell Programming and Scripting

Cant debug shell script

Hi I am relatively new in shell scripting Below is the code which i developed but for some reason, it keeps giving me error: /apps/bss/BatchProg/at1/batch/scripts/ksh/TBATLC02.ksh: syntax error at line 41 : `then' unmatched #!/usr/bin/ksh... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

7. Shell Programming and Scripting

Help with debug the script

Hi, I have this script, searches and sets variables, then searches and sets more variables from multiple files. I'd need to debug it a bit. #!/bin/bash egrep $1 `find | grep MAGT` >/tmp/resRA-$$ thread=`sed -n '/{0x/ {s/^.*{0x\(*\).*/\1/p;q}' /tmp/resRA-$$` tag=`sed -n '/Tag=/... (5 Replies)
Discussion started by: Vitoriung
5 Replies

8. Shell Programming and Scripting

Problem with the script, help me debug

Hi, When i run the script ./script.sh sun, this give me no output, it should give me the list of file. If i run the script without the argument it should send me echo inside usage(). What is the problem? please help -Adsi #!/bin/sh ROOT_PATH=/net/icebox/vol/local_images/spins... (2 Replies)
Discussion started by: asirohi
2 Replies

9. Shell Programming and Scripting

debug this script

echo "input time in hhmmss" read $st h=`echo $st | cut -c1-2` min=`echo $st | cut -c3-4` s=`echo $st | cut -c5-6` echo "input time in hhmmss" read $end h1=`echo $end | cut -c1-2` min1=`echo $end | cut -c3-4` s1=`echo $end | cut -c5-6` x= `expr $h /* 60 + $min` y= `expr $h1 /* 60 +... (8 Replies)
Discussion started by: abhishek27
8 Replies

10. Shell Programming and Scripting

Debug an Awk Script

I would like to extract the following fields from the text file attached. I copied the contents from a pdf file and pasted them into the text file so I can use awk to extract them. The layout is as listed below. name1,name2,name3,name4,Title,designation,nationality,dob, national ... (1 Reply)
Discussion started by: mboro
1 Replies
Login or Register to Ask a Question