Find the answer to your Linux question:
Results 1 to 5 of 5
I am trying to write a bash script that checks for a directory and if the directory is not there creates it. I have everything written but I have no ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Posts
    2

    bash question


    I am trying to write a bash script that checks for a directory and if the directory is not there creates it. I have everything written but I have no idea how to have bash check for a directory.

    example,

    if [/Library/Admin Is not there]; then
    mkdir /Library/Admin
    fi

    can't figure out the part between the [].

    Thanks. for the help.

  2. #2
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Code:
    #!/bin/sh
     
    if [[ -e $1 ]]
            then echo "Directory Exists."
            else mkdir $1
            echo "Directory Created."
    fi
    That will do it with a command line arg. for the directory that you want to check.
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  3. #3
    Just Joined!
    Join Date
    May 2004
    Posts
    2

    logging in user

    I have another bash script that runs at login and is supposed to get the username of the person that is logging in, but it doesn't do it. However, if I run the script from the terminal after I have logged in it gets the username of the person logged in.

    here is what I use to get the username
    LOGINNAME=`/usr/bin/who | /usr/bin/grep console | /usr/bin/awk '{ print $1 }'`

    How can I get the username as they are logging in and not after they have logged in?

    Thanks.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Mar 2003
    Location
    Wisconsin
    Posts
    1,907
    The problem is you're running your script before you're actually logged in.

    Try putting the call to your script as close to the end as possible in the .bash_profile file. If that doesn't work, try it in the .bashrc file...

    I know there's a gui equivilent to .bash_profile, like one of those rc files. I'll see if I can't find it.

    Or you might try a sleep command. I'm not sure if that would make your entire login pause until the command is finished executing or not. If it doent, you can make it sleep for like a minute or two until the login process is finished.

    Jeremy
    Registered Linux user #346571
    "All The Dude ever wanted was his rug back" - The Dude

  6. #5
    Just Joined! bclark4444's Avatar
    Join Date
    Dec 2003
    Posts
    55
    You could use the command:
    LOGINNAME=`id -un`

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •