1st yr Exam revision thread - Scripts, C, Commands

Hello, I have an exam for 1st year Linux and Unix programming coming up in a week and I need some help going over the past exams, I want to make sure I'm getting the right answers in the past exams to ensure full marks.

The internet is a distraction so making this thread will hopefully help me focus! Also, it will be good revision for anyone else in the same boat, while hopfully helping me commit it to memory and understand it better, and the questions should be pretty easy :cool:

  1. Write a bash script for the minimum program that works as follows:
$ min  2 3
The minimum of 2 and 3 is 2

Heres my answer:

#!/bin/bash
 
if 
$1 < $2
echo "The minimum of $1 and $2 is $1"
else
echo "The minimum of $1 and $2 is $2"
fi

Pseudocode:
If
arg1 is less than arg2
output: "the minimum of arg1 and arg2 is arg1"
else
output: "the minimum of arg1 and arg2 is arg2"

Is my algorithm correct? The program compiles, but when I run it I get syntax error. What is the correct syntax please help.

---------- Post updated at 11:13 AM ---------- Previous update was at 10:43 AM ----------

Figured it out :stuck_out_tongue:

if
[[ "$1" < "$2" ]];
then
echo "The minimum of $1 and $2 is $1"
else
echo "The minimum of $1 and $2 is $2"
fi

It works and outputs correctly. Do you think this answer would get full marks in an exam?

  1. What is the search path for the shell?

It isn't obvious to me that this shouldn't be treated as homework and be required to have been posted in the Homework & Coursework Questions forum and adhere to the special rules associated with that forum. I'll leave that determination to the moderators, but I'll treat my responses as if it had been posted there.

I had never seen an if statement coded this way, but after looking at the grammar for the shell, it is a valid format. More common ways of coding if statements would be:

if [[ condition ]]
then
    true branch code
else
    false branch code
fi

or

if [[ condition ]]; then
    true branch code
else
    false branch code
fi

Although indentation is not required by the shell (and it may seem unimportant for a trivial case like this script), when the code in the true and false sections becomes more complex, indentation will make your code much easier to read. I strongly suggest that you get into the habit of always using a consistent indentation style and stick with it no matter how trivial the script is.

Other than using 2 and 4 as operands to your script, what values have you used to convince yourself that this script is working correctly? When you issue the commands:

min 8 10
    and
min 10 2

do you get what you expected?

Do you intend for your script to compare numeric values or alphanumeric string values?

1 Like

Thanks for the reply I appreciate it, I also wasn't sure if this should be in the homework section, I didn't want to clog it up as I want to keep this alive for a week. If the mods wants to move it they can, and some of the questions I am going to ask are pretty basic :cool:

My program is working fine, with any values I throw at it.

min 205 204

Will output:

The min of 205 and 204 is 204

The same when the numbers are reversed, I'm happy with that, also I will remember to indent more,ty.

Heres some more questions, are the ansers I've given correct? Will they get all the marks?

a) What is the purpose of the shell?
The shell is a program that interprets commands and acts as an intermediary between the user and the inner workings of the operating system.
b) What is the search path for the shell?
The search path is contained in the C shell �path� variable. Like all C shell variables, it can be referenced by placing a dollar sign in front of the name. The current search path can be printed with % echo $path.
The search path is also a component of the environment which is passed from process to process, independent of the shell.
c) Using the curses library, what commands would you use to

  • Clear the whole screen.
    clear();
  • Clear only the first two lines of the screen.
    Deleteln??
    I am unsure about what command to use in the last one, theres seems to be alot of commands in the curses library for removing lines, but I can't seem to find the specific one that deletes the first two lines. Any hints?

Now to the threads content itself:

I am not sure if this is in the scope of the question, but i miss some input validation: what will happen in the following cases:

min 0 -1
min -2 -5
min foo bar
min 1.5 1.6
min 0 1 2
min 1
min

Right now the behavior of your program is not only "unspecified" but also "not determined" and this is generally a bad idea, as the venerable Don Cragun probably knows best of us all.

It is good style to make programs as foolproof as possible, which means: always validate your assumptions. If you get input from users check if it is what you expect it to be. If you allocate temporary space (i.e. by using a tempfile) make sure the file system provides the free space you expect to need. If you want to write a file make sure the privileges the script is run under include write access there - etc., etc., ... True, this makes scripts a lot more complicated (i often end up writing 100 lines for basically executing a single command), but it also will make sure it exits with some defined state.

Always strife to build programs as finite-state machines. They do not always have to carry out their goal, but at least they should respond in a defined way to obstacles. Specifically for your script: it doesn't need to "correct" unintended inputs, but it should react with some defined exit state and/or some error message telling the reason why it can't continue.

Heres some more questions, are the ansers I've given correct? Will they get all the marks?

hmm.... First, I'd like to doubt that you are using or are being tought csh. Using csh is more than antiquated today, it is not POSIXly correct and generally csh Programming Considered Harmful as Tom Christiansen has pointed out already in 1996.

Second, if you indeed use csh: check your answer again, very carefully. UNIX is neither "Unix" nor "unix", you know. ;-))

I hope this helps.

bakunin