Need help on how to execute several programs

  1. The problem statement, all variables and given/known data:

Get each of these programs to run. Prove that you've done this(use script). Give a description of each program along with sample executions.

These are the exact programs we were given.

  1. Relevant commands, code, scripts, algorithms:
    PROG2:
#!/bin/sh
#beginline endline file
sed -n $1,$2p $3

PROG3:

#
set pwrs=(00000000)
@pwrs[1]=1
@next=2
while($next<9)
   @last=$next+1
   @pwrs[$next]=$pwrs[$last]*2
   @next=$next+1
end

@count=1
while($count<=8)
   echo $pwrs[$count]
   @count=$count+1
end

PROG4:

#!/bin/csh -v
#-fvx
# usage:prog4 [dir(s)]

if($#argv==0) then
        set dir="."
else
        set dir=$argv[1]
endif

while(1)
        if(! -d $dir) then
                echo $0 no directory $dir
        else
                echo $dir
                @ fcount=0
                @ dcount=0
                foreach file($dir/*)
                        if(-f $file) then
                                @ fcount++
                        else if(-d $file) then
                                @ dcount++
                        endif
                end
                echo $fcount files $dcount directories
        endif
        if($#argv<=1) then
                break
        else
                shift
                set dir=$argv[1]
        endif
end
  1. The attempts at a solution (include all code and scripts):

I am not sure how to execute the programs. I attempted to execute them using ./<filename> with the files being prog2, prog3, and prog4 respectively, but I don't know what arguments to pass if any. The programs are written in the vi editor, but I tried writing one program(which is not listed here) using the script command and it did not work. I also don't know how to actually print the results to turn them in, I only know how to use the snipping tool to screenshot.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)

SUNY Polytechnic Institute, Utica, NY, US, Cavallo, CS307

Any help is greatly appreciated, it is due 10/22 at 12pm EST

OK, have a look at the program code: to get a parameter passed to a program there are two things necessary. First, the parameters have to be passed (obviously) and second the program has to have some facility to receive parameters. Do you see any such facilities in these programs?

Second: what is necessary for some file to be recognised as executable? For instance, in DOS/Windows it is the extension ".com" or ".exe". What is it in UNIX?

vi is just an editor - a program to write text files. A written text file is just that - it doesn't matter using which tool it was written in first place.

Here is a tip for all your future dealings with UNIX: describe your errors exactly! "Doesn't work" can have several doezens of reasons, but an error message of "file not found" nails it down to exactly one. So, instead of telling us "didn't work" show us the command you entered (maybe it was wrongly phrased), the error message you got (maybe it tells us what went wrong) and the return code (use echo $? to get it). This way we can help you a lot better.

That depends on the setup of your system. Usually there is some print queue set up.

I hope this helps.

bakunin

These are the errors I get when attempting to run the code and I still do not understand what parameters need to be sent with ./<filename> to get the programs to execute. I'm sorry if these seem like really dumb, basic issues, I have very little experience with Unix.

prog2 : I understand that there are three parameters that need to be passed( $1 , $2 p, $3 ), but I don't know what they are. I assume one is a file name and another is a command, but I am not sure what the third is.

[gaben:CS307]$ ./prog2
sed: -e expression #1, char 1: unknown command: `,'

prog3 : There are syntax errors in the code, but I have the code identical to the handout.

[gaben:CS307]$ ./prog3
./prog3: line 2: syntax error near unexpected token `('
./prog3: line 2: `set pwrs=(00000000)'

prog4 : And prog4 simply prints out several blank lines and then an actual line of code, rather than executing.

[gaben:CS307]$ ./prog4


if ( $#argv==0 ) then
 if: Badly formed number.

No problem with you "have very little experience with Unix.", but I'd be very surprised if the teacher / tutor / professor hadn't given you any hints in their lesson on how to execute a program or script. It's the same way as on many other OS, btw., e.g. MSDOS or windows.

Next to the lessons, the *nix man pages are an invaluable source of information. Look into your shell's man page for "positional parameters". You seem to use two different shells, obviuously, sh and csh , with (partly, admitted) incompatible syntax. So, what works error free in one doesn't necessarily in the other. Is that intended?

So, with the error message please also post the shell emitting it.
And, please be meticulous when programming AND posting - one space too many may lead to synatx errors there and misinterpretations here. E.g. do you have if ( $#argv==0 ) then as in post#3, or if($#argv==0) then as in post#1?

You may want to set the --xtrace ( -x ) shell option when running your script; this might give you (and us) some more insight on what's going on and wrong.

I was not aware that I was using two different shells, although I will admit that I was in was in the wrong shell, which I did not realize until you mentioned this. My instructor told us to use tcsh which I switched to and I am still only able to get one program to execute, but I am getting slightly different errors now.

prog2 I attempted to pass with parameters

gaben:~/CS307> ./prog2 1,4d Zidane
sed: -e expression #1, char 5: extra characters after command

I know sed in an editing command and I assumed that $1,$2 were references to the lines to be edited, p is the command that will be editing those lines, and $3 is the name of the file being modified. I also attempted:

gaben:~/CS307> ./prog2 a,cd Zidane

Because I know $ means variable substitution, but this just returns a new line.

prog3 now gives me

@pwrs[1]=1: No match.
@next=2: Command not found.
next: Undefined variable.

which I assume is due to a lack of parameters.

prog4 is giving the exact same output as before.

My code is written exactly as it is in post #1, I even copied pasted it directly from the vi editor, so the spacing and new lines are all exactly the same from the post as in the actual code.

Is --xtrace ( -x ) something that I input on the command line, and since it is an argument, what is the command that gets passed with it? Is this something that I can find using man , and if so, in what section?

I tried to be more specific this time and tried to include everything I thought would be necessary. If there is anything else, let me know, but I am completely stuck.

I can't talk for [t]csh . In bourne compatible shells, set -x . Me personally, I search for keywords like "option" or "set" in the respective man pages.

If your "code is written exactly as it is in post #1", how come the error msg in post#3 is different?

That's a start.
Then you need to know how the sed command works: after the options (here: -n) the next word is the sed code. Usually one puts quotes around it, to make it better visible (and allow spaces in it). The next word(s) is(are) input files.
Compare this information with "man sed"; it starts with a short invocation description.
You need to read more in order to understand the given sed command, and what parameters make sense.

These are two arguments! Parameter $1 will become 1,4d

The eqivalent of set -x in sh-derived shells is set echo in csh-derived shells. All shells take a -x option:

sh -x shscript
csh -x cshscript

I am not sure why it adds spaces. The only coding I have changed is in prog3, I changed the spacing in some areas, and the results are now

gaben:~/CS307> more prog3
more prog3
#
set pwrs=(00000000)
@ pwrs[1]=1
@ next=2
while($next<9)
   @ last=$next+1
   @ pwrs[$next]=$pwrs[$last]*2
   @ next=$next+1
end


@ count=1
while($count<=8)
   echo $pwrs[$count]
   @ count=$count+1
end
gaben:~/CS307> ./prog3
./prog3
@: Badly formed number.
gaben:~/CS307>

prog3 is missing dozens of spaces that csh wants (again I was surprised when csh needs spaces and when not), and it should be last=next-1 to make sense.
Is it part of the exercise to fix the missing spaces or fix the code?
Are the missing spaces because of your typos, or is it your text editor?

The only requirements for the assignment are to run the code, print output, and explain what the code does. The missing spaces which I noticed were due to trying to copy the code off a handout where the spacing is sometimes difficult to see. As for the line which you corrected, that was my own error, but even after correcting it, I still receive the same error: @: Badly formed number. .

Because of the missing spaces. Debug run of prog4:

csh -vx prog4


if ( $#argv==0 ) then
if ( 0==0 ) then
if: Badly formed number

One line is due to the -v option (verbose), and the following line is after parsing (substitutions) due to the -x option (debug).
You see it recognizes 0==0 as one token, but it needs 3 token i.e. two spaces:

if ( $#argv == 0 ) then

Analog, prog3:

set pwrs=(0 0 0 0 0 0 0 0)
@ pwrs[1] = 1
@ next = 2
while ($next < 9)
   @ last = $next - 1
...

Especially the @ lines are very "spacy".

1 Like