Groups

    Unix Help Please

    I have to create the following if anyone can help?

    Prepare a shell script that will prompt for the name of a directory and then list the files in it to the screen. Send error messages to a separate file called 'errors'.


    Anyone could help be greatly appreciated as I have been trying a for a few hours...

    9 Comments

    Original Poster

    anyone?

    Are you writing it in bash?

    When you say prompt for the name of the directory, do you want the user to input the path or do you want to pick it up from pwd?

    What error messages are you referring to? Do you mean if the directory is empty or the directory doesn't exist?

    It seems like a pretty simple script just need to clarify the points above.

    Original Poster

    yes bash,

    User just enters the name of a directory, dont think its meant to be the path (though that would make it easier)

    The error message of if the directory does not exist, If the directory exists but is empty that would be fine to allow it to run and just not list anything

    Take a look here and see if any of it helps: linuxconfig.org/Bas…ial

    In particular, look at "6. Reading User Input" as a starting points. You could adapt some of the code there to take input from the user for the dir name.

    As a starting point (I'll leave it to DVS_Dee though as he sounds like he knows more about this stuff than me)

    #!/bin/bashecho -e "Please type a directory name: \c "read someDir# now … #!/bin/bashecho -e "Please type a directory name: \c "read someDir# now the variable $someDir will be what the user entered, use it in conjunction with ls or something.

    Original Poster

    well I wrote it as:

    clear
    echo "Please input a directory"
    read DAT
    cd DAT
    ls
    cd ~


    But I cant work out the error handler and that will only look in the current directory for the file and though that will do if needs be I would rather look in all files for it and I tried using Find but I dont want it to list all the . files (the system files etc.)

    Yeah oldmanhouse is pointing you in the best direction to get started. "read" does exactly what it says on the tin, at this point the user can input the directory. Store this as a variable. Use "ls" to list directory contents (nb if directory's empty ls should return exit status 1). I think you've got enough info to point you in the right direction and as it seems like this is a homework assignment there's no point in me doing it for you as you won't learn that way.

    Just start writing bits of your script and testing the results to see if you get the desired outcome or not, that's the 'fun' part of scripting.

    Let us know if you get stuck at a specific part.

    Good luck!

    Original Poster

    above is what i tried...

    Original Poster

    and its coursework I dont wana do after these couple bits...

    jamhops

    above is what i tried...



    What you've got is cool, but just add some checking incase the directory doesn't exist.

    So something like:

    clear
    echo "Please input a directory"
    read DAT
    \rm ~/errors
    touch ~/errors

    if [ -d $DAT ];then
    cd $DAT
    FILES=`wc -l`
    if [ $FILES -ge 1];then
    ls
    else
    echo "Directory $DAT is empty"
    echo "Directory $DAT is empty" > ~/errors
    fi
    else
    echo "Directory $DAT not found"
    echo "Directory $DAT not found" >> ~/errors
    echo -n
    echo "See ~/errors for errors"
    echo -n
    fi

    cd ~

    Post a comment
    Avatar
    @
      Text
      Top Discussions
      1. Just heard this...2 ★★★★★★★★★★★★★★ congrats to all on 392k ★★★★★★★★★★★★★★7767228
      2. Fifa 18 web app Now active.11
      3. The HUKD Wuntu code sharing thread! (Three Mobile)40107
      4. I want to talk about the WEATHER no politics no religion19047102

      See more discussions