The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed remove last 10 characters of a line start from 3rd line minifish Shell Programming and Scripting 7 03-26-2008 04:42 PM
please help to parse the line ajaya Shell Programming and Scripting 1 05-11-2006 02:15 AM
please help to parse the line ajaya Shell Programming and Scripting 1 05-10-2006 10:11 AM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 03:36 PM
How to parse a line? edefoe Shell Programming and Scripting 2 12-22-2005 06:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-23-2007
Malumake Malumake is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
Question SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions, I need have a basic permissions line added to their permission file. I am creating this script so our helpdesk guys can use it later because I can see something similar happening again.

Everything on the following script works correctly unless the user has no permissions left. The sed statement will display the correct insertion but will not overwrite the file. I have also tried piping it to cat and then redirect to the file but that just appends....Please Help!!

Oh! BTW this is in Korn shell under AIX 5.3
Also FYI permission lines are always first in the file and they are the only lines that start with numerals.

Code:
echo "Where is the user Listing?"

read usrList

usrName=""

fChar=""

while [ 1 ]

do

  read usrName || break

      sudo sed -e '/APCLERK/d' /home/$usrName/.facet | cat >> /home/testdir/$usrName
  
      fChar=`head -c 1 /home/testdir/$usrName`

          if [[ $fChar != [0-9] ]]

          then

            sudo sed -f /home/testdir/sedInqIns.sed /home/testdir/$usrName > /home/testdir/$usrName

          fi                         

      


done < $usrList
Here is the sed insert file::


Code:
1,1{
 i\
1       /PRISM/SD-START "INQUIRY~      ; ; ; ;Y;   ;    ;0000;      ;    ;Y;      ; ;      ;     ~~"
 }

On execution of this script the users that have no permissions are left with a completely blank file (there should be other settings in the file besides permissions). The users that have other permissions are fine.

Any help will be greatly appreciated.
Mahalo!!

Last edited by Malumake; 10-23-2007 at 09:17 PM.. Reason: Additional Information
  #2 (permalink)  
Old 10-24-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
I suspect the problem to be the following:

After changing the file /home/$usrName/.facet and writing the results to /home/testdir/$usrName (so far ok) you try to modify this file writing back to it immediately. This is FORBITTEN! Something like

sed 's/this/that' file > file.new

works, but

sed 's/this/that' file > file

won't. The reason is: sed will read the first line of the input file (which is "file"), apply any rules that might be applicable and then write the result (the changed or not changed line) to <stdout>. This <stdout> is at this moment pointing to "file" and this is why after the first line the file will contain nothing more than exactly that first line - so sed encounters the EOF and thinks it is finished. In the first example it is not writing back to its input file but some other file "file.new", which is why there is no problem.

There is another (perhaps minor) detail i found: You read from keyboard a variable "usrList" and do nothing with it. Why?

bakunin
  #3 (permalink)  
Old 10-24-2007
ajcannon ajcannon is offline
Registered User
  
 

Join Date: Aug 2007
Location: Binfield, Berkshire. UK
Posts: 91
To Bakunin

just a minor bit of pedantry - usrList is being read via the '< usrList' statement at the end of the 'read.....' clause - or so it seems to me......???!

cheers
  #4 (permalink)  
Old 10-24-2007
ajcannon ajcannon is offline
Registered User
  
 

Join Date: Aug 2007
Location: Binfield, Berkshire. UK
Posts: 91
Whoops!

Sorry - I take that back - it doesn't seem to be being used at all. I should have kept my mouth shut!
  #5 (permalink)  
Old 10-24-2007
drl's Avatar
drl drl is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 711
Hi
Quote:
Originally Posted by bakunin
The reason is: sed will read the first line of the input file (which is "file"), apply any rules that might be applicable and then write the result (the changed or not changed line) to <stdout>. This <stdout> is at this moment pointing to "file" and this is why after the first line the file will contain nothing more than exactly that first line - so sed encounters the EOF and thinks it is finished.
I would have explained this behaviour somewhat differently: when the shell begins to process a command line, it will literally remove the characters that involve re-direction. In dealing with re-direction, the shell will "zero" a file that is being re-directed to. If there was data on the file, it will have been erased before the command is executed. The result is that the command -- in this case sed -- will never see any input, because it already has been destroyed. The first read will encounter EOF.

Most of us have fallen into this operational trap. For example, we often want to sort a file onto itself -- just re-ordering data, neither gaining or losing data. So sort has an option to write the output to the input file, but one must use "-o file", and not "> file". The GNU sed allows a similar construction with "in-place" editing, but what actually happens is that sed writes to a temporary file and then renames it at the end. You can see this if you display the inode number (ls -li file) before and after the operation. You'll see that they are different.

The flow-chart of shell operations is displayed in some books, for example, O'Reilly's Learning the bash shell, 2nd, page 177 ff ... cheers, drl
  #6 (permalink)  
Old 10-24-2007
Malumake Malumake is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
Clarification

Believe it or not usrList actually feeds line by line into the do loop.

So, what I'm getting, is that in order for this to work correctly, I need to feed the intial sed statement to a temp file and then have the second sed use the temp back into the original?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0