Register
It is currently Fri Aug 22, 2014 7:43 am

New and needing some help.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sat Mar 27, 2010 8:54 pm   

Joined: Sat Mar 27, 2010 7:06 pm
Posts: 5
I am new and I am having trouble with this on steps 2 and 3. Can anyone help me out here?

1. Write a shell script that asks the user to input the name of a file. The shell
script then tests to see if that file exists, if the file exists and it
is owned by the user the shell echos "The File is there and you own it".

2. If the file is not found, then the shell script calls a function and passes
the first argument to the function "The File Was Not found, Please try Again"
which is then echoed to the user and
another argument is passed to the function "You entered [filename]" which is
also echoed to the user showing the value of the filename variable,
the function then exits with an exit value of 10.

3. If the file is found, but is not owned by the user the shell script calls the
same function and passes the argument to the function "The File Was found,
but you do not own it. Please try Again" and another argument is passed to the
function "You entered [filename]" which is also echoed to the user,
the function then exits with an exit value of 10.

Code:
#!/bin/bash

echo -n "Enter a file name "

  read file
error_fn()
{
echo "ERROR: $1"
   echo "ERROR: $2"
     exit 10
}


until [ $# = $file ]
  do
     
echo you entered $file
error_fn "The File Was Not found, Please try Again"
           done
exit 10


           if [ $1 -eu ]
                  then echo The File is there and you own it

           else
            echo The file was found, but you do not own it

           fi
           exit 10


Top
 Profile  
 PostPosted: Sun Mar 28, 2010 7:50 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
The file search, are you talking about the current working directory, a search that includes the $PATH variable or all volumes?


Top
 Profile  
 PostPosted: Sun Mar 28, 2010 7:57 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Here's what I tried so far. It only does a search in the current directory that the file resides.

Code:
#! /bin/sh

echo -n "enter filername->"
read myfile

if [ ! -f "$myfile" ]
then
   echo "'$myfile' does not exist...exiting program"
   exit 1
else
   if [ -O "$myfile" ]
   then
      echo "'$myfile' exist and $(echo $(ls -l $myfile) | cut -d" " -f3) owns it"
      exit 0
   else
      echo "'$myfile' exist but you don't own it $(echo $(ls -l $myfile) | cut -d" " -f3) does"
      exit 0
   fi
fi


Last edited by G4143 on Sun Mar 28, 2010 8:45 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Mar 28, 2010 8:01 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
it's not asked to find the file entered, but just to check if it exists*:
Code:
#!/bin/bash

Error() {
   echo "Error: $1"
   echo "       You entered [ $2 ]"
   exit 10
}

read -p' Input the name of a file : ' filename

[ -f "$filename" ] && {
   [ -O "$filename" ] && {
      echo "The File is there and you own it"
   } || {
      Error "The File Was found, but you do not own it. Please try Again" "$filename"
   }
} || {
   Error "The File Was Not found, Please try Again" "$filename"
}
*it's not clear


Last edited by Watael on Sun Mar 28, 2010 2:55 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Mar 28, 2010 11:25 am   

Joined: Sat Mar 27, 2010 7:06 pm
Posts: 5
G4143 wrote:
The file search, are you talking about the current working directory, a search that includes the $PATH variable or all volumes?



I believe it's all volumes. It kind of sounds like that is what is meant but it's not specific.


Top
 Profile  
 PostPosted: Sun Mar 28, 2010 11:28 am   

Joined: Sat Mar 27, 2010 7:06 pm
Posts: 5
In any case thanks a lot G4143 and Watael for your time. I appreciate it, have a good one.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP