0

Im trying to create a dynamic cronjob which executes a PHP script which needs a DB argument for specific directorys.

The Output should look like this:

/usr/bin/php /var/www/html/test.php db_test

I got following:

    #!/bin/bash
    DIR="/var/www/html/*"
    PHP="/usr/bin/php"
    ACCOUNTS=$(ls -d $DIR)
    DBPREFIX="db_"
    DB=$( find /var/www/html/ -mindepth 1 -maxdepth 1 -type d | awk -F'/' '{print $5}' )

    for CUSTOMER in $ACCOUNTS
    do
            echo $PHP $CUSTOMER $DBPREFIX$DB
    done

This outputs following:

/usr/bin/php /var/www/html/test.php db_test test2 test3 test4

The Problem is that the DBPREFIX and DB vars need also to go trough a loop so that i get for every single db a complete command.

How am I able to do so?

patrick_
  • 156
  • 2
  • 12

1 Answers1

1

You seem to be looking for something like

for dir in /var/www/html/*/; do
    base=${dir#/var/www/html/}
    db=${base%/}
    echo php "$db" "db_$db"   # or maybe php "${dir%/}" "db_$db"?
done

You could probably use something like find -printf "..." to accomplish much the same thing, but the -printf option to find is a GNU extension.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you very much thats what I was searching for. Could you explain what the var base and db are doing? – patrick_ Dec 07 '17 at 10:29
  • I'm discarding the directory name with `${var#prefixtoremove}` and the trailing slash with `${var%suffixtoremove}`. – tripleee Dec 07 '17 at 10:42
  • Read about [parameter expansions](http://wiki.bash-hackers.org/syntax/pe#substring_removal) for further explanation. – PesaThe Dec 07 '17 at 11:21