Display header in script output

Hi -- Working on my own through the book "Learning the KornShell and came to task 4-1, which there is:
a script "highest" and it will sort an "album" file.
highest filename

The author mentions adding a header line to the scripts output if the user types in the -h option. It says "assume the option ends up in the variable "header" i.e $header is -h"

If user runs the script with -h, it will produce a header on the output that reads "Album Artist" and a new line" if the -h option is not used there is no header or new line.

From the book:
The expression
${header:+"Albums Artist\n")
yields null if the variable header is null or "Albums Artist\n" is not null.
we can put the line:
print -n ${header:+"Albums Artist\n"}

My problem is i do not know how to make $header relate to the -h option. It says "assume....." but i want to follow along and run the script. Tried the man pages, but no luck. If you have any ideas please respond. Thanks.

Look into getopt .

What you want to do is simple:

# pseudocode:
if [ command line options passed include "-h" ] ; then
     header="-h"
else
     header=""
fi

How you can achieve that: see CarloMs post above. getopts (probably preferable to "getopt" because being a shell builtin) is the canonical method.

I hope this helps.

bakunin

Ok thanks to the both of you. I was able find a "go_test" script to use as a work around to print a header when using the -h option using the getopts. The album sort script works well. However when i try to combine them into one script, it messes up the sort -n and header options. If you have any suggestions please let me know.

vi albums

17 Rolling Stones
20 Led Zeppelin
5 ACDC
16 Aerosmith
12 Black Crowes
4 The Cult
22 Tom Petty
10 Nirvana
18 Stone Temple Pilots
8 Van Halen
1 Talking Heads

go_test -h
vi go_test

while getopts ":h" opt; do
  case $opt in
    h)
      print -n "ALBUMS ARTIST\n" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

highest albums [howmany]
howmany default is 10
vi highest

filename=$1
filename=${filename:?"Filename Missing, include the filename after the \"highest\" script as an arguement."}
howmany=$2
sort -nr "$filename" | head -${howmany:=10}

What does your complete script look like? Also, what are your expected arguments now? It looks like you have a 'highest' parameter rather than just outputting a header.

The script name is highest
The first required parameter is "filename" and the filename here is "albums". If no "filename" is provided a message is displayed.
The second parameter is an optional number [how many] meaning how many albums/artist do you want to display (default of 10).
There is also a requirement that if the user uses the -h option, it would create a header "ALBUMS ARTIST" + newline, and if the -h is not used as an option, then no header or new line would exist, but the rest of the script will run. If the user enters a different option such as -a, an invalid option message is displayed. My file "albums" is above, but here is the entire script and also the output with the messages. Note when i run the script without the -h it runs great, minus the header. When run it with -h, i have problems. Thanks again.

script (highest)

while getopts ":h" opt; do
  case $opt in
    h)
      print -n "ALBUMS ARTIST\n" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

filename=$1
filename=${filename:?"Filename Missing, include the filename after the \"highest\" script as an arguement."}
howmany=$2
sort -nr "$filename" | head -${howmany:=10}

output

$ highest albums 9
22 Tom Petty
20 Led Zeppelin
18 Stone Temple Pilots
17 Rolling Stones
16 Aerosmith
12 Black Crowes
10 Nirvana
8 Van Halen
5 ACDC
$ 
$ 
$ 
$ 
$ highest -h albums 9
ALBUMS ARTIST
sort: options '-hn' are incompatible
head: invalid option -- 'a'
Try 'head --help' for more information.
$ 

First: you output the header line to <stderr> and the rest to <stdout>. This is possible, but <stderr>, as the name suggests, is for error messages and even if it does what you expect it to do you should reconsider.

Second: you haven't specified a certain shell to use (which you should indeed do), but your usage of "print" suggests you using a Kornshell. You do not need I/O-redirection in this case, Kornshells "print"-statement knows the parameter "-u<descriptor>", so that:

print -u2 "text to print"

will do the same as

print "text to print" >&2 

Further, you do not need the "\n" at the end, print automatically adds a newline if you skip the "-n" option, which suppresses exactly that. Lastly, you should routinely use the "-" as the last option to make sure the string you want to print doesn't end up as being interpreted as parameter:

print -u2 - "text to print"

will work even if "text to print" would be replaced by text containing legal options to print, which would otherwise break your code. Here is an example:

text="-u2"
print "$text"         # will print nothing, because "$text" expands to an option
print - "$text"       # will print "-u2" as expected

Finally, I'd like to quote your first post:

Now that you know how to relate the "header" variable with the "-h" option you should not just copy what you found on the net, but put that mechanism to use for your own goals. Define a variable "header" and either put "-h" in there or not, depending on the "-h"-option being invoked or not. This should not be too hard, given that you now know how to use "getopts". Then use what is mentioned in your book to get what you want.

Here is a tip: you can look at the code while it executes and see what the shell really sees by using the "set -xv" command. Swithc that on with "set -xv" and off with "set +xv", like this:

#! /bin/ksh

# this part will be executed normally:

typeset variable="value"
typeset othervar="othervalue"

#watch execution from here:
set -xv

print - "variable: $variable     value of othervar: $othervar"

#switch it off again:
set +xv

print - "variable: $variable     value of othervar: $othervar"

exit 0

Save this to "myscript.ksh", slap on the excution bit and try it with:

./myscript.ksh 2>&1 | more

Tracing messages arrive at <stderr> and are first redirected to <stdout> ("2>&1"), then output is paginated by "more", which is not necessary in case of the short sample. For longer script parts, though, it is.

As "set +/- xv" only switches on and off tracing you can add it to interesting parts of scripts, watch what they do and move these commands around to inspect other parts. I do that regularly in my own scripts as part of my debugging routine.

I hope this helps.
bakunin

The problem is that if you call it with -h then your filename & count are actually in parameters 2 and 3, not 1 and 2.

What you need to do is shift the parameters down once getopts has processed everything it can. Add this line just after your getopts loop:

shift $((OPTIND-1))

OPTIND holds the index of the next parameter getopts would process. Since getopts has finished, this is the first unprocessed parameter - 1 if you didn't have -h (so we'll shift 0 places, i.e. do nothing), or 2 if you did (so we'll shift 1 place, and $2 will become $1, $3 becomes $2, etc).

Bakunin -- Thank you for your detailed explanation. I added the ksh. I took note and saved off your set -xv, and also got a good grasp of how the dash after your print command to ensure your string is not interpreted as a parameter. I took your advice to go back to the original goal of making my header a variable.

Carlos -- Thanks for suggesting the getopts and then the "shift optind", that worked great to shift my parameters back by one.

I really appreciate both of you.

final script

#!/bin/ksh

header="Albums Artist"
while getopts "h" opt; do
        case $opt in
                h)
print - ${header:+"Albums Artist\n"}
;;
\?)
      print - "Invalid option, use -h to display header: -$OPTARG" >&2
exit 1
;;
esac
done

shift $(($OPTIND - 1))

filename=$1
filename=${filename:?"Filename Missing, include the filename after the \"highest\" script as an arguement."}
howmany=$2
sort -nr "$filename" | head -${howmany:=10}

output

$ highest.ksh
highest.ksh: line 19: filename: Filename Missing, include the filename after the "highest" script as an arguement.
$ 
$ highest.ksh -a albums
highest.ksh: -a: unknown option
Invalid option, use -h to display header: -
$ 
$ highest.ksh albums 4 
22 Tom Petty
20 Led Zeppelin
18 Stone Temple Pilots
17 Rolling Stones
$ 
$ highest.ksh -h albums 11
Albums Artist

22 Tom Petty
20 Led Zeppelin
18 Stone Temple Pilots
17 Rolling Stones
16 Aerosmith
12 Black Crowes
10 Nirvana
8 Van Halen
5 ACDC
4 The Cult
1 Talking Heads
$ 

First off: congrats! You got a working script. The following is written in the hope that it might interest you equally:

As it is, programming (yes, writing scripts is programming) is mostly about getting your thinking in order. Working code usually "falls out" of this process by itself. Most of the time while writing code is actually not spent writing, but planning: "what should the program do if ...".

Now, let us have a look at your program:

Yes, it did, what it should, so your basic requirement is fulfilled. How could it be made better?

First, you should habitually quote your strings - always:

filename=$1

This will break once "$var" contains a space char. Try it out:

var="oneword"
filename=$var               # this willl work
var="two words"
filename=$var               # but this will give an error message
filename="$var"             # yet, this will work regardless of what $var contains

The next thing is: you have covered the case that a user forgets to provide a filename. Now, what would your program do if the user provides a filename but it is not found (or is not readable, or not a regular file)? Wise programmers go to painful lengths to validate user input, because they know: if there is anything that can be trusted than it is the users ability to break things.

Read the man page of the test command to understand the following:

filename="$1"
# filename=${filename:?"Filename Missing, include the filename after the \"highest\" script as an arguement."}
if [ "$filename" = "" ] ; then
     print -u2 "Error: Filename Missing ..."
     exit 1
fi
if [ ! -f "$filename" ] ; then
     print -u2 "Error: File $filename does not exist or is not a regular file."
     exit 1
fi
if [ ! -r "$filename" ] ; then
     print -u2 "Error: File $filename is not readable."
     exit 1
fi

Finally, i suggest you create the variables you work with first. The shell does not require this (unlike like other programming languages, which do so), but it is still preferable to do so. You can make the variables have the appropriate type (integer or string) and you get an opportunity to document your code along the way:

#! /bin/ksh

typeset    filename=""               # filename to read from
typeset -i howmany=10                # number of lines to display

[...]

filename="$1"
# filename=${filename:?"Filename Missing, include the filename after the \"highest\" script as an arguement."}
if [ "$filename" = "" ] ; then
     print -u2 "Error: Filename Missing"
     exit 1
fi
if [ ! -f "$filename" ] ; then
     print -u2 "Error: File $filename does not exist or is not a regular file."
     exit 1
fi
if [ ! -r "$filename" ] ; then
     print -u2 "Error: File $filename is not readable."
     exit 1
fi

I hope this helps.

bakunin

1 Like