2

What I have is a few script files that are used for crons for different buildings in my company, but what I'm running into is I'm having to go into each file and change the OAK3 to a different building id, as well as the oak3(lowercase). The files are all located in there respectives warehouses folder ex: Desktop/CRON/OAK3. What I would like it to do, is if it's OAK3 use OAK3, and oak3(lowercase) instead of having to go into each file everytime we create a new db for a warehouse.

I am new to the linux world so I'm not sure if there is a way, and haven't found anything on google.

Example.sh

/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1

Desired effect is possible

/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/$WAREHOUSE_ID/$warehouse_id_count_portal.txt --ignore-lines=1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Nomad
  • 250
  • 3
  • 11
  • 27
  • And if I understand, you want to do this based on the _directory_ name `OAK3`, or you want to do away with the use of directories to separate them? – Michael Berkowski Jan 04 '14 at 01:33

3 Answers3

1

If i get what you want, which I´m not sure, this will help to do all new databases

  databases=`mysql -B -r -u ${user} --skip-column-names -p${pass} --execute='show databases'`
  for db in $databases; do
    ## now loop through the above array
     echo $db # current DB
     mysqldump -u $user --password=$pass $db > "$db.sql" #dump db to file
     done
Lucas
  • 9,871
  • 5
  • 42
  • 52
1

Using a combination of dirname and basename with the Bash special variable $0, you can get all of what you need.

The running script's filename is $0. Meanwhile dirname $0 will give you the directory path of the executing file. But you don't want the full path, just the last part, which basename will provide. realpath is used to expand the directory so . is not used.

Getting just the last directory name:

$ ls
tmp.sh           # Ok, there's our file
$ dirname tmp.sh
.                # The . is current directory
$ dirname $(realpath tmp.sh)
/home/mjb/OAK3   # so we expand it with realpath
$ basename $(dirname $(realpath tmp.sh))
OAK3             # then take only the last one with basename

So here's how it will work for you:

# Get the directory name
warehouse=$(basename $(dirname $(realpath $0)))

# And lowercase it with `tr` into a new variable
warehouse_lcase=$(echo $warehouse | tr '[:upper:]' '[:lower:]')

# Substitute the variables
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/${warehouse}/${warehouse_lcase}_count_portal.txt --ignore-lines=1

See also: Can a Bash script tell which directory it's stored in?

Community
  • 1
  • 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • This looks like it should work, I'll give it a try once I get back to work! Thanks – Nomad Jan 04 '14 at 01:44
  • 1
    @Spartacus38 Did you get a chance to try this out? – Michael Berkowski Jan 08 '14 at 20:09
  • 1
    This works beautifully!!!!!! THANK YOU. Having to copy/paste new id's in have been a pain. – Nomad Jan 09 '14 at 20:18
  • It works fine when ran independently, but when I added it to a cron I have it does nothing? Ideas? – Nomad Jan 09 '14 at 21:07
  • 1
    Run from cron how? Is this saved in its own file, made executable, and that file called from cron? If so, that should work as expected. If it does nothing at all, check cron logs, verify execute perms. If the vars didn't work out, cron would be reporting errors on the import file locations and you would be notified. – Michael Berkowski Jan 09 '14 at 21:28
  • I have it setup in cron as @hourly bash /home/username/Desktop/CRON/OAK3/count_portal.sh. It seems like it does nothing, but receive no error, when running bash independently through terminal it has no issues and imports like it should. – Nomad Jan 09 '14 at 23:26
  • Which user's crontab? Does `username` have read & execute permission on it? Does that user have execute permission on all the directories above it? You'll need to debug the cron on your system. Create a simple script in that directory that just does something like `echo $0` and run it every minute to a log: `* * * * * /path/to/OAK3/test.sh >> /tmp/yourlog` – Michael Berkowski Jan 10 '14 at 00:49
  • Yes the user has read & execute permissions as I have about 30 jobs running. Meaning all my jobs with the manual 'OAK3' and other values work fine, but when using it with variables the crons do not run. – Nomad Jan 10 '14 at 21:17
  • @Spartacus38 Did you verify that the script is even touched by cron? Is it logged? Stick something like `echo 'script running' >> /tmp/logfile` into it and see if `/tmp/logfile` gets written. – Michael Berkowski Jan 10 '14 at 21:20
0

There is lot easier way to figure out the basename of the current-working-directory: pwd -PL | sed sg.\*/ggg

[san@alarmp OAK3]$ pwd; pwd -PL | sed sg.\*/ggg
/opt/local/OAK3
OAK3

So, if I understand your requirement correctly, if you don't wanna change the script(s) manually by hand, you can do this whilst inside that particular directory:

$ cat example.sh 
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1
#
$ this_dir=$(pwd -PL | sed sg.\*/ggg)
#
$ sed -e "s/${this_dir}/\${WAREHOUSE_ID}/g" example.sh 
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/${WAREHOUSE_ID}/oak3_count_portal.txt --ignore-lines=1
#
$ sed -e "s/$(echo $this_dir | tr '[:upper:]' '[:lower:]')/\${warehouse_id}/g" example.sh 
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C  --delete test \
    /workplace/gwwallen/ETLdump/OAK3/${warehouse_id}_count_portal.txt --ignore-lines=1

Use -i option to make the change permanent in-file (without creating a new one) like this:

sed -ie "s/${this_dir}/\${WAREHOUSE_ID}/g" example.sh 
MacUsers
  • 2,091
  • 3
  • 35
  • 56
  • 1
    The sed command is a very useful tool, but "dirname `/bin/pwd`" or "dirname $path_variable" is a better solution than pulling out the sed hammer in this case. – Ned Jan 04 '14 at 03:32