0

I have a script(main.sh) which calls another script(Get_Files.sh) and gets a value in a variable like the below:

File_to_Refresh="$(sh /Aug/work/Get_Files.sh /Aug/Work/Universal_File.txt)"

Now I have to schedule the script main.sh for 3 different files i.e. the Universal_File.txt with 3 different values and on 3 different days.

So, to make the script generic, I want to pass the Universal file to Get_Files.sh from the Cron Entry only.

How Can i achieve the same?

Below is working if I run the script manually like sh /Aug/Work/main.sh /Aug/Work/Universal_File.txt but it's not working when i run it through cron.

File_to_Refresh="$(sh /Aug/work/Get_Files.sh "$1")"

Cron :

45 08 * * * sh /Aug/Work/main.sh /Aug/Work/Universal_File.txt
  • Are you trying to execute main.sh every day, but Get_Files.sh uses each of the 3 files every third day? (so file1, file2, file3 in a 3 day cycle) – Smock Sep 5 at 8:35
  • No , I execute main.sh as per the below dates: monday--( with get_file.sh uses file1) Wednesday-- (with get_file.sh uses file2) Friday--( with get_file.sh uses file3) – user3901666 Sep 5 at 9:28
  • Are you attempting to have just one cron entry, or will 3 (one each for mon/wed/fri) do? Is there any reason the script cannot be day-of-week aware and have three different statements depending on which day it is? (Or at least use some logic based on a parameter passed to it to decide which one) – Smock Sep 5 at 9:46
  • offcourse , there will be 3 cron entries as i run for different files on 3 days. – user3901666 Sep 5 at 9:52
  • I just want of we can pass the file from cron itself and define something like a $VAR in my script in place of file – user3901666 Sep 5 at 9:53
1

If you have 3 cron entries, 1 for each day (as mentioned in comments), you should be able to specificy the file used for each cron entry as an argument and use $1 in the main.sh script

$ cat main.sh
File_to_Refresh=$(sh sub.sh $1)
echo FileToRefresh: $File_to_Refresh

$ cat sub.sh
echo Sub \$1: $1

$ ./main.sh /Aug/Work/File1.txt
FileToRefresh: Sub $1: /Aug/Work/File1.txt

Not convinced you need the " marks around the $(xxx), it seems to work both ways for me.

  • Thanks Smock..Let me check that. – user3901666 Sep 5 at 11:26
  • Added the comments , I tried as suggested, but it's not working – user3901666 Sep 5 at 12:50
  • can you add an echo Sub \$1: $1 into the Get_Files.sh script to see if the parameter is being passed to it or not? – Smock Sep 5 at 13:09
  • Changed it "$1" and it is working manually but not from cron – user3901666 Sep 9 at 8:13
  • do any of the scripts rely on environment variables? – Smock Sep 9 at 14:54

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.