You are not logged in.

#1 2010-02-10 03:17:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] How to use Linux Bash to Create an Array of Files in a Dir...

So I created a bash script which executes at random one PHP script in a directory,

scripts=(
    flag_videos_which_need_grabbing.php
    grab_video.php
    grab_speaker_image.php
);

php -f /home/rt_cron/${scripts[$((RANDOM%${#scripts[*]}))]} > /dev/null 2>&1;

However, because I change the PHP scripts in that directory frequently I'd like this bash script to actually create its 'scripts' array from the current contents of the /home/rt_cron directory. Basically I need something like this,

scripts = $(ls /home/rt_cron/*.php)

Any ideas anyone?

Last edited by tony5429 (2010-02-10 03:32:35)

Offline

#2 2010-02-10 03:29:17

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to use Linux Bash to Create an Array of Files in a Dir...

Well just minutes after posting that I found a solution with awk,

scripts=($(ls /home/rt_cron/*.php | awk -F" " '{$1=$1; print}'));

Offline

#3 2010-02-10 03:29:43

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [SOLVED] How to use Linux Bash to Create an Array of Files in a Dir...

scripts=(`ls /home/rt_cron/*.php`)

Methinks.

tony5429 wrote:

Well just minutes after posting that I found a solution with awk,

scripts=($(ls /home/rt_cron/*.php | awk -F" " '{$1=$1; print}'));

You don't need that awk in there at all.

Last edited by Peasantoid (2010-02-10 03:32:03)

Offline

#4 2010-02-10 03:40:10

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to use Linux Bash to Create an Array of Files in a Dir...

Oh great; that's even better! Thanks, peasantoid!

Offline

Board footer

Powered by FluxBB