LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pass parameters through bash doesn't work (https://www.linuxquestions.org/questions/linux-newbie-8/pass-parameters-through-bash-doesnt-work-4175502854/)

eudald 04-24-2014 10:53 AM

Pass parameters through bash doesn't work
 
Hi all,
First of all, apologises for main topic, but I'm not able to change its name.

I'm writting a script in bash with some functions and it doesn't work properly.

For example, which is what happens:
Code:

compFunct(){
  $1 $3.$2 $4
}

#main
vDest="/tmp/export/file"
vOrig="/tmp/export/filedest"
echo "Compression method: "
read vCompression
case $vCompression in
  zip)
    vFormat="zip"
    ;;
  tar.gz)
    vFormat="tar -czvf"
    ;;
  *)
    echo "Unknown compression format"
    ;;
esac
compFunct $vFormat $vCompression $vDest $vOrig

For the first case it works, but not for the second. I think there migh be any problem with the space or cause these are options, but I don't know.

Errormessg:
Code:

tar -czvf: command not found
Any clues?
Thanks,
Eudald

szboardstretcher 04-24-2014 11:07 AM

What is your input command?

Have you tried putting #!/bin/bash -x at the top and running again for additional error output? Try that.

eudald 04-24-2014 11:33 AM

Hi szboardstretcher,
There's no input on this script,

Thanks for your tip, I've noticed that the function takes the parameter as a whole cause I changed IFS at the beggining of the script for other needs.

I'll change that and will check if it works.

Edit: It works, thanks once again

Eudald

szboardstretcher 04-24-2014 11:59 AM

That's why you should always paste your whole script when troubleshooting ;) Glad you got it worked out.

grail 04-24-2014 12:28 PM

I would be curious to know how it worked? If you reset IFS back to its original value your 4 positional arguments will move and be incorrect as vFormat unquoted will now yield:
Code:

$1 - tar
$2 - -cvf
$3 - tar.gz
$4 - /tmp/export/file

Hence the original source will no longer be part of what the function compFunct sees. So either you changed more than just resetting IFS or your version of bash behaves really differently??


All times are GMT -5. The time now is 11:50 PM.