LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to insert $2 argument until the end to variable? (https://www.linuxquestions.org/questions/programming-9/how-to-insert-%242-argument-until-the-end-to-variable-881183/)

DoME69 05-17-2011 07:05 AM

how to insert $2 argument until the end to variable?
 
how to insert $2 argument until the end to variable?

For example:

>> cucu.csh user "long long message "

$1 = user
$2 = "long long message "

?

grail 05-17-2011 07:59 AM

You might have to explain further?? You seemed to have asked the question and provided the answer.

DoME69 05-17-2011 08:26 AM

What i'm trying to say is...

how can i set $2 to be the rest of the line.

for now i have $2 = "long"

MTK358 05-17-2011 08:41 AM

Quote:

Originally Posted by DoME69 (Post 4358828)
how can i set $2 to be the rest of the line.

Do you mean "even if the rest of the line isn't quoted"?

SL00b 05-17-2011 08:47 AM

You don't have to SET anything. Your first parameter is $1, and your entire second parameter, so long as it's enclosed in quotes, is $2.

Here's a simple script called test.sh, to demonstrate this principle:

Code:

echo The first variable passed is $1
echo The second variable passed is $2

And here's the result of running it:

Code:

:~> ./test.sh user "long, long, long, long message"
The first variable passed is user
The second variable passed is long, long, long, long message


DoME69 05-17-2011 08:51 AM

O.K...

So if i force user to use " message "
how can i check if it use commas for message?

SL00b 05-17-2011 09:10 AM

I assume you mean quotes, not commas. The answer is: it depends.

If you only expect two parameters, then the presence of a third parameter would indicate the user had failed to use quotes correctly. In that case, I'd add a test to see if $3 is non-blank, and echo an error message if true.

SL00b 05-17-2011 09:14 AM

Another way you could go is like this:

Code:

LONGSTRING=$2 $3 $4 $5 $6
echo The first variable passed is $1
echo The second variable passed is $LONGSTRING

This one assumes no more than five words are part of the long string. You could expand it to the max you expect, or construct a FOR loop. Either way, the user no longer has to worry about quotes.

MTK358 05-17-2011 09:19 AM

Quote:

Originally Posted by SL00b (Post 4358876)
This one assumes no more than five words are part of the long string. You could expand it to the max you expect, or construct a FOR loop. Either way, the user no longer has to worry about quotes.

The catch is that all whitespace will be converted into a single space. So if the user enters (without quotes) this:

Code:

test    text
the script will see it like this:

Code:

test text
Whitespace will still be preserved if quotes are used.

grail 05-17-2011 09:50 AM

Also if you really only want to have 2 and exactly 2 arguments:
Code:

if (( $# != 2 ))

Nominal Animal 05-17-2011 10:06 AM

If your script expects two arguments, and you want to combine any extra arguments to the second one, use
Code:

first="$1"
shift 1
second="$*"

and you have the first argument in $first, and the rest in $second.


All times are GMT -5. The time now is 02:25 AM.