LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-06-2005, 05:12 AM   #1
YaK.
LQ Newbie
 
Registered: Nov 2005
Posts: 6

Rep: Reputation: 0
script: 'for' and 'cut' problems


Hello,

I have two problems with making a shell script.

1- If with a 'ls -lia' I got, for exampl, this result:
Code:
 98487 -rw-r--r--   1 me me    2 Nov  5 12:59 prova1  
100458 -rw-r--r--   1 me me    2 Nov  5 12:59 prova2  
100460 -rw-r--r--   1 me me    2 Nov  5 12:59 prova3  
 98616 -rw-r--r--   1 me me    2 Nov  5 12:59 prova4  
 98617 -rw-r--r--   1 me me    2 Nov  5 12:59 prova5
then, if I make in a shell script a
Code:
for abc in `ls -lia`
do .....
the value of abc first is '98487', then '-rw-r--r--', then '1', then 'me'....
Is it possible to make a 'for' storing in abc a whole line each time (first 98487 -rw-r--r-- 1 me me 2 Nov 5 12:59 prova1 , then 100458 -rw-r--r-- 1 me me 2 Nov 5 12:59 prova2 .... ) ?


2- The second question is a problem with cut: I have already red the cut man pages but I can't find anything to select from 'ls -lia' only one column (for example the protections). 'ls -lia | cut -f1 -d\ ' doesn't work, it returns 100458 and 100460 for the first column; 98487, -rw-r--r--,-rw-r--r--,98616,98617 for the second .... I could make 'ls -lia | cut -b1,2,3,4,5,6' to select the first column, but this only works if I know how large it is. If I wanted to select, for example, the name column I don't know if it is from -b22 to 28 or -b50 to 100....

Thx for help

Last edited by YaK.; 11-06-2005 at 05:13 AM.
 
Old 11-06-2005, 05:43 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you would change the IFS variable (Input Field Seperator) to "\n" easiest is to set it within the backticks. in fact backticks are slightly obselete now, so try using $(IFS="\n" ls ... ) instead.
 
Old 11-06-2005, 05:49 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
oh no, hang about, that IFS is going to be in the wrong scope.... use "IFS="\n" for i in $(ls...)"
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I cut something between quotes? jfall Linux - General 2 02-09-2005 09:03 PM
removing php font tags for cut and paste of script? rioguia Linux - Newbie 0 10-16-2004 07:23 AM
How to use cut in Perl script? mimf Linux - General 1 12-01-2003 04:21 AM
shell script - cut off the '^M' d-fens Programming 5 07-20-2003 11:20 AM
using "cut" in perl script armegeden Linux - General 3 03-06-2003 09:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:28 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