Help with improving korn shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with improving korn shell script
# 1  
Old 12-20-2015
Help with improving korn shell script

I am primarily a SQA/Tester and new to korn shell. How can I improve the following script?
Code:
#/bin/ksh
SourceLocation=~/Scripts/Test/Source
TrackerLocation=~/Scripts/Test/Tracker
TargetLocation=rdbusse@rdbmbp:/Users/rdbusse/Scripts/Test/Target

for file in $(cd $SourceLocation; ls)
do

  echo Step 1: Examine $file
  if [ ! -f $TrackerLocation/$file ];
  then
    echo Step 2: Processing $file
    eval scp -p $SourceLocation/$file $TargetLocation/$file
    ret_code=$?
    if [ $ret_code != 0 ]; then
      printf "Error : [%d] when executing command: $ret_code
      #exit $ret_code
    else
      echo Step 3: Writing $TrackerLocation/$file
      touch $TrackerLocation/$file
    fi
  else
    echo Step 2: $file previously processed
  fi

done


Last edited by Scrutinizer; 12-20-2015 at 07:29 PM.. Reason: CODE tags
# 2  
Old 12-20-2015
Please use code tags as required by forum rules!

When asking to improve something, it would be nice to supply
- the objective (although quite clear in this case)
- the problem(s) (malfunctions, error msgs, performance issues, ...)
# 3  
Old 12-20-2015
Thanks RudiC,

The scripts lists and transfers files from the Source to the Target and then Logs the process states within the Tracker. I have run the script successfully, I am just asking for suggestions in improving the script. How enhance certain statements, or what other commands could do a more efficient operation.
# 4  
Old 12-20-2015
You could try using the rsync utility:

Code:
#/bin/ksh
SourceLocation=~/Scripts/Test/Source
TargetLocation=rdbusse@rdbmbp:Scripts/Test/Target

rsync -av --del $SourceLocation/ $TargetLocation

This syncs all files under $SourceLocation to $TargetLocation (including sub-directories) and also removes any extra files or directories in Target but not in Source.
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 12-20-2015
place of the rsync

Does the rsync utility replace the rest of the code i.e. the loop code?
# 6  
Old 12-20-2015
yes It has a number of options to control how it works and will usually examine the date/time/crc of existing files and only synchronize anything that is different on the target system.

This may be useful if you would like any updates to scripts to also be applied to the target system.
# 7  
Old 12-20-2015
If you just want some improvements to the existing script, I would recommend:

Code:
#!/bin/ksh
SourceLocation=~/Scripts/Test/Source
TrackerLocation=~/Scripts/Test/Tracker
TargetLocation=rdbusse@rdbmbp:/Users/rdbusse/Scripts/Test/Target

for sfile in $SourceLocation/*
do
  [ -f "$sfile" ] || continue
  file=${sfile##*/}

  echo Step 1: Examine $file
  if [ ! -f "$TrackerLocation/$file" ];
  then
    echo Step 2: Processing $file
    scp -p "$SourceLocation/$file" "$TargetLocation/$file"
    ret_code=$?
    if [ $ret_code != 0 ]; then
      printf "Error : [%d] when executing command:" $ret_code
    else
      echo Step 3: Writing $TrackerLocation/$file
      touch "$TrackerLocation/$file"
    fi
  else
    echo Step 2: $file previously processed
  fi

done

Improvements:
Use shell to expand $SourceLocation/* not a sub-shell + ls command.
Ignore sub-directories or other special files within the script directory.
Quoted $file references to cater for white space in file names.
Avoid unnecessary eval call for security reasons.

Last edited by Chubler_XL; 12-20-2015 at 09:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

2. Shell Programming and Scripting

running a script in korn shell

I'm learning bash and have discovered that the shell can only work with integers and not decimals. I'd like to run my scripts in korn to account for this, but just now, when I tried to run my script, I got an error message that said 'no such file or directory,' even though when I'm in the shell... (3 Replies)
Discussion started by: Straitsfan
3 Replies

3. Homework & Coursework Questions

Korn Shell Script

1. The problem statement, all variables and given/known data: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. 2. Relevant commands, code,... (3 Replies)
Discussion started by: burm
3 Replies

4. Shell Programming and Scripting

Korn Shell Script

I have to solve some exercises in Korn Shell, but i'm having some problems. For example: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. I... (3 Replies)
Discussion started by: burm
3 Replies

5. Solaris

Problems with korn shell script

Hey Guys, I'm looking for some advice about a korn shell script I've written. I've spent hours googling for an answer hopefully someone here can help me out. Basically the part of the script I'm having problems with is when I need to SFTP a file from one server to another. The line looks... (6 Replies)
Discussion started by: hilather
6 Replies

6. Shell Programming and Scripting

Korn Shell script not running

I am sorry, this is really trivial, yet I am not able to understand what the problem is! I am using korn shell and running this script #!/bin/ksh keep=3 while ; do echo $keep keep=$(($keep-1)) done I am getting this error: `keep=$' unexpected I am not able to understand it because ... (1 Reply)
Discussion started by: Asty
1 Replies

7. AIX

Help with Korn Shell script

I have this Korn shell script that runs via a cron entry. It runs in a loop "watching" a specific file system for files with a certain name. The file system that it is watching is an upload file system for an FTP server. When files that are the correct name come in, it takes the extension of the... (1 Reply)
Discussion started by: heprox
1 Replies

8. UNIX Desktop Questions & Answers

korn shell script

hi all i am writing the korn shell script. i have a SQL script which gives me the folowing output DSA.WLG.20050713211544.20051025.20050713211544 28991 1130198400 DSA.WLG.20050713211544.20051025.20050713211544 25881 1130198400 DSA.WLG.20050711210100.20051025.20050711210100 25881 ... (3 Replies)
Discussion started by: pavan_test
3 Replies

9. UNIX for Dummies Questions & Answers

korn shell script

hello., i have 2 files.. 1 file is in this folder /home/test/ssk/DSA.WLG.20050713211544.20050710.20050713211544 (this part) other file is in this folder /home/kk/dev/DSA.WLG.20050711210100.20050710.20050711210100 ... (1 Reply)
Discussion started by: pavan_test
1 Replies

10. Shell Programming and Scripting

Multiple su - in Korn Shell script

i am trying to do something like this : #!/bin/ksh # Change to the userid user1 su - user1 #Issue the command to change directory and list files cd /home/user1/ ls -lrt exit #Come out of the user1 to root again #change to user 2 su - user2 cd /home/user2/ ls -lrt... (2 Replies)
Discussion started by: furrari
2 Replies
Login or Register to Ask a Question