LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-19-2007, 12:46 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Shell: string1 += string2. How to do this?


!/bin/sh

I would like to do the following operation on strings in shell (concatenate w/ a space):

Code:
 
string1='ham butter ham'
string2='donut'
string1 += $string2
echo $string1
to yield "ham butter ham donut" as the output. how do I do this?
 
Old 06-19-2007, 12:50 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
try
Code:
string1=${string1} ${string2}
 
Old 06-19-2007, 12:57 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 211Reputation: 211Reputation: 211
Didn't work for me. Here's my code if it helps... Thanks.

Code:
dir_list=`ls -ad */ 2>/dev/null`
cleaned_dirs=''

for dir_name in $dir_list ; do
    cd $dir_name
    # ... some stuff...
    if [ $? ]; then
        cleaned_dirs=${cleaned_dirs} ${dir_name}
    fi
    cd ..
done
 
Old 06-19-2007, 01:13 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Sorry, I should have known better. /bin/sh isn't taking kindly to the whitespace. It should be something like this
Code:
string1="$string1 $string2"
 
Old 06-19-2007, 01:13 PM   #5
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Try using an array.

diff:

Code:
cleaned_dirs=()

        cleaned_dirs=(${cleaned_dirs} ${dir_name})
Code:
string1 += $string2
has spaces around the +=, which is wrong. You cannot put spaces around an assigment operator in the shell. Bash doesn't understand += anyway (some shells do though).

Code:
string1=${string1} ${string2}
fails for the same reason, a space between string1 and string2 (this tries to run the command $string2). It should be

Code:
string1="${string1} ${string2}"
But, as I said, you should be using an array for this purpose (a list of seperate directories).
 
Old 06-19-2007, 01:18 PM   #6
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
the array may not really be necessary if he is only building this list in order to pass it as the arguments to a command. Then an array may only complicate the things. This scenario isn't unlikely in a shell script.

To the OP, dawkcid caught my mistake at the same time I did, but you should use the code in my post, as dakwcid's concatenates the strings without a space in between them.
 
Old 06-19-2007, 01:49 PM   #7
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by PatrickNew
the array may not really be necessary if he is only building this list in order to pass it as the arguments to a command. Then an array may only complicate the things. This scenario isn't unlikely in a shell script.
Oh, I was assuming it was intended just for logging/reporting which directories had been processed. So, yeah, a string may be preferable depending on what you want to do with it.

Quote:
To the OP, dawkcid caught my mistake at the same time I did, but you should use the code in my post, as dakwcid's concatenates the strings without a space in between them.
Yeah, that was a typo, I fixed it.
 
  


Reply

Tags
shell, string



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
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 03:02 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 01:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:53 AM.

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