$ 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.
$
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:
Code:
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
Code:
#!/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
Code:
$ highest.ksh
highest.ksh: line 19: filename: Filename Missing, include the filename after the "highest" script as an arguement.
$
Code:
$ highest.ksh -a albums
highest.ksh: -a: unknown option
Invalid option, use -h to display header: -
$
Code:
$ highest.ksh albums 4
22 Tom Petty
20 Led Zeppelin
18 Stone Temple Pilots
17 Rolling Stones
$
Code:
$ 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:
Quote:
Originally Posted by Decoy Octopus88
Code:
#!/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}
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:
Code:
filename=$1
This will break once "$var" contains a space char. Try it out:
Code:
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:
Code:
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:
Code:
#! /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
The awk below executes and produces the current output, which is correct, except I can not seem to include the header lines # and ## in the output as well. I tried adding !/^#/ thinking that it would skip the lines with # and output them but the entire file prints as is. Thank you :).
file
... (8 Replies)
Hi,
Does anyone know if there is anyway to run sdiff such that it shows the name of the files as it display the results of the differences? That is, I want it to show the filenames on each column and then display the differences
I can't find any option that allows this. Maybe someone has a... (2 Replies)
Hello All i have a KSH script which basically takes attribute name as input argument and searches whole Netezza appliance and prints information of where that column is used (Table/Views) etc.
Problem with this approach business users have to raise SUDO access request, Install Putty, run through... (1 Reply)
Hi,
So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file
eg
File contents
Name Place
Jim NY
Jill NJ
Cathy CA
Sam TX
Daniel FL
And what I want is... (2 Replies)
Hello There...
I have a sample input file ..
number:department:amount
125:Market:125.23
126:Hardware store:434.95
127:Video store:7.45
128:Book store:14.32
129:Gasolline:16.10
I will be doing some manipulations on all the records except the header, but the header should always be... (2 Replies)
Hi,
I'd like to redirect the STDOUT output from my script to a file and simultaneously display it at a console.
I've tried this command:
myscript.sh | tail -f
However, it doesn't end after the script finishes running
I've also tried this:
myscript.sh | tee ~/results.txt
But it writes... (3 Replies)
This is Input - starts with Storage Group Name and ends with Shareable and the loop continues all I need is Storage group name and Alu numbers in the below output format requested.
Storage Group Name: abcd
Storage Group UID: 00:00:000:00:0:0:0
HBA/SP Pairs:
HBA UID ... (6 Replies)
Hi,
In a script i am trying to run a SQL by connecting Sybase DB.
While executing the script I am getting the output with the header. means:
Employee_Name--------------------------- Anup Das
I need the Output:
Anup Das
Kindly advice. (2 Replies)
i have a script that access the database and then returns some row. but in the command prompt it it not showing the column header. how to get that ?
below the script:
------------------------------------------------------------------------
#!/bin/ksh
.... (4 Replies)