Find the answer to your Linux question:
Results 1 to 9 of 9
Hi Linux Gurus: I am newbie to shell script programming. I was trying to write one script which look for $1.txt file in two dir (say in and out), and ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    8

    Shell script for file existence

    Hi Linux Gurus:
    I am newbie to shell script programming. I was trying to write one script which look for $1.txt file in two dir (say in and out), and if file found in either of the said dir, it should copy back to my current working dir. Hope I made my requirement clear to you all.

    Thanking you all in advance...

    Regards,
    ..Jay

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You need to check if the file exists using 'test'.
    Check the man page for the test command and look through a couple of scripting tutorials.
    Go here for a tutorial:

    http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

    Check this page for the test command for files:

    http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html

    And this should be the next:

    http://tldp.org/LDP/abs/html/

    Does this make sense? If not, feel free to ask more questions!

    Regards

  3. #3
    Just Joined! Sivel's Avatar
    Join Date
    Jun 2005
    Location
    Maryland
    Posts
    20
    The following site is a good quick reference for file testing:

    Metalshell.com - Sh Source Code - File Testing

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    8
    Dear All:

    Thanks for your advice. I think I have not explained the things clear. I am running a script which will create a file say abc.txt in (in or out dir of my home dir). I just want to create a script which look for this file and no soon abc.txt file created in either of the mention above dir, it should copy back to my current working dir.

    Kinldy help...

    Regards,
    ...Jay

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Is this what you're looking for?

    This will look for a specified file in a certain path every 30 seconds.

    Code:
    echo -n "What file are you looking for? "
    read FILE
    echo -n "Where should I look? "
    read PATH
    
    while
    	[ ! $RESULT ]
    do
    sleep 30
    RESULT=`find $PATH -name $FILE -print`
    done
    echo "I found it"
    echo $RESULT
    #cp $RESULT ~/

  6. #6
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by cooljay2708 View Post
    Hi Linux Gurus:
    I am newbie to shell script programming. I was trying to write one script which look for $1.txt file in two dir (say in and out), and if file found in either of the said dir, it should copy back to my current working dir. Hope I made my requirement clear to you all.

    Code:
    for dir in in out
    do
       if [ -f "$dir/$1.txt" ]
       then
         mv "$dir/$1.txt" .
         break
       fi
    done

  7. #7
    Just Joined!
    Join Date
    May 2007
    Posts
    8

    Thanks Mr. Johnson, but while running the mention above script, loops got terminated if $1.txt not found in either of these dir. Whereas, I am looking for a script which keeps running untill $1.txt not found in the said dir. Once the $1.txt found, it should move to my working dir.

    Please advice...

    Regards,
    ...Jay

  8. #8
    Just Joined!
    Join Date
    May 2007
    Posts
    8

    Thanks to you all for your support and ideas...now I am able to get the same from mention below script"

    #!/bin/sh

    cdir=`pwd`

    while [ ! $RESULT ]
    do
    if [ -e out/$1.txt ]
    then
    RESULT="out/$1.txt"
    cp out/$1.txt .
    cp out/$1_log.txt .
    break
    elif [ -e in/$1.txt ]
    then
    RESULT="in/$1.txt"
    cp in/$1.txt .
    cp in/$1_log.txt .
    break
    fi
    sleep 30
    done

    Is it possible to check the time stamp of older files through script. If $1.txt is already existing in (in or out dir), the said above script will copy the same as it is. Can we check for time of $1.txt (suppose, if $1.txt creation time is 1 hr ago, then it shouldn't copy). Please suggest.

    Regards,
    ...Jay

  9. #9
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    The easies solution I guess is cp -u, it will only copy then if the source file is newer than the destination file, or if the destinationfile doesn't exist.

Posting Permissions

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