LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2011, 07:05 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
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 "

?
 
Old 05-17-2011, 07:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You might have to explain further?? You seemed to have asked the question and provided the answer.
 
Old 05-17-2011, 08:26 AM   #3
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
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"
 
Old 05-17-2011, 08:41 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by DoME69 View Post
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"?
 
Old 05-17-2011, 08:47 AM   #5
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
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
 
Old 05-17-2011, 08:51 AM   #6
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
O.K...

So if i force user to use " message "
how can i check if it use commas for message?
 
Old 05-17-2011, 09:10 AM   #7
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
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.
 
Old 05-17-2011, 09:14 AM   #8
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
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.
 
Old 05-17-2011, 09:19 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by SL00b View Post
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.
 
Old 05-17-2011, 09:50 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Also if you really only want to have 2 and exactly 2 arguments:
Code:
if (( $# != 2 ))
 
Old 05-17-2011, 10:06 AM   #11
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script to insert characters at end of basename in a filename SteveInTallyFL Programming 9 08-20-2010 01:08 AM
insert character into a variable jadeddog Programming 4 11-04-2008 03:39 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
How do you insert an iptables rules before the end?.... steppin_razor Linux - Security 2 11-25-2001 06:39 PM
How do you insert an iptables rules before the end?.... steppin_razor Linux - Networking 0 11-20-2001 11:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration