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.
Um Yes!
Wow!
Thank you!
Yes, that did help. There was a script I was trying to use (written by someone else), and they had the syntax wrong. I know practically nothing about scripting, but this gave me enough to get it working, thanks!
In my bash scripts I use ((i++)) and this works just fine for me
Thank you. That did help