The UNIX and Linux Forums  

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
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 04:59 PM
remove the text (*@yahoo.com, hotmail.com) from one file juniorbear Shell Programming and Scripting 11 06-15-2008 08:37 AM
How to remove FIRST Line of huge text file on Solaris madoatz UNIX for Dummies Questions & Answers 5 06-23-2007 02:19 PM
read list of filenames from text file, archive, and remove fxvisions Shell Programming and Scripting 5 03-20-2007 09:56 PM
how to remove ^M from windows text file in unix pragy1999 Shell Programming and Scripting 1 04-13-2006 11:04 AM

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 06-19-2008
raidzero's Avatar
raidzero raidzero is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 60
remove specified text from file

I am trying to write a script that kills old sessions, I've posted here over the past few days and the script is just about perfect except I want to be given the option to exclude specified PIDs from being killed. this is the entire script:

Code:
if [ -e /tmp/idlepids ]
then
rm /tmp/idlepids
fi
if [ -e /tmp/idlepidss ]
then
rm /tmp/idlepidss
fi
if [ -e /tmp/unwantedpids ]
then
rm /tmp/unwantedpids
fi
numusers=$(who -q | grep Total | tr -s " " | cut -d" " -f3)
echo Total number of sessions: ${numusers}
if test $numusers -gt 60; then
diff=$(expr $numusers - 60)
if test $diff -eq 1
then
echo "There is $diff too many sessions. Killing the oldest session."
else
echo "There are $diff too many sessions, killing the $diff oldest sessions:"
fi
who -u -H | sort -r -k6.2,6 | grep bhb | tr -s " " | cut -d" " -f7 >> /tmp/idlepids
head -$diff /tmp/idlepids >> /tmp/idlepidss
echo "About to kill the following sessions:"
while read PID; do who -u | grep $PID | sort -k4.2,6; done</tmp/idlepidss
#echo "Press ENTER to continue..."
#read enterKey
echo "Last chance to save PIDs..."
echo "Enter up to 9 PID numbers you want to keep alive: \c"
read pid1 pid2 pid3 pid4 pid5 pid6 pid7 pid8 pid9
cat /tmp/idlepidss | grep -v $pid1 | grep -v $pid2 | grep -v $pid3 | grep -v $pid4 | grep -v $pid5 | grep -v $pid6 | grep -v $pid7 | grep -v $pid8 | grep -v $pid9 > /tmp/unwantedpids

while read PID; do kill $PID; done</tmp/unwantedpids
echo "Sessions killed. Exiting"
else
echo "There are less than 60 users. Exiting"
fi
exit 1

It gives a bunch of grep usage errors
I tested grep -v on the command line and it works.

Code:
# cat /tmp/idlepidss
43532
35134
49276
# echo $pid1 $pid2 $pid3

# read pid1 pid2 pid3
43532 35134
# cat /tmp/idlepidss | grep -v $pid1 $pid2
grep: 0652-033 Cannot open 35134.
# set -o vi
# cat /tmp/idlepidss | grep -v $pid1 |grep -v $pid2
49276
#

  #2 (permalink)  
Old 06-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Probably a quoting issue; if $pid2 is the empty string then you get a syntax error for grep -v $pid2 but it's easy to fix: grep -v "$pid2" with double quotes around the variable (always when using variables, as a strong recommendation). However, you also need to watch out for grep -v "" because it will remove all lines. As a workaround, you could use "${pid2:-nosuchstringmadam}" to replace an empty variable value with an unlikely default string.

As an aside, you can also simplify the long string of greps with egrep -v "$pid1|$pid2|$pid3" etc. And of course, cat file | grep something is always possible to write as grep something file (the purpose of cat is to catenate multiple files together; using it on a single file is usually misdirected).
  #3 (permalink)  
Old 06-19-2008
raidzero's Avatar
raidzero raidzero is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 60
Thanks a lot, worked great
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 06:22 AM.


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