Saturday, September 19, 2009

Bash Script to Check whether a user has an account in your system

#!/bin/bash
# validuser.sh
# Usage: validuser.sh username
# Script to check whether suplied user has an account in your system

if [ $# -ne 1 ]
then
echo "You supplied wrong arguments"
echo "Usage : `basename $0` user_name"
exit 1
fi

#grep -w "$1" /etc/passwd > /dev/null
grep -w "^$1" /etc/passwd > /dev/null
if [ $? -eq 1 ]
then
echo "$1 is not a valid user"
else
echo "$1 is a valid user"
fi

# Using greps -q option you can simplify and faster your script
# if `grep -qw "^$1" /etc/passwd`
# greps -q option prints nothing just returns exit status 0 or 1

Out Put:
[root@localhost shell]# sh validuser.sh
You supplied wrong arguments
usage : validuser.sh user_name
[root@localhost shell]# sh validuser.sh venu
venu is a valid user
[root@localhost shell]# sh validuser.sh venuk
venuk is not a valid user
[root@localhost shell]# sh validuser.sh root
root is a valid user
[root@localhost shell]# sh validuser.sh roott
roott is not a valid user
[root@localhost shell]#

12 comments:

  1. Hi Dear,
    My name is kannan.I am the member of orkut bash community.I saw ur script.i exe ur script like this "sh validuser.sh venu".I got o/p "venu is a valid user".But their is no user like venu in my box.sample passwd entry is given below. shell:x:12354:12356:venu:/home/shell:/bin/bash
    I suggest to use grep -w ^$1 /etc/passwd in ur script
    regards
    kannan v

    ReplyDelete
  2. Thank you kannan.
    I concentrated on whole words.But I forgot there is a comment field in /etc/passwd file.I found the error by changing the comment field like bellow

    [root@localhost shell]# usermod -c "venu madhav reddy" sai
    [root@localhost shell]# sh validuser.sh madhav
    madhav is a valid user

    But madhav is not a valid user in my system.

    I updated the script as kannan pointed out.Then output is

    [root@localhost shell]# sh validuser.sh madhav
    madhav is not a valid user

    ReplyDelete
  3. In bash (or any POSIX shell) there is no need to use the external command, basename. It can be done by the shell:

    printf "%s\n" ${0##*/}

    The -w option to grep is not standard. Many version do not have it. Instead, use:

    grep "^$1:" /etc/passwd

    ReplyDelete
  4. hello dear
    i am sahil doing bca
    can u give me some shelle scipts
    1)which inform as soon as a specified user whose name is given along command line is logged on into system
    dude will it be done with who|grep?as u showen in example ?

    2)program which takes a file name and prints its size ?

    please mail me at sahil.pahwa@live.com

    one request more can u tell me what all are other program can be possible ?which can come in my exam day after tomm

    ReplyDelete
  5. hello venu
    I am trying to run a bash shell script from the last two days but it is not running can u please help me in this:
    #!/bin/bash -x

    # minimum time gap between phases
    min_time=15

    #set loop for all stations/events folders
    data="/nfs/grad/mohit/Data"
    GoodData=$data/GoodData

    cd $data
    #stns=`ls -d $data`
    stns=$data/*SKS


    for station in $stns; do
    echo "working on directory $station"
    events=$station/Event*
    for event in $events; do
    echo "working on event direcotry $event"
    cd $event
    sacfiles=$event/*.sac
    for sacfile in $sacfiles; do

    t0=`saclst t0 f $sacfile | awk '{print $2}' `
    t1=`saclst t1 f $sacfile | awk '{print $2}' `
    t2=`saclst t2 f $sacfile | awk '{print $2}' `
    t3=`saclst t3 f $sacfile | awk '{print $2}' `
    t4=`saclst t4 f $sacfile | awk '{print $2}' `
    t5=`saclst t5 f $sacfile | awk '{print $2}' `
    t6=`saclst t6 f $sacfile | awk '{print $2}' `

    diff1=`echo "scale=4; sqrt(($t1 - $t0)^2)" | bc`
    diff2=`echo "scale=4; sqrt(($t2 - $t0)^2)" | bc`
    diff3=`echo "scale=4; sqrt(($t3 - $t0)^2)" | bc`
    diff4=`echo "scale=4; sqrt(($t4 - $t0)^2)" | bc`
    diff5=`echo "scale=4; sqrt(($t5 - $t0)^2)" | bc`
    diff6=`echo "scale=4; sqrt(($t6 - $t0)^2)" | bc`
    if[$diff1 -ge 15;then:;
    echo sks and s are not overlapping
    elif[$diff2 -ge 15]
    then
    echo sks and scs are not overlapping
    elif[$diff3 -ge 15]
    then
    echo sks and skks are not overlapping
    elif[$diff4 -ge 15]
    then
    echo sks and skiks are not overlapping
    elif[$diff5 -ge 15]
    then
    echo sks and skIks are not overlapping
    elif[$diff6 -ge 15]
    then
    #mkdir $GoodData/$station/$event
    # cp *.sac $GoodData/$station/$event/*.sac
    echo "i would copy *.sac to $GoodData/$station/$event/*.sac"
    else
    echo phase are overlapping
    fi
    done
    done
    done

    ReplyDelete
  6. sorry mohit
    Too many syntax errors and I don't know saclst command,
    I think it is a perl command or package.

    ReplyDelete
  7. hi.....i need a shell script to trace function calls and flow of execution,given a c program file as input......please help me with this problem

    ReplyDelete
  8. i got the valid user but i need home directory along with valid user

    ReplyDelete
  9. Really nice article its tell us how to to main-tan scripts with easy way thanks for share it java programming homework help .

    ReplyDelete
  10. What a way to approach. Have you heard of such stories? If so, great. Thanks !

    ReplyDelete