Jump to content

passing bash variables to other programs


Recommended Posts

I'm in a hurry now, but when I come back from work I'll read and comment your last post. Meanwhile as I had some spare time this morning I've rewritten your checkforprog() function to make it more user friendly, because as is written an user with missing dependences has to re-run the script several times to notice all the dependences problems that he may have.

 

notice also that all the error output is redirected to stderr (it was going to stdout in the original function). Here is the code (written in a hurry and only tested once, but I guess it works fine):

####################################################################
# checkforprog() - make sure that dependencies are installed
####################################################################
checkforprog ()
{
   # Description: Checks that all the dependences needed are in user's path
   #
   # Usage: -> checkforprog <program names to check>
   # Inputs: $@ == program names
   # Outputs: None || informative error output
   # Returns: 0 if right
   #          1 if missing dependences are found (and informative error output)
   local prog progs="${@}" # checks all dependences at once
   local dep_err=0 # flag
   for prog  in $progs; do
       if ! which $prog &> /dev/null; then
           echo >&2 "[dvd-homevideo] ERROR: program \"$prog\" not found! "
           dep_err=1
       fi
   done   
   if [ $dep_err == 1 ]; then
       echo >&2 "[dvd-homevideo] Check the dependencies and make sure everything is installed."
       echo >&2 "[USAGE] run \"$0 -h\" for more information"
       return 1
   fi    
}

###################
# now the function is called as:
# Check for dependencies!
DEPENDENCES="dvgrab transcode mplex dvd-menu dvdauthor"
checkforprog $DEPENDENCES || exit 1
if [ "$burndvd" -eq 1 ]; then
   checkforprog growisofs || exit 1
fi

 

As simple test, the following:

 

aru@mandrakeusers ~$ checkforprog ls FOO BAR cat tar FUU 
[dvd-homevideo] ERROR: program "FOO" not found! 
[dvd-homevideo] ERROR: program "BAR" not found! 
[dvd-homevideo] ERROR: program "FUU" not found! 
[dvd-homevideo] Check the dependencies and make sure everything is installed. 
[USAGE] run "bash -h" for more information 
aru@mandrakeusers ~$

 

ups as is tested in command line, the last line's $0 is expanded to "bash", but obviously from inside the program the output will be right.

 

HTH

 

later during the evening more ;)

Edited by aru
Link to comment
Share on other sites

I gave passing and returning arrays a shot, but I just couldn't get it to go. My reference was Steve's link from page one. I could get an 'example' program to work, however in my program something wasn't matching up and the result was a mess of code that didn't work. If you can figure it out (which I am sure you can) and you think it would make the code much better, then feel free to.

Link to comment
Share on other sites

hi, sorry for the delay (I've been busy) ;)

 

 

about passing arrays to functions, just keep in mind what I posted in my first post here in this thread, you pass strings, and you have to tell bash to interpret them in the right way. So to pass arrays to a function you have to pass the array to a string first and later convert it back to an array (it's easy), for example:

myfunction () {
  local -a my_inner_array=(${@}) # text converted into array (fields are separated by spaces default $IFS).
}

# calling the function
outer_array=(val1 val2 val3)
myfunction "$(echo ${outer_array[@]})" # the array becomes text, then is read by the function.

 

Now, some advanced tricks about this and extended to functions with long list of arguments:

 

You can use 'getopts' inside fucntions to make them easier to use and more readables; just keep in mind that you have to reset "getopts builtin variables" in order to reuse getopts inside functions (also will be a good idea to do the same within the main script with main getopts code).

 

Here is the code; I'm using as template your menu function -- but just focusing to input arguments and their types. The function only returns the typeset of input arguments to show that the "vob" array is kept inside the function after being passed as an argument:

 

#! /bin/bash
# filename: menu_test.sh
menu () {
   # Description: - Creates the background menu for the DVD
   #
   # Usage: -> menu -i background_image -a background_audio -v voblist_array [ -t title_file ]
   # Inputs:
   while getopts "i:a:t:v:" opt; do case ${opt} in
      i) local BG_PIC="${OPTARG}";; # background_image used for the DVD menu
      a) local BG_AUD="${OPTARG}";; # background_audio used for the DVD menu
      t) local TITLE_FILE="${OPTARG}";; # optional title_file used for the DVD menu
      v) local -a VOBARR=(${OPTARG});; # array assignation
      *) echo >&2 "${FUNCTNAME} ERROR: Bad args." && return 1;; # error in funct args
   esac; done
   OPTIND= OPTARG= OPTERR= # cleaning getopts inner variables
   # Outputs: remove_title[]?
   # Returns: 0 if right
   #          1 if...
   
   local menu_options count j old_IFS IFS lines
   local OUTPDIR="$outdir" # from global namespace

   # -----------------------------------------
   # everything else is stripped, 
   # I'm just testing input args types:
   # -----------------------------------------
   for VARIABLE in BG_PIC BG_AUD TITLE_FILE VOBARR; do
       typeset -p $VARIABLE # displays var type (-- normal; -a array) and value
   done  
}

# dumb arguments to test input arguments.
bgimage="somename.ext"
audiofile="anothername.ext"
titlefile="againanothername.ext"
vob=(aviname1 aviname2 aviname3 aviname4) # array to test

# testing the function with diverse syntax, all of this ways will work, but are more:
echo "test 1:"
echo 'menu  -i "$bgimage" -a "$audiofile" -v "$(echo ${vob[@]})" -t "$titlefile"'
menu  -i "$bgimage" -a "$audiofile" -v "$(echo ${vob[@]})" -t "$titlefile" 
echo
echo "test 2:"
echo 'eval menu -i "$bgimage" -a "$audiofile" -v "\"${vob[@]}\"" -t "$titlefile"'
eval menu -i "$bgimage" -a "$audiofile" -v "\"${vob[@]}\"" -t "$titlefile" 
echo 
echo "test 3:"
echo -n 'eval menu -i "$bgimage" -a "$audiofile" -v '; echo -n "\"'\${vob[@]}'\" "; echo '-t "$titlefile"'
eval menu -i "$bgimage" -a "$audiofile" -v "'${vob[@]}'" -t "$titlefile"

 

so when executed we have this output:

 

aru@mandrakeusers ~/__dvd_project$ sh menu_funct.sh
test 1:
menu  -i "$bgimage" -a "$audiofile" -v "$(echo ${vob[@]})" -t "$titlefile"
declare -- BG_PIC="somename.ext"
declare -- BG_AUD="anothername.ext"
declare -- TITLE_FILE="againanothername.ext"
declare -a VOBARR='([0]="aviname1" [1]="aviname2" [2]="aviname3" [3]="aviname4")'

test 2:
eval menu -i "$bgimage" -a "$audiofile" -v "\"${vob[@]}\"" -t "$titlefile"
declare -- BG_PIC="somename.ext"
declare -- BG_AUD="anothername.ext"
declare -- TITLE_FILE="againanothername.ext"
declare -a VOBARR='([0]="aviname1" [1]="aviname2" [2]="aviname3" [3]="aviname4")'

test 3:
eval menu -i "$bgimage" -a "$audiofile" -v "'${vob[@]}'" -t "$titlefile"
declare -- BG_PIC="somename.ext"
declare -- BG_AUD="anothername.ext"
declare -- TITLE_FILE="againanothername.ext"
declare -a VOBARR='([0]="aviname1" [1]="aviname2" [2]="aviname3" [3]="aviname4")'
aru@mandrakeusers ~/__dvd_project$

 

in all three tests the VOBARR array takes the vob array from outside perfectly. Test 3 is tricky, guess why?

 

HTH, I'm trying to introduce andvaced features, hope you don't mind :)

 

Edited: fixed typo in first example; array assignation made clearer

Edited by aru
Link to comment
Share on other sites

I have incorporated all of your recent suggestions, but I am having trouble returning the array from my functions. For some reason, I get no value returned at all. I know that I must be calling the function incorrectly because it is like it just skips over the function call completely. As you always do, please shed some light on this topic for me. Again, I have attached the latest version.

 

A continued thanks goes out to you for all of your help.

dvd_homevideo_revision5.txt

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...