• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Saturday, September 19, 2009

Posted by venu k
12 comments | 10:11 AM
#!/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