AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
 View Only
  • 1.  how to write tranfer file script?

    Posted Mon September 08, 2008 11:42 PM

    Originally posted by: chandru0078


    Dear Gurus

    Can anyone help me to enhance below script so its more user friendly.

    Actually user azura want to move a file name HRMS.txt from her home directory to
    /home/elsag/cobol/RBS/DATASET/ .below is the current picture

    1:user will transfer the HRMS.txt file every end of the month
    2:she will execute trasnfer.sh script from her home directory
    3:last month HRMS.txt file under /home/elsag/cobol/RBS/DATASET/HRMS.txt will be rename with today date&time
    4:current month HRMS.txt file will be move from /home/azura to /home/elsag/cobol/RBS/DATASET/

    The problem is user sometime executing script twice which makes current HRMS.txt file which already moved by user renamed with today time.

    So i want a script that can tell user the file already moved to /home/elsag/cobol/RBS/DATASET/ successfully and if she execute the file again must able to prompt her there is no HRMS.txt file found in home direcotry and should abort the mv process so she not renaming again currently move file.
    currently used script

    #!/usr/bin/ksh
    echo "-------------------" >> /home/azura/hrms.log
    date >> /home/azura/hrms.log
    echo "-------------------" >> /home/azura/hrms.log

    echo "FILE BEFORE TRANSFERED" >> /home/azura/hrms.log
    ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log
    sudo chown elsag:staff /home/azura/HRMS.txt >> /home/azura/hrms.log
    echo " FILE OWNERSHIP HAS BEEN CHANGED TO ELSAG:ELSAGRP " >> /home/azura/hrms.log
    ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log

    echo "RENAME OLD HRMS FILE" >> /home/azura/hrms.log
    sudo mv /home/elsag/cobol/RBS/DATASET/HRMS.txt /home/elsag/cobol/RBS/DATASET/HRMS.txt_`date +%d%m%Y_%H:%M:%S` >> /home/azura/hrms.log
    echo "MOVING NEW HRMS.txt TO /home/elsag/cobol/RBS/DATASET " >> /home/azura/hrms.log
    sudo mv /home/azura/HRMS.txt /home/elsag/cobol/RBS/DATASET >> /home/azura/hrms.log
    echo "FILE SUCCESSFULLY MOVED" >> /home/azura/hrms.log
    #AIX-Forum


  • 2.  Re: how to write tranfer file script?

    Posted Thu September 11, 2008 01:55 AM

    Originally posted by: SystemAdmin


    
    #!/usr/bin/ksh 
    
    if [[ -f /home/azura/hrms.txt ]]; then echo 
    "-------------------" >> /home/azura/hrms.log date >> /home/azura/hrms.log echo 
    "-------------------" >> /home/azura/hrms.log   echo 
    "FILE BEFORE TRANSFERED" >> /home/azura/hrms.log ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log sudo chown elsag:staff /home/azura/HRMS.txt >> /home/azura/hrms.log echo 
    " FILE OWNERSHIP HAS BEEN CHANGED TO ELSAG:ELSAGRP " >> /home/azura/hrms.log ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log   echo 
    "RENAME OLD HRMS FILE" >> /home/azura/hrms.log sudo  mv /home/elsag/cobol/RBS/DATASET/HRMS.txt /home/elsag/cobol/RBS/DATASET/HRMS.txt_`date +%d%m%Y_%H:%M:%S` >> /home/azura/hrms.log echo 
    "MOVING NEW HRMS.txt TO /home/elsag/cobol/RBS/DATASET " > /home/azura/hrms.log sudo mv /home/azura/HRMS.txt /home/elsag/cobol/RBS/DATASET >> /home/azura/hrms.log echo 
    "FILE SUCCESSFULLY MOVED" >> /home/azura/hrms.log 
    
    else print 
    "No text file found: /home/azura/hrms.txt" | tee -a /home/azura/hrms.log fi
    

    #AIX-Forum


  • 3.  Re: how to write tranfer file script?

    Posted Thu September 11, 2008 12:37 PM

    Originally posted by: SystemAdmin


    i think this will help you.. try this..

    #!/usr/bin/ksh
    echo "-------------------" >> /home/azura/hrms.log
    date >> /home/azura/hrms.log
    echo "-------------------" >> /home/azura/hrms.log

    if http:// -f /home/elsag/cobol/RBS/DATASET/HRMS.txt
    then

    • get the old file date and compare with today date, if it is less than 30 days.
    send some warning or error message..

    else
    • you can run your script*

    echo "FILE BEFORE TRANSFERED" >> /home/azura/hrms.log
    ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log
    sudo chown elsag:staff /home/azura/HRMS.txt >> /home/azura/hrms.log
    echo " FILE OWNERSHIP HAS BEEN CHANGED TO ELSAG:ELSAGRP " >> /home/azura/hrms.log
    ls -l /home/azura/HRMS.txt >> /home/azura/hrms.log

    echo "RENAME OLD HRMS FILE" >> /home/azura/hrms.log
    sudo mv /home/elsag/cobol/RBS/DATASET/HRMS.txt /home/elsag/cobol/RBS/DATASET/HRMS.txt_`date +%d%m%Y_%H:%M:%S` >> /home/azura/hrms.log
    echo "MOVING NEW HRMS.txt TO /home/elsag/cobol/RBS/DATASET " >> /home/azura/hrms.log
    sudo mv /home/azura/HRMS.txt /home/elsag/cobol/RBS/DATASET >> /home/azura/hrms.log
    echo "FILE SUCCESSFULLY MOVED" >> /home/azura/hrms.log

    fi
    #AIX-Forum


  • 4.  Re: how to write tranfer file script?

    Posted Mon September 15, 2008 04:01 PM

    Originally posted by: nagger


    Other improvement:

    Add as a second line
    LOG=/home/azura/hrms.log

    and then change every
    >> /home/azura/hrms.log
    to
    >>$LOG

    That will help readability, reduce the chance of typos and make changing the LOG file easier.
    Actually you repeat the files names /home/elsag/cobol/RBS/DATASET/HRMS.txt and /home/azura/HRMS.txt again and again and again.
    Perhaps they could be a variable too!

    I assume you don't want error messages in the log file so problem become a mystery :-)
    If not add a 2>> filename before each ">> filename" so both error and standard out go into the log.

    Actually, a ksh function would help:

    log()
    {
    $* 2>>/home/azura/hrms.log >> /home/azura/hrms.log
    }

    then change the script to
    log echo
    log date
    log echo FILE BEFORE TRANSFERED
    log ls -l /home/azura/HRMS.txt
    log sudo chown elsag:staff /home/azura/HRMS.txt

    etc.
    This makes it much more understandable
    and stop all that echo after some commands - the log will tell you when it failed your don't need to check.

    You do realise you
    chown elsag:staff
    but output claiming to " FILE OWNERSHIP HAS BEEN CHANGED TO ELSAG:ELSAGRP "
    Which is wrong in the upper/lower case AND wrong the group is different staff != elsagrp

    So how about:

    #!/usr/bin/ksh
    LOG=/home/azura/hrms.log
    SOURCE=/home/azura/HRMS.txt
    TARGET=/home/elsag/cobol/RBS/DATASET/HRMS.txt

    log()
    {
    $* 1>>/home/azura/hrms.log 2>>/home/azura/hrms.log
    }

    log echo "-------------------"
    log date

    if -f $TARGET
    then
    echo The file $TARGET already exists or some message like this outputs on the screen

    else
    log ls -l $SOURCE
    log sudo chown elsag:staff $SOURCE
    log sudo mv $TARGET $(TARGET)_`date +%d%m%Y_%H:%M:%S`
    log sudo mv $SOURCE $TARGET
    echo The file $TARGET has been saved
    fi
    Hope this helps, N
    #AIX-Forum


  • 5.  Re: how to write tranfer file script?

    Posted Thu September 11, 2008 12:43 PM

    Originally posted by: SystemAdmin


    sorry man some type mistakes..

    that one was if condition with

    if -f filename
    nit http..

    thanks
    #AIX-Forum


  • 6.  Re: how to write tranfer file script?

    Posted Fri September 26, 2008 12:30 AM

    Originally posted by: chandru0078


    Dear All

    Thank you very much for the GREAT feedback.
    #AIX-Forum