1

I'm trying to figure out how to create a bash with multiple arguments, and one argument has multiple flags. I need to do this to open xterm, navigate to bash script, and execute it with arguments.

This is my code:

#!/bin/bash
if [ "$Running_In_New_Xterm" != t ] ; then
Running_In_New_Xterm=exec xterm -hold -e "cd $3 && $1 $2"
Running_In_New_Xterm=t
fi

$2 is a command to run flags on $1 -a blah -o blahblah

I'm trying to determine the proper way to execute #2. I read I have to add ' ' when having an argument with multiple spaces, but I am trying that on the input side, and it isn't working. I get " unexpected EOF while looking for matching `' ' " which suggests my problem has something to do with the fact that $2 isn't being executed correctly. I'm at the point I think it has something to do with the bash itsel.

5
  • 2
    Apart from the issue of quoting and the shell's interpretation of the strings that you pass into that argument for xterm, you also assign Running_In_New_Xterm in the (environment of xterm) the value of exec. Is that a typo?
    – Kusalananda
    Commented Apr 21, 2018 at 19:23
  • I am executing xterm if the conditions aren't meant ( != t). I've tried it, and it works. It's just for some reason I'm having difficulty passing the $2 flags through another script, even when I put ' ' outside them i.e. '-a blah -o blah blah'
    – Jacob
    Commented Apr 21, 2018 at 19:36
  • 1
    Well, yes, I can see that. I was just curious about Running_In_New_Xterm=exec.
    – Kusalananda
    Commented Apr 21, 2018 at 19:37
  • There is no typos that I can see
    – Jacob
    Commented Apr 21, 2018 at 20:08
  • -e "cd $3 && $1 $2" does not work? What is the exact resulting command line you want executed (as you would type it into a shell)? Commented Apr 22, 2018 at 16:40

1 Answer 1

0

cd $3 && $1 $2 is not a program which can be called. You need

Running_In_New_Xterm=exec xterm -hold -e bash -c "cd $3 && $1 $2"

or (with quoting, if the arguments require that; -a blah -o blahblah should probably not be quoted)

Running_In_New_Xterm=exec xterm -hold -e bash -c "exec cd '$3' && exec '$1' '$2'"
3
  • No need of bash -c . Take a look at man xterm : One parameter (after all options) may be given. That overrides xterm's built-in choice of shell program: but The -e option cannot be used with this parameter since it uses all parameters following the option.
    – ctac_
    Commented Apr 22, 2018 at 12:36
  • @ctac_ But one parameter is not enough in this case. Commented Apr 22, 2018 at 14:57
  • 1
    what I want to say is 'cd $3 && $1 $2 is not a program which can be called' is false and the OP problem is not the shell. xterm always start a shell. If $1 is executable, the command line is ok.The issue is from quoting.I try this way and it's ok. "./scriptbash.sh 'ls' '-l *.txt' '../1'". I get in the xterm window the expected result : all files ended with .txt in the directory ../1
    – ctac_
    Commented Apr 22, 2018 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .