LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash command separator/arguments separator (https://www.linuxquestions.org/questions/programming-9/bash-command-separator-arguments-separator-602542/)

reverse 11-26-2007 04:05 PM

Bash command separator/arguments separator
 
Is there a simple way to force BASH *not* to use the ";" character as shown below:

Code:

$ command_1 ; command_2 ; ... ; command_n
but rather a user-specified character, for example the character.. erh.. ! (or something, whatever):

Code:

$ command_1 ! command_2 ! ... ! command_n
Perhaps an environment variable, or similar?

Alas, should you want to pass a space-infested string as the first argument to a program, a construction of the following sort has to be used:

Code:

$ program "this is argv1"
I need to know if, yet again, the quote character can be replaced with something else, such as.. I'll just take ! again:

Code:

$ program !this is argv1!

rsashok 11-26-2007 04:18 PM

No, you can't replace double quote with anything else.

gnashley 11-27-2007 01:00 AM

You can't easily replace the quotes, but the separator character can be chaged with the 'IFS' variable (Internal File Separator). The standard setting for this is equivalent to 'space,tab,end-of-line'. Usually, if you are going to change it in a script you save the original value so it can be restored for normal use in other loops, so you atrt by putting:
OLDIFS=$IFS
IFS='whatever separator'

code....

IFS=$OLDIFS

chrism01 11-27-2007 01:21 AM

I don't think you can replace ';', that's not part of IFS, it's a cmd separator, different concept.

IFS: http://dict.die.net/ifs/

reverse 11-28-2007 03:36 PM

Hello. Sorry, I was *actually* looking for the variable that contains the character(s) that separates arguments passed to a program. I understand this is IFS, however, I do not understand why this does not work:

Code:

$ IFS=:
$ echo "${IFS}"
:
$ /bin/ls a.txt:b.txt
/bin/ls: a.txt:b.txt: No such file or directory
$ /bin/ls a.txt b.txt
a.txt b.txt
$

So obviously, although the 2nd and 3rd lines show that IFS is supposed to be set to ":", the command given on the 4th line (1st LS) does not work. I would expect this to work:

Code:

$ IFS=:
$ /bin/ls:file1:file2:file3


rsashok 11-28-2007 03:59 PM

Try following:
Code:

$bash -c 'IFS=":"; files="a.txt:b.txt"; ls -l $files'
It worked for me.

makyo 11-28-2007 04:52 PM

Hi.

From the O'Reilly book:
Quote:

Command-Line Processing

1. Splits the command into tokens that are separated by the fixed
of set metacharacters: SPACE, TAB, NEWLINE, ;, (, ), <, >, |, and &.

9. Takes the parts of the line that results from parameter,
command, and arithmetic substitution and splits them into words
again, This time it uses the characters in $IFS as delimiters
instead of the set of metacharacters in Step 1.

-- Learning the bash Shell, 2nd, pages 177 - 179.
Best wishes ... cheers, makyo

ta0kira 12-01-2007 10:38 AM

Maybe you can somehow wrap the command lines with sed? I'm not sure how you'd do that, but it sounds entirely plausible. I don't know if it could be done transparently unless you created a wrapper program which created a vterm and passed everything along to the "real" term while filtering typed input.
ta0kira

makyo 12-01-2007 05:05 PM

Hi.
Quote:

Originally Posted by ta0kira (Post 2976706)
Maybe you can somehow wrap the command lines with sed ...

I don't see the problem as anything except not being able to use IFS on literal strings.

To parse a string, assign it to a variable, set IFS, and use the results. Reset IFS afterwards as necessary. The example from rsashok illustrates this.

Or am I missing something? ... cheers, makyo

chrism01 12-01-2007 06:19 PM

You prob need to use single quotes around it, to avoid it being used as the bash built-in do-nothing/null cmd:
Code:

:          [builtin] - do nothing; this builtin has no effect

http://www.linspireguide.com/LinspireCommands2

If used, it's usually in an if block where it's clearer to say if "[[ sometest ]] do nothing else... fi" rather than try to reverse the logic.
eg
Code:

if [[ complextest ]]
then
    # this line does 'nothing ie a null cmd like x=x
    :
else
    do something
fi


reverse 12-02-2007 05:51 AM

Hello. Perhaps some (most/all?) of the people who read the thread are wondering about why I care about IFS. I /care/ about it because I am trying to understand why the system() function is considered insecure (according to its manual page). See this thread for a few details: http://www.linuxquestions.org/questi...secure-603187/

chrism01 12-02-2007 09:21 PM

See my answer there....


All times are GMT -5. The time now is 08:30 AM.