LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-16-2013, 02:24 PM   #1
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
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.
 
Old 01-16-2013, 02:45 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
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?
 
Old 01-16-2013, 03:06 PM   #3
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Original Poster
Rep: Reputation: 11
It is part of the command itself stored in $JOBCOMMAND

When doing: echo $JOBCOMMAND, the result is:

$SCRIPTDIR/path/to/script.sh "param1" "param2"
 
Old 01-16-2013, 03:56 PM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
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
...
 
1 members found this post helpful.
Old 01-17-2013, 12:50 AM   #5
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Original Poster
Rep: Reputation: 11
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.
 
Old 01-17-2013, 09:03 AM   #6
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
Can you post any of your script then?
 
Old 01-18-2013, 12:51 AM   #7
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Original Poster
Rep: Reputation: 11
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!

Last edited by Mark1986; 01-18-2013 at 01:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache/PHP: Executing 'at' scheduled jobs? bomix Linux - Software 11 01-23-2009 01:02 PM
Can a mailbox be stored in a database? zyrixnet Linux - Server 3 09-25-2007 09:49 PM
LXer: CLI Magic: Executing jobs once LXer Syndicated Linux News 0 05-29-2006 01:33 PM
Executing a script stored in an mounted NFS Share Riddick Linux - Software 6 01-13-2006 06:14 PM
Executing cron jobs on a w2k3 server using cygwin hq4ever General 4 06-01-2005 06:45 AM


All times are GMT -5. The time now is 03:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration