Just deleted tons of stale, unsused user accounts. Here are some KSH shell basics and special shell variable. I need to Google them up myself to get reminded that ‘$?’ is the output variable for script execution. That is, it’s 0 for successful execution and 1 if there is any error.
Category: Scripting
My Favorite One-Liners: Some Useful Shell Programs… But first things first… I would like to note that this aint my list of favorite shell programs.. well, it has become… but what I mean is this aint an original article that I created and posted here.. this is a section from the now defunt, Sysadmin Magazine… and you’ll be reading a lot of those here….
“Keep It Simple, SysAdmin,” or perhaps just “Keep It Shell Script”, that is one of the motto that every sysdamin knows… They automate, they script, they relax.
Too many system administrators, especially former or frustrated programmers, get involved and distracted creating ad hoc programs that grow until they have a life and a following of their own. These programs generally are written in C, awk, or C shell, have no documentation, and are incomprehensible, even to the author, less than two weeks after being finished.
Ever wonder how to increment a number inside a variable in a bash or korn shell? If you’re the geeky type of guy, I’m sure you know how..
Well, this quick post is sort of like a note to self.. I jsut want to have it handy in case I need it again.. and for sure, I will need it again..
Anyway, hours are spent searching for the correct format or syntax on the variable, so here it is:
while [[ $NUMBER != $CNTR1 ]]
do
# Separation.. one file each line
#
head -$CNTR1 list | tail -1 > ./processing/$FILENAME.$CNTR1
# moved (or copy) ‘feedme’ as ‘list’
cp ./processing/$FILENAME.$CNTR1 ./processing/list
## ADD the ‘t’ and ‘x’ logic here
##
awk ‘{print $1}’ ./processing/list > ./processing/cutfile
ACCT=`cut -c1 ./processing/cutfile`
if [ $ACCT != 1 ]
then
PREFIX=’t’
else
PREFIX=’x’
fi
UID=`awk ‘{print $1}’ ./processing/list`
echo “UID is $PREFIX$UID”
NAME=`awk ‘{print $2}’ ./processing/list`
cat ./processing/list
echo $PREFIX
## END OF T and X Logic
## CHECK if user exists or not
## If New User, Create it
##
cat /etc/passwd | grep $UID
return=$?
if [ $return = 0 ]
then
echo “User account already exist”
exit
else
useradd -d $HOMEDIR$PREFIX$UID -g $GROUPMEM -G $SUPGROUP -s $USERSHELL -c “$COMMENTS$NAME `date ‘+%d%b%y’`” -m $PREFIX$UID
#echo $HOMEDIR$PREFIX$UID $GROUPMEM -G $SUPGROUP -s $USERSHELL -c “$COMMENTS$NAME `date ‘+%d%b%y’`” -m $PREFIX$UID
echo “$PREFIX$UID:telus2009″ | chpasswd
#echo “$PREFIX$UID:telus2009″
echo “Let’s continue”
fi
CNTR1=$(($CNTR1+1))
sleep 1
done
It’s the bold and red text… Hope it helps someone.
I’m thinking of doing a series of post that will talk about Shell Scripting, scripting tutorial, examples, application… the whole nine yards of Unix/Linux shell scripts/programming.
The topic of shell scripting is a very broad topic and posting one super long post here will take me all day.. or weeks, depending on my mood.
and internet searches.
So, this will be the start of a series of post about Shell scripts and shell programming. Every new post about scripting will be placed below, with link to the appropriate post.
And did a trend search for shell programming, shell scripts, shell tutorial and it’s alright. Though the volume of searches has been decreasing but that’s alright. There are still some.
Perfect!
The last post, was used to chmod a list of home directory.
I used another application for that ‘for..do’ loop again… it’s climbing my favorite script command list..
second only to ‘while…do’..
for X in `ls -last | grep 2006 | awk ‘{print $9}’`
do
rm -rf $x
done
bye bye 2006 directories!
The Bourne shell for loop looks like this:
for arg in list do ...Handle $arg... done
Real life application..
# ls -l | awk ‘{print$9}’
user01
user02
user03
user04
user05
user06
user07
user08
user09
# pwd
/home
# for x in `ls -l | awk ‘{print$9}’`
> do
> chown $x $x
> done
# ls -aslt
total 136
.. yeah you know what the output is..
draft……. note to self
#!/bin/bash -x
FILE=”/tmp/IAP_sched”
DIR=”/PRA/prod/files/nz/area11/logs/”
FILE2=$(awk ‘{ print $1 }’ $FILE)
echo “*** Nameserver IP ***”
for ip in $FILE2
do
echo “Doing GTL on” $ip
/PRA/prod/scripts/arc/get_log_times *$ip* >> /tmp/output.txt
done
*alias command is not recognize.. hence the full path of ‘glt’
Just want to document it here. A colleague request to kill multiple PID owned by different users. They don’t have any special privilege to kill process other than their own.
Here’s a simple for…loop script that will look for and kill each PID it found.
#!/bin/sh -x
ps -ef | grep nobody | awk ‘{print $2}’ > /tmp/PID
filePID=”/tmp/PID”
for PID in `cat $filePID`
do
kill -9 $PID
done
#
Line 1, tells which shell will execute the script
Line 2, gets all the PID to be killed and store them on a file.
Line 3, assigns a variable for the file
Line 4 to 7 is the for…do loop which basically gets each line on the file, store the value on $PID and then kill whatever value is in it on line 6.
Yucky script. Post a better one.
Thanks.