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 05 '19 at 08: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 05 '19 at 09: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 05 '19 at 09:46
  • offcourse , there will be 3 cron entries as i run for different files on 3 days. – user3901666 Sep 05 '19 at 09: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 05 '19 at 09:53
  • have a look at passing parameter to scripts. Usually $1 is first argument. If cronjobs are ... main.sh file1, ... main.sh file2 etc, to reference that in main.sh then $1 would be file1 when executed from cron1, file2 from second cronjob. possibly relevant post – Smock Sep 05 '19 at 09:58
  • I know that option of $1 but I am not sure if the part of the script sh /Aug/work/Get_Files.sh $1 will work – user3901666 Sep 05 '19 at 10:19
  • What actually happens when that last cronjob is run? Do you get errors? – Kusalananda Sep 09 '19 at 09:32
  • The cron is not invoking the script only...but when i run manually it runs fine – user3901666 Sep 09 '19 at 09:47
  • The permissions are all fine – user3901666 Sep 09 '19 at 09:49

1 Answers1

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.

Smock
  • 125
  • 5