LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Passing a paramter to a list of command in another file (https://www.linuxquestions.org/questions/linux-newbie-8/passing-a-paramter-to-a-list-of-command-in-another-file-802827/)

tqz 04-19-2010 06:37 AM

Passing a paramter to a list of command in another file
 
Hello all

I am attempting to write my first ever script from scratch and making some progress.

My first shell script file executes a list of commands contained in a second file. I need to pass a parameter to this command file how do I do this???

Hope this makes sense...

t.

PMP 04-19-2010 06:42 AM

Quote:

Originally Posted by tqz (Post 3940179)
Hello all

I am attempting to write my first ever script from scratch and making some progress.

My first shell script file executes a list of commands contained in a second file. I need to pass a parameter to this command file how do I do this???

Hope this makes sense...

t.

Explain more !!

tqz 04-19-2010 06:55 AM

Sorry...okay

Im am trying to pass a variable 'x' lets say to /etc/bacula/bconsole -c bin/bconsole.conf </bin/baculacommands.sh. The first part before the < is the bacula console program and after the < is a file which contains a list of commands that needs to be executed by the console program. I need to pass a variable to this command list file as well how do I do this and how do i ref the variable in the second file...

Hope this is much more clearer...

Thanks.

catkin 04-19-2010 08:54 AM

Quote:

Originally Posted by tqz (Post 3940192)
Sorry...okay

Im am trying to pass a variable 'x' lets say to /etc/bacula/bconsole -c bin/bconsole.conf </bin/baculacommands.sh. The first part before the < is the bacula console program and after the < is a file which contains a list of commands that needs to be executed by the console program. I need to pass a variable to this command list file as well how do I do this and how do i ref the variable in the second file...

Hope this is much more clearer...

Thanks.

Not much clearer! :)

I think the problem is you are asking for the impossible so it's hard to understand but easy for you to conceptualise.

If you want to write a script that will pass a combination of fixed commands and variable data to an external program that will take its input from stdin (as bconsole does) then it is better to use a "here document" or a "here string" rather than a file because you can easily change the variable data and your script will be briefer and easier to understand. The alternative is to write the input file on-the-fly but it's more obscure and complex.

Here's a "here string" example which also captures output from bconsole in case the script is not being run interactively (you could log it instead of echoing it)
Code:

    buf="$( $bconsole <<< "@output /dev/null
            messages
            @output
            run job="$job_name" level=$job_level yes
            quit" 2>&1
    )"
    echo "DEBUG: buf from run job is '$buf'"


tqz 04-19-2010 10:04 AM

HI Catkin thanks for your response but I am so confused! :)

Here is an example of my files:-

so my bacula.sh file
#! /bin/bash

Vol=$(echo "st storage=DEV6-Changer" | bconsole | grep Volume: | cut -d':' -f2)
echo $Vol
/etc/bacula/bconsole -c bin/bconsole.conf </bin/baculacommands.sh
exit 0

For my baculacommands.sh file i have something like:-
mount
purge
3
8

I just want to add the $Vol name that I have found from my first file to the bottom of my second file - but am clueless as to how to pass the variable to this file...

Hope this is clearer...

catkin 04-19-2010 11:55 AM

Based on your script but using a here string instead of the file and omitting capturing the bconsole output
Code:

#! /bin/bash

Vol=$(echo "st storage=DEV6-Changer" | bconsole | grep Volume: | cut -d':' -f2)
echo $Vol
/etc/bacula/bconsole -c bin/bconsole.conf  <<< "mount
purge
3
8
$Vol"

If you still want to use a file then
Code:

#! /bin/bash

Vol=$(echo "st storage=DEV6-Changer" | bconsole | grep Volume: | cut -d':' -f2)
echo $Vol
cat /bin/baculacommands.sh > /bin/baculacommands.tmp.sh
echo $Vol >> /bin/baculacommands.tmp.sh
/etc/bacula/bconsole -c bin/bconsole.conf </bin/baculacommands.tmp.sh
exit 0


tqz 04-20-2010 04:53 AM

Hi Catkin

And thanks for ALL your help :) Much appreciated!

I did resolve the problem in the end. Probably not as simple or clean as your code but it works. I simply added lines to clear the content of the baculacommands file each time and then add the commands that I want to execute to that file along with the volume name as a string! Works a charm!

Thanks again! :)

catkin 04-20-2010 05:27 AM

Quote:

Originally Posted by tqz (Post 3941262)
Hi Catkin

And thanks for ALL your help :) Much appreciated!

I did resolve the problem in the end. Probably not as simple or clean as your code but it works. I simply added lines to clear the content of the baculacommands file each time and then add the commands that I want to execute to that file along with the volume name as a string! Works a charm!

Thanks again! :)

Welcome! :)

Glad you found a solution that works for you.

The Thread Tools menu can be used to mark the thread SOLVED.


All times are GMT -5. The time now is 06:26 AM.