Shell Script (beginner)

  1. The problem statement, all variables and given/known data:
    Arguments:
    http://farm9.staticflickr.com/8070/8212131370\_8b6e8c10c5_c.jpg

I am given these three arguments. $1, $2, $3
The first argument is the path to a directory.
So, how would I go into the directory and compare files?

I also need to check if the second argument (a C program) can compile or not. I don't know how.

I have no idea.
Try to keep your answer simple. I'm new to this.
Thank you so much for your time.
(I am learning Bourne shell)

  1. Relevant notes:

This is the handout of my assignment:
http://farm9.staticflickr.com/8346/8211167069\_8fc3096663_z.jpg
http://farm9.staticflickr.com/8201/8208444087\_510263744b_c.jpg

  1. My attempt at a solution:
if (test $# -ne 3)
then
  echo Usage a2.sh <testdir> <C program> <time limit>
  exit 1 # exit failure
elif (test ! -d $1) # arg1 is not a directory
then
  echo $1 is not a directory.
  exit 1 # exit failure
elif (test ! -r $1) || (test ! -x $1) # arg1 is a directory, but not readable or excutable.
then
  echo $1 is not accessible.
  exit 1 # exit failure
elif (test ! -e $2) || (test ! -r $2) #arg2 doesn't exist, or is not readable.
then
  echo $2 is not accessible.
elif(test # check if arg2 doesn't compile. I don't know this case!!!!!!!!!!!!!!)

elif (test -ne [0-9][0-9]*)
then
  echo The third argument must be an integer.
fi
fi
fi
fi
fi
fi
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    York University, Toronto, Canada, Bil , cse 203 1

A single fi is enough to close the if elif. For testing if file can be compiled, it is clearly mentioned in assignment to use cc compiler without options. Use regex to test for integers:-

if [ $# -ne 3 ]
then
        echo "Usage a2.sh <testdir> <C program> <time limit>"
        exit 1    # exit failure
fi

if [ ! -d $1 ]   # arg1 is not a directory
then
        echo $1 is not a directory.
        exit 1    # exit failure
elif [ ! -r $1 ] || [ ! -x $1 ]  # arg1 is a directory, but not readable or excutable.
then
        echo $1 is not accessible.
        exit 1    # exit failure
elif [ ! -e $2 ] || [ ! -r $2 ]   #arg2 doesn't exist, or is not readable.
then
        echo $2 is not accessible.
# elif [ $2 use cc compiler with no options ]
elif ! [[ $3 =~ ^[0-9]+$ ]] # using regex to test integer
then
        echo The third argument must be an integer.
        exit 1
fi
  1. When you say "compare files", what exactly you want to do?
  2. To check if a C program can compile or not, pass the C file to the C-compiler' for e.g., cc test.c and check the exit status echo $? . Anything other than 0 would mean compilation was not a success.

Thank you for helping me.

compare files:
I mean I'm given the path to a directory. How do I go into the directory, and how can I tell it's a .in file and find a corresponding .out file?

I meant to write:

elif test $3 -ne [0-9][0-9]*

Can I still use this?

I don't know how to get the C program from the second argument path...:frowning:


---------- Post updated 11-24-12 at 04:23 PM ---------- Previous update was 11-23-12 at 05:06 PM ----------

Hey guys.

I'm not sure if this is the correct way.
So, I figured out:

getFile=` find $1 -type f -name '*.in' `

I think this means it finds all the .in files in the path of argument 1.

Now, I'm really stuck on how to find it's corresponding .out file.

Use a for loop instead, for every .in file check if .out exist or not:-

for in_file in *.in
do
        out_file=$( echo $in_file | sed 's/\.in/\.out/g' ) # replace .in with .out
        if [ ! -f ${out_file} ]  # check if corresponding .out file exists.
        then
                echo ${out_file} is missing.
        fi
done

Good research skills! Many people here are not at all able to independently come up with an at least valid try.

Alas, this line might even work, so when i tell you that there are a few problems with it - please believe me! I'll try to explain:

first problem: proper quoting

find $1

This will work as long as "$1" contains only "normal" paths, like "/some/dir". It will fail, though, when the path contains a space, because a space character is also used by the shell to delimit arguments. Consider the following two lines:

command arg1 arg2 arg3
command arg1 "arg2 arg3"

the first command gets 3 arguments, "arg1", "arg2" and "arg3". The second command only gets two: "arg1" and "arg2 arg3". The double quote in this case tells the shell to treat the blank like a normal character. Because it won't hurt script programmers routinely protect their variables:

find "$1"

next problem: where do you want to search?

"find" is a command which searches recursively. That is, it finds files not only in the directory it starts in, but in every subdirectory there might be too. Do you want that? Or do you want to only search in the directory you named? Depending on your answer you may have to either modify "find" (read the man page about the "-prune"-option) to only search this directory and not its subdirectories or use "ls" instead of find.

last problem: subshell invocation

it is possible to invoke a series of commands and fill their output to a variable. Historically this has been done with backticks, like you did. Still, backticks have some severe limitations and are deprecated. Instead there is the "$(...)". Alas, there are still a lot of examples on the net with backticks. Believe me, they are all flawed in this regard. Backticks do not offer a single advantage over the modern subshell invocation but a lot of disadavantages: cannot be nested, cannot be quoted, just to name two. Therefore:

getFile="$(find "$1" -type f -name '*.in')"

I hope this helps.

bakunin