1

I have a script to delete files older than x mins/days.

find $1 -type f -mmin +$2 -exec rm -f {} \; (name is test2.sh)

I need to pass arguments to test2.sh from another script test3.sh for example.

0

Assuming that the parameter expansions in test2.sh are correctly quoted as

find "$1" -type f -mmin +"$2" -exec rm -f {} \;

then test3.sh might have a call to test2.sh like this:

test2.sh "some file name" 5

$1 in test2.sh will expand to the string some file name and $2 will expand to 5.

This is assuming that test2.sh is located in a directory that is listed in the PATH variable of test3.sh. If not, use a full path (absolute or relative), for example:

./test2.sh "some file name" 5

It also assumes that test2.sh has first been made executable with chmod +x test2.sh and that it has a correct #!-line.


After clarification in comment below:

The test3.sh script could do (assuming it's a bash script):

read -p 'Enter directory name: ' dirname
read -p 'Enter number of days: ' days

minutes=$(( days * 60 * 24 ))

./test2.sh "$dirname" "$minutes"

The next step would be to validate the user's input so that $days is an actual integer and so that $dirname is a valid name of a directory.

If it's not bash but sh:

printf 'Enter directory name: '
read -r dirname

(etc.)

Most implementations of find also have -delete:

find "$1" -type f -mmin +"$2" -delete
5
  • I know how my script works. But I need script test3.sh in which we have echo "Enter directory:" read directory and then days , and then pass the to that test2.sh script – Itso Jan 11 '18 at 11:47
  • @Itso I've update my answer. – Kusalananda Jan 11 '18 at 11:52
  • I get this error: find: invalid argument -exec' to -mmin' – Itso Jan 11 '18 at 12:51
  • @Itso Make sure your quoting is correct. You might get this if you have find ... -mmin +"$2 -exec ... maybe? – Kusalananda Jan 11 '18 at 13:11
  • 1
    @ltso that happens if the argument following -mmin is empty (no +, no value), i.e. find ... -mmin -exec rm -f {} \;. But as Kusalananda says, you should really be using -delete instead of -exec rm -f {} \;. Check your single and double quoting for your find command. AND for the script test3.sh. – roaima Jan 11 '18 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.