1

A bash scrip is invoked like this:

$./script 25 "str1 str2"

and it is supposed to launch a terminal, that runs another script that receives both arguments, exactly as they are above (including quotation marks). I've tried this:

lxterminal --command=$"./script2 "$"$@"

but this seems to omit the quotations marks, so the call would end up as ./script2 25 str1 str2. What is the correct notation to replicate the arguments as they were in the original command line?

4
  • 1
    If you want to pass str1 str2 as one single string, add single quotes to your first command. $./script 25 "'str1 str2'" Feb 4, 2017 at 21:26
  • @Serg The thing is that when I launch lxterminal --command=./script2 25 'str1 str2' from a terminal, it works fine, but trying $./script 25 "str1 str2" (what produces the same command, as shown by echo), it raises an error, saying that lxterminalhas been used in a wrong way (Usage: lxterminal [Options...], etc.). Besides, this is intended to be typed by non-skilled users, so the double-single quoting will bring trouble.
    – nightcod3r
    Feb 4, 2017 at 21:36
  • well, OK, I understand it's sometimes necessary to make it simple for non-skilled users, but the simplifications should go up to a certain point. Shell scripts aren't exactly a tool for unskilled user either. Let me ask you this - are there going to be more arguments, or are you always going to have something like number "string1 string2" structure ? If that's so, you could refer to them by position. Also, why do you use $ in second script within the --command= part ? Feb 4, 2017 at 21:55
  • @Serg Absolutely, the shell is not the place, but launching the program is the only requirement to start with it, I'm just trying to make it as simple as possible. As for the arguments structure, it can actually vary, only the number is mandatory, but it could go alone, or be followed by a sigle string str1, or by a string made of two strings with a space delimiter "str1 str2". Finally, the '$', to be honest, I'm not sure about it, it just worked.
    – nightcod3r
    Feb 5, 2017 at 0:15

1 Answer 1

4

The problem is that the argument to lxterminal's --command is just one string, it can't take a command and its arguments like some other terminals like xterm do.

lxterminal parses that string using its own rules to determine the command to run an its arguments. That's similar but not identical to the Bourne shell parsing.

It does recognise '...' as strong quotes and space as argument separator, so you can implement quoting for it as:

lxquote() {
  awk -v q="'" '
    function lxquote(s) {
      gsub(q, q "\\" q q, s)
      return q s q
    }
    BEGIN {
      for (i = 1; i < ARGC; i++) {
        printf sep "%s", lxquote(ARGV[i])
        sep = " "
      }
    }' "$@"
}

And call lxterminal as:

lxterminal --command="$(lxquote ./script2 "$@")"

Alternatively, if script's interpreter is bash, you can do:

printf -v code '%q ' ./script2 "$@"
CODE=$code lxterminal --command="bash -c 'eval \"\$CODE\"'"
2
  • A closing bracket is missing before the }' "$@" line in the lxqoute function, please edit. Apart from that, it works nicely. The second option (yes, bash is the interpreter) did not work, but can't say why.
    – nightcod3r
    Feb 5, 2017 at 2:13
  • @nightcod3r, for the second option, it would be interesting to run it with lxterminal replaced with strace -fe execve lxterminal to understand why it fails for you. Feb 5, 2017 at 15:17

You must log in to answer this question.

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