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 ...
- 04-03-2008 #1Just 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?
- 04-03-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
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:
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.if [ ! -f <filename> ]
then
<do something>
fi
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.
- 04-04-2008 #3Just 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
- 04-04-2008 #4Linux 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
- 04-08-2008 #5Just 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?
- 04-08-2008 #6
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 :'(" fiDISTRO=Arch
Registered Linux User #388732
- 04-08-2008 #7Just Joined!
- Join Date
- Apr 2008
- Location
- new york
- Posts
- 15
ye man thanks alot thats pretty clsoe to whwta im trying to get at.


Reply With Quote
