Register
It is currently Thu Dec 14, 2017 7:02 am

braces {} with read and cat?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Tue Jan 19, 2016 8:29 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
Hi

I came across the following code:
Code:
$ opt="-ltr"
$ len=10
$ ls $opt | {
> read
> cat
> } | head -n${len}

I don't quite understand how read and cat can be on their own within braces on seperate lines.
Does the result of the "ls" go into the default REPLY variable, but then how does cat show REPLY?


Top
 Profile  
 PostPosted: Tue Jan 19, 2016 10:11 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
If you can't get, what a piece of code does, execute it.

The result of posted code is:
Code:
head: option requires an argument -- 'n'
Try 'head --help' for more information.


Use set -vx to see what bash does with the line of code given


Top
 Profile  
 PostPosted: Tue Jan 19, 2016 11:14 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
thanks for set -vx

output is:

Code:
$ls $opt | {
ls $opt | {
> read
read
> cat
cat
> } | head -n${len}
} | head -n${len}
+ read
+ ls --color=tty -ltr
+ head -n10
+ cat
-rwxr-xr-x 1 applmgr oinstall 10615 Dec  7 10:19 Backout_Dep.sh
-rw-r--r-- 1 applmgr oinstall 12768 Dec  7 10:20 config.cfg
-rw-r--r-- 1 applmgr oinstall 14540 Dec  7 10:20 config_backout.cfg
-rwxr-xr-x 1 applmgr oinstall 10589 Dec  7 10:22 EBS_Dep.sh
-rwxr-xr-x 1 applmgr oinstall   431 Dec  7 10:37 setoravars.sh
-rwxr-xr-x 1 applmgr oinstall   251 Dec  7 10:39 setreleasevars.sh
-rwxr-xr-x 1 applmgr oinstall  2100 Dec  7 15:05 ebs01.sh
-rw-r--r-- 1 applmgr oinstall    73 Dec  7 15:06 oravars.dat
-rwxr-xr-x 1 applmgr oinstall  3706 Dec 14 10:03 rgl_deploy01.sh
-rwxr-xr-x 1 applmgr oinstall  3852 Dec 14 11:42 rgl_deploy.sh
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"
++ echo -ne '\033]0;[email protected]:/ns/apps/ist/deployments/ebs/Branches/CUSTOM/bin/.sh'


still not sure how cat is able to output all files ?
Seems its a way of limiting the output of ls to only 10, so I understood that part, I also understand that the result of ls is read into $REPLY, but how does cat simply display the contents of $REPLY?


Top
 Profile  
 PostPosted: Tue Jan 19, 2016 11:43 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Mea culpa.
Forgot the len=10 line and didn't even consider reading the code.

Try this:
Code:
opt="-ltr"
len=10
ls $opt | {
   read
   echo REPLY is $REPLY
   cat
} | head -n ${len}


And then comment out the read like this
Code:
opt="-ltr"
len=10
ls $opt | {
   #read
   echo REPLY is $REPLY
   cat
} | head -n ${len}
This shows that the read just takes place one time.
It reads the first line of output of the ls command.. (which is something like is total 564 )
As the $REPLY is NOT used effectivly supressing the output of this line.

Then the cat does it's work.

The "{" is a block command internal to bash.
It just groups commands to a single block.
Hence the entire block is redirected to head
Read help "{"

A kind of obscure filtering out the line having is total <some number>
Do not write things like that.
Lots of folks will struggle to get, what you want to do.


Top
 Profile  
 PostPosted: Wed Jan 20, 2016 4:48 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
thanks, I understand the code now.

I guess something like
Code:
ls -l | tail -n+2 | head -n10

or
Code:
ls -l | grep -v '^total' | head -n10

is a better way of doing what I'd like, the former is probably a bit more efficient.


Top
 Profile  
 PostPosted: Wed Jan 20, 2016 10:03 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
grep has a -matchcount option.
Code:
time  ls -l | grep  -m 10 -v '^total'

You can prefix any command with time to see how much time a command spends in user and in kernel space.


Top
 Profile  
 PostPosted: Thu Jan 21, 2016 3:35 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
excellent, even better!!!!
and thanks for the tip about time!
learning so much from you. many thanks.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP