Find the answer to your Linux question:
Results 1 to 2 of 2
Hi All: I am a newbie for ksh scripting.My code doesn't work. I am trying to automate the process of file read from a directory. The files older than 3 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    1

    Need help with KSH script

    Hi All:

    I am a newbie for ksh scripting.My code doesn't work.
    I am trying to automate the process of file read from a directory. The files older than 3 days are supposed to be archived. If they are in the three day limit, then they can be processed as a comand line to a C program.

    Here is the script:

    #!/bin/ksh
    checkduration=259200 # 3 days in seconds
    cur_timestamp=`date +%s` #current time in seconds

    cd /home/roab/toc/; # change dir
    for i in /home/roab/toc/*; do
    if [ -f $i ]; then # if the file is there
    filename=${i#/home/roab/toc};# get file name
    last_mod=$(stat -c "%Y" ${i#/home/roab/toc}); # get last mod time of file in seconds last_mod=$(stat -c "%Y" filename)
    #above line gathers file modified time in seconds from epoch

    if [ $last_mod ]
    then
    time_diff=`expr $cur_timestamp - $last_mod`; #cal time difference 3 days or more
    if [ $time_diff -lt $checkduration ] # file modified three days or less back
    then
    mv ${i#/home/roab/toc} /home/roab/old_data; #if more then move to a new folder
    else
    /root/home/source/programs/abc.out filename; # if not call c program with filename as argument
    cat filename>>log; #append this file name to log sheet
    fi
    fi
    fi


    the following are the errors:
    stat: cannot stat `/22018-000181': No such file or directory
    stat: cannot stat `/22018-000182': No such file or directory
    stat: cannot stat `/22018-000183': No such file or directory
    stat: cannot stat `/22018-000184': No such file or directory
    stat: cannot stat `/22018-000185': No such file or directory
    stat: cannot stat `/22018-000186': No such file or directory
    stat: cannot stat `/22018-000187': No such file or directory


    Can someone help?


    rgds,
    Kevin

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try to get the filename with:

    Code:
    filename=$(basename $i)
    instead of:

    Code:
    filename=${i#/home/roab/toc}
    Besides of that, you can "stat" the file with the full path.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...