Script problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script problems
# 1  
Old 03-30-2009
Script problems

Hi Can anybody please explain how the below script works and if there is any problems with it? The script is part of an archival process but it keeps crashing out.

Code:
#!/sbin/sh -
clear
purgelist="/var/opt/moto/live/scripts/old_messages_purge_list"
for i in `cat $purgelist`
do 
    filepath=`echo $i|sed 's/^[   ]*\([^=  ]*\)[=    ].*$/\1/g'`
    filedays=`echo $i|sed 's/^[^=]*=\(.*\)[|   ].*$/\1/'`    
    ext=`echo $i|sed 's/^[^|]*|\(.*\)$/\1/'`
 find $filepath   -name '*'$ext  -mtime $filedays -print -exec rm {} \; 
done

old_messages_purge_list contains the following:

Code:
/var/opt/home/adm/file_processor/FAL/SNDR/archive/=+90|.arc
/var/opt/home/adm/file_processor/FAL/SNDR/ack/=+90|.ctl
/var/opt/home/adm/file_processor/FAL/RECVR/archive/=+90|.out
/var/opt/home/adm/file_processor/FAL/RECVR/ack/=+90|.ctl


Last edited by Yogesh Sawant; 03-30-2009 at 11:31 AM.. Reason: added code tags
# 2  
Old 03-30-2009
The main problem here is the pipe character "|" towards the end of each string. This character is wrecking the syntax of your expanded "for" line and each echo statement because shell is interpreting it.
We can use unix "dirname" and "basename" commands to break the long line into the directory name and the trailing string. Then extracting the two parameters can be simplified by changing the pipe character "|" to an equals sign "=" to create a common delimiter for each field.
To handle a string containing awkward characters such as pipe, we must keep it between double quotes characters and avoid expansion by shell.
For example replacing the "for" loop with a "while" loop:

Code:
#!/sbin/sh
clear
purgelist="/var/opt/moto/live/scripts/old_messages_purge_list"
cat ${purgelist} | while read line
do 
        filepath="`dirname ${line}`"
        # Extract string containing ext and filedays
        # Change pipe character to = character to make common delimiter
        work=`basename "${line}"|sed -e "s/\|/=/g"`
        filedays=`echo "${work}"|cut -f2 -d=`
        ext=`echo "${work}"|cut -f3 -d=`
        echo "filepath=${filepath}"
        echo "ext=${ext}"
        echo "filedays=${filedays}"
done

# 3  
Old 03-30-2009
Quote:
Originally Posted by runnerpaul
Hi Can anybody please explain how the below script works and if there is any problems with it? The script is part of an archival process but it keeps crashing out.

What happens, exactly? Do you get any error messages?
Quote:
Code:
#!/sbin/sh -
clear
purgelist="/var/opt/moto/live/scripts/old_messages_purge_list"
for i in `cat $purgelist`


Not only is cat unnecessary, but it will break your script is any lines contain spaces.

Use redirection:

Code:
while IFS= read -r i
do
  : ...
done < "$purgelist"

Quote:
Code:
do 
    filepath=`echo $i|sed 's/^[   ]*\([^=  ]*\)[=    ].*$/\1/g'`


You don't need an external command (sed) for that:

Code:
filepath=${i%/*}/

Quote:
Code:
    filedays=`echo $i|sed 's/^[^=]*=\(.*\)[|   ].*$/\1/'`


Or for that:

Code:
temp=${i##*=}
filedays=${temp%%|*}

Quote:
Code:
    ext=`echo $i|sed 's/^[^|]*|\(.*\)$/\1/'`


Or that.
Quote:
Code:
 find $filepath   -name '*'$ext  -mtime $filedays -print -exec rm {} \; 
done

old_messages_purge_list contains the following:

Code:
/var/opt/home/adm/file_processor/FAL/SNDR/archive/=+90|.arc
/var/opt/home/adm/file_processor/FAL/SNDR/ack/=+90|.ctl
/var/opt/home/adm/file_processor/FAL/RECVR/archive/=+90|.out
/var/opt/home/adm/file_processor/FAL/RECVR/ack/=+90|.ctl

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Problems

Hi, I'm newbie in the shell script world and i want to solve some problems I am experiencing. My main goal is to create a ksh script to use 2 text files as input to execute a local shell script(create user commands in the criar.users.aix file) through a ssh connection in which the list of... (1 Reply)
Discussion started by: joaochambino
1 Replies

2. Homework & Coursework Questions

Problems executing script

Hi, I'm a complete novice to Linux and UNIX. I'm having trouble getting a script to execute properly. I did a similar script earlier in the semester with no problems. For whatever reason I can't get this one to work. Any help would be greatly appreciated as I'm completely lost and frustrated at... (5 Replies)
Discussion started by: Lindy70
5 Replies

3. Shell Programming and Scripting

shell script to call perl script problems

Ok, don't ask me why, but all calls to perl must be called by a shell script. Its really not ideal, but its what I have to work with. Calling it isnt the issue, its passing in the arguments. I have about 1000 perl scripts to call by a shell script. Right now, I'm executing the shell script... (3 Replies)
Discussion started by: regexnub
3 Replies

4. UNIX for Dummies Questions & Answers

Problems with script and Cron

Hi Guys, total newbie here, Ive read through some of the threads about cron and scripts not working and have still drawn a blank as to why mine isnt working correctly. I have a script that runs the ausearch with a set of criteria i have setup, the only access i have to the system is via... (5 Replies)
Discussion started by: richie190784
5 Replies

5. Shell Programming and Scripting

two script problems

i have two problems with my phone book script the first i want to only allow a valid name if it has letters and spaces not a number in the name. so "joe smith" is ok "joe smith1" is not ok. the second problem is how can i cut a name out of a txt file i have this as my test txt file joe blogs... (3 Replies)
Discussion started by: zappedback
3 Replies

6. Shell Programming and Scripting

Shell script problems to do

Does anyone know a good site to do shell script problems? (0 Replies)
Discussion started by: cleansing_flame
0 Replies

7. Shell Programming and Scripting

Problems with an if/then loop within a script

Hi there, I have written a script to clear out log files from the var/tmp dir. It works up to a point. What I needed to do was to exit the script if there was no files to be deleted. I can get this working on a test script but when I implement it into my program it errors out with a `then` not... (3 Replies)
Discussion started by: lodey
3 Replies

8. UNIX for Dummies Questions & Answers

script problems

Hi, Here is an example of a problem I have: File2: contain the following lines: a^ aaa^aa aa^~ b^ bbb^bb bb^~ c^ ccc^cc cc^~ d^ dddd^dd dd^~ File1: contain the following lines: b^ bbb^bb bb^~ c^ ccc^cc cc^~ I get File2 as input and I want to do as following: for each line in... (3 Replies)
Discussion started by: hellsd
3 Replies

9. UNIX for Dummies Questions & Answers

Copy Script Problems .....

I got this script: print -n "Enter file name: " read name .... ..... ..... etc but at the prmpt, when I press enter (without typin a word), comes up with sum error message, is there away of getting it not to print that message?? (8 Replies)
Discussion started by: Makaveli.2003
8 Replies

10. UNIX for Dummies Questions & Answers

having ksh script problems

well i have written a script to telnet and ftp to all my servers, the script runs great, BUT i can not for the life of me figure out how to get the script to repeat if the conditions are not filled. this is what i have so far ######################################### TorF(){ echo T... (4 Replies)
Discussion started by: jerzey4life
4 Replies
Login or Register to Ask a Question