Find the answer to your Linux question:
Results 1 to 7 of 7
hey guys, im making a practice script right now, so far it jsut demands the user for a filename. how would i make it say an error msg on teh ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Location
    new york
    Posts
    15

    script help

    hey guys, im making a practice script right now, so far it jsut demands the user for a filename.
    how would i make it say an error msg on teh screen if the file does not exist, and how do i make it count the number of lines in teh file?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by sk8rrr View Post
    hey guys, im making a practice script right now, so far it jsut demands the user for a filename.
    how would i make it say an error msg on teh screen if the file does not exist, and how do i make it count the number of lines in teh file?
    To count lines, you can use "wc -l <filename>", if you want only the number, you can pipe it like "cat <filename> | wc -l".

    To do something if a file doesn't exist, you can use this in bash:

    if [ ! -f <filename> ]
    then
    <do something>
    fi
    Note however, that that check will also work if <filename> is a directory, a symlink, a socket, a device node or anything that is not strictly a regular file, which might or might not be convenient.

    You can add more checks to see if that's the case and so the script would act consequently. Read the bash man page for additional info on that things, and ask here if you need more info.

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Location
    new york
    Posts
    15
    k so , so far i got
    #!/bin/bash

    echo -n "Please enter XHTML filename: "

    read filename

    if [ ! -f <filetest> ]
    then
    <correct>

    else
    <incorrect>
    fi


    -it asks for file name, when entered, it says error ,no matter on filename, and also says echo <correct>. how do i set the variable that only the file called "tester1" is teh corect one, and all others get teh error

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    I am not sure if I understand it correctly, but maybe what you want is something in the lines of

    if [ "$filename" == "tester1" ]
    then
    <foo>
    else
    <bar>
    fi

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Location
    new york
    Posts
    15
    thanks for attempt, i figured it out, possibly the easy way. works now to ask for file and show the number of lines.

    is there a way to make it so, each time a filename that is not existant is entered there is a custom error message? or will it always be a standard message for error?

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Yeah. How are you doing it now?
    Code:
    read filename
    
    if [ -f "$filename" ]; then
        echo "Congrats, the file exists!"
    else
        echo "The file $filename does not exist :'("
    fi
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Apr 2008
    Location
    new york
    Posts
    15
    ye man thanks alot thats pretty clsoe to whwta im trying to get at.

Posting Permissions

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