Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

.#! bin/sh

  • 07-05-2004 9:54pm
    #1
    Closed Accounts Posts: 3


    How is it going there !!

    I is a newbie .... and need help (dont they always:) )

    I am stuck on a UNIX command, can you help ?

    #!/bin/sh

    I want to run a script from the terminal.

    My script is "ScriptName" and i pass in an argument aswell

    ./ScripName UserName

    How do i check is $* a vaild user on the system, and can i go to his home directory from the pwd ?

    I just need to count the number of files that he has in his home directory.



    I am a novice and i'm just stuck !!


Comments

  • Closed Accounts Posts: 3,354 ✭✭✭secret_squirrel


    Offhand I would say
    just

    cd ~$1 ## cd to users home directory

    if [ $? ne 0 ] ; then ## check condition code.

    print "Specify a valid username as a parameter"

    fi

    You might want to doublecheck the syntax on that its off the top of my head

    [edit] second thoughts using the [ ~$1 -d ] test (is parameter a valid directory) might be a better check. Again double check the syntax. Both those will work on korn shell and prolly bash as well.


  • Closed Accounts Posts: 3,354 ✭✭✭secret_squirrel


    For the file count something along the lines of ls | wc -l should work

    (pipe the results from the directory list command into the word count command. -l flag means count no of lines.

    You could also use a for loop to loop around the filename list from ls if you actually wanted to do something with the file names. If not it would be overkill.

    My advice get unix scripting in a nutshell by o'reilly.


  • Closed Accounts Posts: 3 ifgrounds


    Great stuff .... but just to be sure, if anyone can confirm, it would be great !!!

    I totally impresssed with this board ... just gone back a few pages and it is a pipeline to knowledge, .... result !!


  • Closed Accounts Posts: 3 ifgrounds


    Originally posted by secret_squirrel
    For the file count something along the lines of ls | wc -l should work

    (pipe the results from the directory list command into the word count command. -l flag means count no of lines.

    You could also use a for loop to loop around the filename list from ls if you actually wanted to do something with the file names. If not it would be overkill.

    My advice get unix scripting in a nutshell by o'reilly.


    Yeah, cool ... i have the rest just needed that bit (note to self, dont speak to soon fella :) )

    o'reilly you say !! me thinks i'll be checkin' that out !! Hey ... cheers again for the help.


  • Closed Accounts Posts: 395 ✭✭albertw


    Originally posted by secret_squirrel

    cd ~$1 ## cd to users home directory

    if [ $? ne 0 ] ; then ## check condition code.

    print "Specify a valid username as a parameter"

    fi

    Its not strictly true also since you can valid users without a home directory, inaccessable NFS mount for example. Soyou might want to first check for the password entry (getent passwd user), then see if you can open the directory.

    Cheers,
    ~Al


  • Advertisement
  • Closed Accounts Posts: 484 ✭✭ssh


    grep ^$*: /etc/passwd > /dev/null || exit 1

    userdir=$(grep ^$*: /etc/passwd | cut -d : -f 6)

    won't work for NIS. won't work in bogstandard sh.


  • Closed Accounts Posts: 395 ✭✭albertw


    Originally posted by ssh
    grep ^$*: /etc/passwd > /dev/null || exit 1

    userdir=$(grep ^$*: /etc/passwd | cut -d : -f 6)

    won't work for NIS. won't work in bogstandard sh.

    but getent will, it goes throuh the entries in nsswitch.conf in order to find a match, so it will work even if you are using LDAP or NIS+ or something to authenticate. writing scripts to grep passwords from passwd, or matching in yppasswd is just reinventing the wheel.

    as for working in sh, /bin/sh can mean different things to different os's, I'd stick to ksh, that way you know exactly what you are getting.

    Cheers,
    ~Al


  • Closed Accounts Posts: 484 ✭✭ssh


    getent....

    you learn something new every day.

    thanks.

    ksh isn't always there though. The reason mine won't work through bog standard sh is because of the blah=$(blah blah blah)

    that's a bashism, afaik.


  • Registered Users, Registered Users 2 Posts: 432 ✭✭Catch_22


    #!/bin/sh
    grep "^$1:" /etc/passwd || getent passwd $1 || exit 1


  • Closed Accounts Posts: 99 ✭✭QBall


    Originally posted by albertw
    as for working in sh, /bin/sh can mean different things to different os's, I'd stick to ksh, that way you know exactly what you are getting.

    I would have thought that it would be more sensible to restrict yourself to the subset of sh which is spoken by most systems?

    After all, ksh isn't available everywhere where sh is, and so long as you don't eat, sleep and live bash it's pretty easy to get most things done in sh. (IMHO) Of course, I have this opinion because I use Perl for non-trivial scripting tasks. :)


  • Advertisement
Advertisement