LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Executing jobs as stored in database (https://www.linuxquestions.org/questions/linux-general-1/executing-jobs-as-stored-in-database-4175445917/)

Mark1986 01-16-2013 02:24 PM

Executing jobs as stored in database
 
Hi everyone,

I am coping with a problem for a few weeks already. After extensive searching I still can't find the solution.

Here is my problem:

I have a MySQL database with a table that has job commands, like: $SCRIPTDIR/path/to/script.sh "param1" "param2"

I have a script that executes a query and retrieves the value from that column. I echo'd the value (stored in $JOBCOMMAND) and it is correct. So that part works.

Now I try to execute the job as:

`$JOBCOMMAND` --> fails
$($JOBCOMMAND) --> fails
`echo $JOBCOMMAND` --> fails
`$($JOBCOMMAND)` --> fails

The error: No such file or directory.

The actual problem (as I see it) is that I have a $ in the job command, which does not get parsed. I am trying hard to avoid eval, but I am completely lost.

Does anyone have any suggestions?

Thank you for your time and effort.

thesnow 01-16-2013 02:45 PM

I am unsure where you are saying the $ is. Is it part of the command itself stored in $JOBCOMMAND, or the variable name is $JOBCOMMAND instead of JOBCOMMAND?

Mark1986 01-16-2013 03:06 PM

It is part of the command itself stored in $JOBCOMMAND

When doing: echo $JOBCOMMAND, the result is:

$SCRIPTDIR/path/to/script.sh "param1" "param2"

thesnow 01-16-2013 03:56 PM

Like this?

Code:

lm ~ # SCRIPTDIR=/tmp
+ SCRIPTDIR=/tmp
lm ~ # JOBCOMMAND='ls -al $SCRIPTDIR'
+ JOBCOMMAND='ls -al $SCRIPTDIR'
lm ~ # $(echo $JOBCOMMAND | sed  -e 's/$SCRIPTDIR/\'$SCRIPTDIR'/')
++ echo ls -al '$SCRIPTDIR'
++ sed -e 's/$SCRIPTDIR/\/tmp/'
+ ls -al /tmp
total 4292
drwxrwxrwt 15 root    root      20480 Jan 16 15:50 .
drwxr-xr-x 23 root    root        4096 Dec 17 15:47 ..
-rw-r--r--  1 root    root          0 Jan 16 00:00 access.log
drwx------  2 root    root        4096 Jan 14 11:19 cvcd
...


Mark1986 01-17-2013 12:50 AM

That would work, except ... ;-)

At this moment I have 1 variable, $SCRIPTDIR. There will be more in the future and I don't want to update the script for each new variable.

thesnow 01-17-2013 09:03 AM

Can you post any of your script then?

Mark1986 01-18-2013 12:51 AM

I solved it for now:

Code:

#!/bin/bash

PART="home/my_account"
SUBFOLDER="test"

SUB_COMMAND(){
  PART_IN=$1
# Add backslashes to all forward slashes
  PART_OUT=$(echo $PART_IN | sed -e 's/\//\\\//g')
# If there are 2 backslashes in front of a forward slash, change to 1 backslash
  PART_OUT=$(echo $PART_OUT | sed -e 's/\\\\\//\\\//g')
# Return end result
  echo $PART_OUT
}

CREATE_COMMAND(){
  SQL='ls /{@PART}/apps/{@SUBFOLDER}'
# Per variable substitute value
# Variable: PART
  PART_SUB=$(SUB_COMMAND $PART)
  COMMAND_OUT=$(echo $SQL | sed -e "s/{@PART}/$PART_SUB/g")
# Variable: SUBFOLDER
  COMMAND_OUT=$(echo $COMMAND_OUT | sed -e "s/{@SUBFOLDER}/$SUBFOLDER/g")
# Return end command
  echo $COMMAND_OUT
}

echo "Part: $PART"
COMMAND=$(CREATE_COMMAND)

echo "Command: $COMMAND"

exit 0

Three notes on this:
1. I will have to create a substitute value for each parameter.
2. I will have to update the CREATE_COMMAND function for each parameter.
3. I changed the variable names from $PART to {@PART} in the value, since I could not find a way to parse $PART.

There was nothing I could think of that was clean and elegant.

Thank you for your time and effort!


All times are GMT -5. The time now is 12:24 PM.