Hey everyone, I'm a student at a local community college and working on my last project for the summer. I've been working on this script for the last two days and cannot figure out what I'm doing wrong here. Hoping that some of you might be able to figure out where I'm coding this incorrectly. I've read the man and info coreutils pages for test and as far as I can tell I'm following the correct syntax for the command I want. Please let me know if you see anything I need to correct in order to get the proper output. Here's the assignment:

Concepts:BASH shell (bash) programming
variables
arguments
control structures
Functions
test (for file types)
User InteractionDescription:Create an executable BASH shell (bash) shell script that will:
Require exactly 2 command line arguments and calls the error_function passing the error message that says You must enter Exactly 2 command line arguments, please try again: The error function takes the argument passed to it, prints it and then exits with an exit code of 1.
Tell the user the values of the two command line arguments that they entered and ask for a Y or y or N or n to confirm that they did not make a typo. If they did do a typo, and the user enters an N or n meaning that they did make a mistake, then call the error function with a different message saying Program exiting because you said you had a typo, Please try again:.
Tell the user if the command line arguments they entered were indeed files in the directory that the user specified. If one or both files did not exist, call the error function and give the message: Sorry, one or both files not located, please try again: .
Then test to see if each file that exists is a regular type of file or if one or the other is a directory type of file. If one of the files is a directory type of file, call the error function and display this message: Sorry, one or both of the files that you entered was a directory, please try again:
Tell the user if they own the files that they entered. (Hint: run man test to figure out how to test for these things.)
Tell the user which file is newer of the 2 file names they entered.

Grading Structure:
50 points total Classroom sections will show me your program in class for a grade. Again make sure that the filename is: ~[username]/bin/[username]proj3.sh
(15 pts) script named ~[yourusername]/bin/[yourusername]proj3.sh It must be executable by me but not be writable by me, it must also be on time..
(10 pts) uses bash and correct syntax, and has no undesirable side effects.
(25 pts) correct report information

Tools:
test, if-then-elif

Now here is the script I've written so far. Note: I'm not asking for a complete solution to the assignment, only for a solution to the error I'm receiving as per the check for file is or is not a directory. I will finish coding the rest of the assignment tonight and tomorrow. Here's the script:

1 #!/bin/bash
2
3 clear
4
5 error_fn()
6 {
7   echo "ERROR: $1"
8   exit 1
9 }
10
11 if [ $# -ne 2 ]
12 then
13   error_fn "You must enter Exactly 2 command line arguments, please try again:"
14 fi
15
16 echo the first argument was $1
17 echo the second argument was $2
18
19 if test $# -eq 2
20
21  echo -n "Are these the command line arguements you entered? Y or N":
22  read user_input
23
24  test $user_input = N
25  test $user_input = n
26  then
27  echo "Program exiting because you said you had a typo, Please try again:        "
28  exit 0
29
30  fi
31
32 if test -f $1
33 then
34  echo "Yes $1 is a file in the current directory"
35 else
36  echo "Sorry, one or both files not located, please try again"
37 exit 0
38
39 fi
40
41 if test -f $2
42 then
43  echo "Yes $2 is a file in the current directory"
44 else
45  echo "Sorry, one or both files not located, please try again"
46  exit 0
47
48 fi
49 if
50 [ -d $1 ]
51 then
52  echo "Yes $1 is a file in the current directory"
53 else
54  echo "Sorry, one or both of the files you entered was a directory, please try again"
55  exit 0
56
57 fi
58
59 if [ -d $2 ]
60 then
61  echo "Yes $2 is a file in the current directory"
62 else
63  echo "Sorry, one or both of the files you entered was a directory, please try again"
64  exit 0
65
66 fi
67
68 if test -O $1
69  then
70  echo "Yes you are the owner of file $1"
71 else
72  echo "No, you are not the owner of file $1"
73 exit 0
74 fi
75
76 if test -O $2
77 then
78  echo "Yes, you are the owner of file $2"
79 else
80  echo "No, you are not the owner of file $2"
81 exit 0
82 fi

When I run the script I get the following:

[shadowchaser@server1 ~]$ bash test3.sh dogs test
the first argument was dogs
the second argument was test
Are these the command line arguements you entered? Y or N:y
Yes dogs is a file in the current directory
Yes test is a file in the current directory
Sorry, one or both of the files you entered was a directory, please try again
[shadowchaser@server1 ~]$

Yes, dogs and test are files in the current directory. No, neither of them are directories themselves. Here is the directory listing:

[shadowchaser@server1 ~]$ ls -l
total 32
drwxr-xr-x  4 shadowchaser shadowchaser 4096 Jul 17 20:05 Desktop
-rw-rw-r--  1 shadowchaser shadowchaser   13 Jul  4 09:13 dogs
-rw-rw-r--  1 shadowchaser shadowchaser   83 Jul 19 19:22 ifthen.sh
-rwxr-xr-x  1 shadowchaser shadowchaser 1485 Jul 29 21:49 jcorzineproj3.sh
-rw-rw-r--  1 shadowchaser shadowchaser    0 Jul 17 21:32 junk
-rw-rw-r--  1 shadowchaser shadowchaser  234 Jul 19 19:18 test
-rw-rw-r--  1 shadowchaser shadowchaser   59 Jul 30 08:30 test2.sh
-rw-rw-r--  1 shadowchaser shadowchaser 1378 Jul 30 15:53 test3.sh
drwxrwxr-x  2 shadowchaser shadowchaser 4096 Jul 22 17:56 testing
[shadowchaser@server1 ~]$

Please let me know if you see anything wrong. I am going nuts trying to figure this out and it is a major project for my grade. Thanks in advance!

Jack

Recommended Answers

All 2 Replies

Figures, after I post this request for help I found the problem in an exit statement after the test -d routine. My fault but thanks anyway! If I have further problems I'll be certain to come back here for help.

Jack

[ -d $1 ] on line 59
test is postive when $1 is a directory.

Your logic is reversed - you display "is in the current directory"
when this test is true, when the file is a directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.