The following script will output one line. the carriage return is
removed by the back quote operation. Any way to prevent that carriage
is removed?
set a=`ls -al`
echo $a
|
|
0
|
|
|
|
Reply
|
kirby
|
3/26/2011 7:55:36 PM |
|
On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
kirby <chou2008@gmail.com> wrote:
> The following script will output one line. the carriage return is
> removed by the back quote operation.
False (I think).
> Any way to prevent that carriage is removed?
>
> set a=`ls -al`
> echo $a
Don't know about C shell, but this should work on a sane shell:
echo "$a"
|
|
0
|
|
|
|
Reply
|
pk
|
3/26/2011 7:51:54 PM
|
|
Forgot to mention, it is C shell.
On Mar 26, 3:55=A0pm, kirby <chou2...@gmail.com> wrote:
> The following script will output one line. the carriage return is
> removed by the back quote operation. Any way to prevent that carriage
> is removed?
>
> set a=3D`ls -al`
> echo $a
|
|
0
|
|
|
|
Reply
|
kirby
|
3/26/2011 7:57:45 PM
|
|
On Sat, 26 Mar 2011 18:02:37 -0400
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:
> On 2011-03-26, pk wrote:
> > On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
> > kirby <chou2008@gmail.com> wrote:
> >
> >> The following script will output one line. the carriage return is
> >> removed by the back quote operation.
> >
> > False (I think).
> >
> >> Any way to prevent that carriage is removed?
> >>
> >> set a=`ls -al`
> >> echo $a
> >
> > Don't know about C shell, but this should work on a sane shell:
> >
> > echo "$a"
>
> That doesn't do it. Any trailing newlines are stripped by command
> substitution.
Right. So, under which circumstances the output of "ls -al" has more than a
trailing newline?
|
|
0
|
|
|
|
Reply
|
pk
|
3/26/2011 9:51:35 PM
|
|
On Sat, 26 Mar 2011 21:51:35 +0000 pk <pk@pk.invalid> wrote:
> > > echo "$a"
> >
> > That doesn't do it. Any trailing newlines are stripped by command
> > substitution.
>
> Right. So, under which circumstances the output of "ls -al" has more than
> a trailing newline?
I mean, except the pathological case where the last filename happens to
have a trailing \n in its name.
|
|
0
|
|
|
|
Reply
|
pk
|
3/26/2011 9:53:19 PM
|
|
On 2011-03-26, pk wrote:
> On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
> kirby <chou2008@gmail.com> wrote:
>
>> The following script will output one line. the carriage return is
>> removed by the back quote operation.
>
> False (I think).
>
>> Any way to prevent that carriage is removed?
>>
>> set a=`ls -al`
>> echo $a
>
> Don't know about C shell, but this should work on a sane shell:
>
> echo "$a"
That doesn't do it. Any trailing newlines are stripped by command
substitution. My method is:
a=`ls -al; echo .`
a=${a%?}
--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
|
|
0
|
|
|
|
Reply
|
Chris
|
3/26/2011 10:02:37 PM
|
|
On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
<cfajohnson@gmail.com> wrote:
> > Right. So, under which circumstances the output of "ls -al" has more
> > than a trailing newline?
>
> It doesn't; that's why I append another character to the command.
> The newline from ls is no longer trailing, and is therefore
> preserved. The extra character can then be removed with parameter
> expansion, leaving the variable with a trailing newline.
Yes I am aware of that, but I don't think that's what the OP was referring
to, since the echo command readds the trailing newline, so he wouldn't see
it missing in the output.
I think he was asking why the other newline characters (ie those separating
the ls -l entries), and those were removed by the shell (I think) because
of the missing quotes when echoing $a.
> Which doesn't help the OP as he wants a csh answer. There may be
> one, but I'd prefer to convince him to switch to a real shell.
Agreed.
|
|
0
|
|
|
|
Reply
|
pk
|
3/26/2011 10:09:21 PM
|
|
On 2011-03-26, pk wrote:
> On Sat, 26 Mar 2011 18:02:37 -0400
> "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:
>
>> On 2011-03-26, pk wrote:
>> > On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
>> > kirby <chou2008@gmail.com> wrote:
>> >
>> >> The following script will output one line. the carriage return is
>> >> removed by the back quote operation.
>> >
>> > False (I think).
>> >
>> >> Any way to prevent that carriage is removed?
>> >>
>> >> set a=`ls -al`
>> >> echo $a
>> >
>> > Don't know about C shell, but this should work on a sane shell:
>> >
>> > echo "$a"
>>
>> That doesn't do it. Any trailing newlines are stripped by command
>> substitution.
>
> Right. So, under which circumstances the output of "ls -al" has more than a
> trailing newline?
It doesn't; that's why I append another character to the command.
The newline from ls is no longer trailing, and is therefore
preserved. The extra character can then be removed with parameter
expansion, leaving the variable with a trailing newline.
Which doesn't help the OP as he wants a csh answer. There may be
one, but I'd prefer to convince him to switch to a real shell.
--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
|
|
0
|
|
|
|
Reply
|
Chris
|
3/26/2011 10:20:12 PM
|
|
On Sat, 26 Mar 2011 18:09:21 -0400, pk <pk@pk.invalid> wrote:
> I think he was asking why the other newline characters (ie those separating
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.
Agreed. From info bash ...
3.5.7 Word Splitting
--------------------
The shell scans the results of parameter expansion, command
substitution, and arithmetic expansion that did not occur within double
quotes for word splitting.
The shell treats each character of `$IFS' as a delimiter, and splits
the results of the other expansions into words on these characters. If
`IFS' is unset, or its value is exactly `<space><tab><newline>', the
default, then sequences of ` <space>', `<tab>', and `<newline>' at the
beginning and end of the results of the previous expansions are
ignored, and any sequence of `IFS' characters not at the beginning or
end serves to delimit words.
-----------------------------
So the new lines are removed unless the IFS is changed to not include
the new line character, or the variable is inside of double quotes.
Regards, Dave Hodgins
--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
|
|
0
|
|
|
|
Reply
|
David
|
3/26/2011 11:06:07 PM
|
|
On 26.03.2011 23:09, pk wrote:
> On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
> <cfajohnson@gmail.com> wrote:
>
>>> Right. So, under which circumstances the output of "ls -al" has more
>>> than a trailing newline?
>>
>> It doesn't; that's why I append another character to the command.
>> The newline from ls is no longer trailing, and is therefore
>> preserved. The extra character can then be removed with parameter
>> expansion, leaving the variable with a trailing newline.
>
> Yes I am aware of that, but I don't think that's what the OP was referring
> to, since the echo command readds the trailing newline, so he wouldn't see
> it missing in the output.
> I think he was asking why the other newline characters (ie those separating
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.
You can also do without quotes if you clear the IFS
IFS= a=`ls -al`
echo $a
Janis
>
>> Which doesn't help the OP as he wants a csh answer. There may be
>> one, but I'd prefer to convince him to switch to a real shell.
>
> Agreed.
>
|
|
0
|
|
|
|
Reply
|
Janis
|
3/26/2011 11:24:07 PM
|
|
>I think he was asking why the other newline characters (ie those separatin=
g
>the ls -l entries), and those were removed by the shell (I think) because
>of the missing quotes when echoing $a.
Yes. This is what I want.
unfortunately, both
echo "$a"
and
a=3D`ls -al; echo .`
a=3D${a%?}
don't do it.
Thanks any way!
On Mar 26, 6:09=A0pm, pk <p...@pk.invalid> wrote:
> On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
>
> <cfajohn...@gmail.com> wrote:
> > > Right. So, under which circumstances the output of "ls -al" has more
> > > than a trailing newline?
>
> > =A0 =A0 It doesn't; that's why I append another character to the comman=
d.
> > =A0 =A0 The newline from ls is no longer trailing, and is therefore
> > =A0 =A0 preserved. The extra character can then be removed with paramet=
er
> > =A0 =A0 expansion, leaving the variable with a trailing newline.
>
> Yes I am aware of that, but I don't think that's what the OP was referrin=
g
> to, since the echo command readds the trailing newline, so he wouldn't se=
e
> it missing in the output.
> I think he was asking why the other newline characters (ie those separati=
ng
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.
>
> > =A0 =A0 Which doesn't help the OP as he wants a csh answer. There may b=
e
> > =A0 =A0 one, but I'd prefer to convince him to switch to a real shell.
>
> Agreed.
|
|
0
|
|
|
|
Reply
|
kirby
|
3/27/2011 12:36:39 AM
|
|
This works for bash, but not for csh.
there's no IFS equivalent var for csh.
> You can also do without quotes if you clear the IFS
>
> =A0 IFS=3D a=3D`ls -al`
> =A0 echo $a
>
> Janis
>
>
>
>
>
>
>
>
>
> >> =A0 =A0 Which doesn't help the OP as he wants a csh answer. There may =
be
> >> =A0 =A0 one, but I'd prefer to convince him to switch to a real shell.
>
> > Agreed.
|
|
0
|
|
|
|
Reply
|
kirby
|
3/27/2011 12:41:17 AM
|
|
On 27.03.2011 01:41, kirby wrote:
> This works for bash, but not for csh.
> there's no IFS equivalent var for csh.
This was not a response to your csh question.
(You should anyway abandon C shell scripting if you can.)
>> You can also do without quotes if you clear the IFS
>>
>> IFS= a=`ls -al`
>> echo $a
>>
>> Janis
|
|
0
|
|
|
|
Reply
|
Janis
|
3/27/2011 12:51:04 AM
|
|
On 2011-03-27, kirby wrote:
>>I think he was asking why the other newline characters (ie those separating
>>the ls -l entries), and those were removed by the shell (I think) because
>>of the missing quotes when echoing $a.
> Yes. This is what I want.
>
> unfortunately, both
> echo "$a"
> and
> a=`ls -al; echo .`
> a=${a%?}
>
> don't do it.
The following *does* do it (in a real shell, not csh):
a=`ls -al; echo .`
a=${a%?}
echo "$a"
--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
|
|
0
|
|
|
|
Reply
|
Chris
|
3/27/2011 1:53:03 AM
|
|
In article
<0a4df1a1-d856-4979-ac4b-6c7b67c4be4b@o15g2000prn.googlegroups.com>,
kirby <chou2008@gmail.com> wrote:
> Forgot to mention, it is C shell.
That was pretty obvious from the fact that you use the set command to
assign a variable.
Why are you writing scripts in C shell instead of a reasonable one?
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
>
> On Mar 26, 3:55�pm, kirby <chou2...@gmail.com> wrote:
> > The following script will output one line. the carriage return is
> > removed by the back quote operation. Any way to prevent that carriage
> > is removed?
> >
> > set a=`ls -al`
> > echo $a
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
0
|
|
|
|
Reply
|
Barry
|
3/27/2011 6:48:17 AM
|
|
On 2011-03-27, Barry Margolin wrote:
> In article
><0a4df1a1-d856-4979-ac4b-6c7b67c4be4b@o15g2000prn.googlegroups.com>,
> kirby <chou2008@gmail.com> wrote:
>
>> Forgot to mention, it is C shell.
>
> That was pretty obvious from the fact that you use the set command to
> assign a variable.
>
> Why are you writing scripts in C shell instead of a reasonable one?
>
> http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
See also:
Top Ten Reasons not to use the C shell
<http://www.grymoire.com/Unix/CshTop10.txt>
Csh problems
<http://www.grymoire.com/Unix/Csh.html#uh-0>
--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
|
|
0
|
|
|
|
Reply
|
Chris
|
3/27/2011 6:59:32 AM
|
|
kirby <chou2008@gmail.com> writes:
> The following script will output one line. the carriage return is
> removed by the back quote operation. Any way to prevent that carriage
> is removed?
>
> set a=`ls -al`
> echo $a
It would help to know what output "ls -al" produces, but it's safe to
assume that it's at least 3 lines. Are you referring to the carriage
return at the end of the last line, or the carriage returns at the end
of each line?
Quoting the tcsh man page:
Command substitution
Command substitution is indicated by a command enclosed
in ``'. The output from such a command is broken into
separate words at blanks, tabs and newlines, and null words
are discarded. The output is variable and command substituted
and put in place of the original string.
Command substitutions inside double quotes (`"') retain
blanks and tabs; only newlines force new words. The single
final newline does not force a new word in any case.
It is thus possible for a command substitution to yield only
part of a word, even if the command outputs a complete line.
By default, the shell since version 6.12 replaces all newline
and carriage return characters in the command by spaces.
If this is switched off by unsetting csubstnonl, newlines
separate commands as usual.
I don't know (a) whether you're using tcsh or csh, or (b) whether
classic csh behaves the same way (it probably does, mostly).
So if "ls -al" produces, say, 10 lines of output, then
set a = "`ls -al'"
will set a to a 10-element array, one for each line. The newlines
aren't saved, but you can still retrieve the individual lines by
traversing the array. For example:
foreach elem ($a:q)
echo "$elem"
end
Note that replacing $a:q by "$a" does not work.
The fact that I've been using tcsh for many years, and I had
to resort to trial and error, even after reading the man page,
to write that loop, should tell you something.
Whatever it is you're doing, if I were doing it, I'd probably
use Perl.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21474)
|
3/29/2011 9:53:59 PM
|
|
|
16 Replies
462 Views
(page loaded in 0.142 seconds)
Similiar Articles: comp.unix.shellhow to prevent back quote expansion? 15 296 (3/26/2011 7:55:36 PM) The following script will output one line. the carriage return is removed by the back quote operation. regutil (Regina) SysFileTree(): requires command line parameter in ...... command line consists of the following: ./myProc a.txt b.txt c.txt To prevent this automatic shell expansion enclose the file mask in either single or double quotes ... FS = , is error? - comp.lang.awkI've gone to look for myself. If I should return before I get back, keep me here. ... than any other WinDOS, I suppose; it's the WinDOS inherent quoting (and quote expansion ... Frame synchronization - comp.dspA extracted quote from the patent: "The present ... eat into the inter-frame gap - enough to prevent sharing of computational blocks between two back-to-back ... Direct3D vs. OpenGL - scene graph libraries - comp.graphics.api ...Regarding games, when you write the sequel or expansion, do ... Too large textures / viewport: how to prevent slowdown? 17 ... tracking back the mouse in a perpective projection ... PIX receives one ping, then drops - comp.dcom.sys.cisco... case, Cisco has built-in various protection to prevent ... User Port expansion - comp.sys.cbm I'm thinking that can ... Solaris 9 CD and the other ... 4 didn't compile back ... IDS 11.50.xC6 is out... - comp.databases.informix> > > > OR better get the old scripts back not java install. ... br> <br> <br><br><div class=3D"gmail_quote">On Tue ... how to use a trigger to stop an insert/update? - comp ... SQL Server insert datatype problem - comp.soft-sys.matlab ...... is a way to get a primary key value back from an INSERT ... ... code to use SQL Server ... sqlite - comp.os ... to stop ... Single quotes in SQL statements over JDBC - comp.databases ... bad pointer exception - comp.lang.c++BTW, they halfheartedly went back on that decision ... Is there absolutely no way to prevent the program from ... To give you a quote from (a draft copy of) the C++ ... .g64 images - comp.sys.cbmBut Michael Klein describes on his pages a 8K RAM expansion ... work as I don't really see anything that could prevent it. ... (always, for most tasks :) You guys are getting back ... How to remove all subdirectory/file under current directory ...But if you want the original rm behaviour back (i.e ... to the beginning of the generated filenames by expansion ... simple, easy to implement, and will _probably_ prevent a ... Invisible window, like console app - comp.os.ms-windows.programmer ...... do their best to be shown, even if you try to stop it ... the main app responds by posting a WM_USER message back ... ms632599%28VS.85%29.aspx#message_on= ly I'll quote ... How many of one string in another - comp.databases.mysql ...I hope you never have to come back and troubleshoot ... You should post as you did now - after Jerry's quote. ... soft-sys.matlab How to Copy Strings ... How do I prevent ... Java function that will convert an arbitrary base to a decimal ...... Finding a bug is a sign you were asleep a the switch when coding. Stop debugging, and go back ... int i = Integer.parseInt( g.trim(), 36 /* radix 2 to 36 */ ); </quote ... Arithmetic coding and continued fractions - comp.compression ...Many say quote a paper on it. Unless they see a Moffat ... given "real" > number, then thecontinuedfraction expansion ... Instead of doing back-multiplication and decimal ... How To Stop Leg Hair From Growing Back So Fast | LIVESTRONG.COMIf you decide to undergo laser hair removal, get a quote from your ... How to Stop Back Hair Growth How To Stop Back Hair Growth | LIVESTRONG.COMHow To Stop Back Hair Growth. The way in which you stop back hair growth ultimately depends on your wallet. You first need to decide how much you're willing to spend ... 7/22/2012 2:49:57 PM
|