This is my first foray into shell scripting so if i’m asking a very basic question please be gentle with me!
I have a shell script which downloads a file via FTP, uses split to break the file into separate smaller files. I then use a for loop to call a PHP file which does some processing on the file, this PHP process is run in the background to completion.
This 2 script combo works fine when run from the command line under sudo, however when it is run from the cron I don’t seem to be able to get the file name value to pass into PHP.
My 2 test scripts are as follows
shell-test.sh
#!/bin/bash
cd /path/to/directory/containing/split/files/
#Split the file into seperate 80k line files
split -l 80000 /path/to/file/needing/to/be/split/
#Get the current epoch time as all scripts will need to use the same update time
epochtime=$(date +"%s")
echo $epochtime
#Output a list of the files in the directory
ls
#For loop to run through each file in the working directory
#For each file we run the php script with safe mode off (to enable access to includes)
#We pass in the name of the file and epochtime
#The ampersand at the end of the string runs the file in the background in parallel so that all scripts execute concurrently
for file in *
do
php -d safe_mode=Off /path/to/php/script/shell-test.php -f $file -t $epochtime &
done
#Wait for all scripts to finish
wait
shell-test.php
<?php
$scriptOptions = getopt("f:t:");
print_r($scriptOptions);
?>
When run from the command line outputs the following which is what I need – the file value is passed to the PHP script.
1319824758
xaa xab xac xad
Array
(
[f] => xaa
[t] => 1319824758
)
Array
(
[f] => xac
[t] => 1319824758
)
Array
(
[f] => xad
[t] => 1319824758
)
Array
(
[f] => xab
[t] => 1319824758
)
However when run via the cron the following is outputted
1319825522
xaa
xab
xac
xad
Array
(
[f] => *
[t] => 1319825522
)
So what I need to know is how to get the value of * as the file name and not as the actual string * (and why this is happening would be useful too!).
My random guess is that cron is running the shell with the -f option for safety. Try adding
to your script. Or find some other way of enumerating the files.