AIX function example with "shift" command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AIX function example with "shift" command
# 1  
Old 05-04-2014
AIX function example with "shift" command

Hello,

I am reading one of the AIX manuals about shell scripting and (AIX 5) and I found this example when introducing to functions:

Code:
function usage
{
  prog="$1"; shift
  print -u2 "$prog: usage: $prog $@"
  exit 1
}

This example is meant to be easy but I don't understand what it is for or what is trying to demonstrate.

The shift command is meant to pass to next argument.. but I don't see what is the purpose here (?)

Then it prints into the STDERR because it prints into file descriptor 2.. but what for?

So.. I am confused. This is probably a very simple thing but I am stuck since yesterday and I still don't figure it out.

Any hint will be appreciated.

Thanks.
# 2  
Old 05-04-2014
By using shift, $1 becomes $2, $2 becomes $3, etc.. in the following line:
Code:
  print -u2 "$prog: usage: $prog $@"

there is "$@" which stands for all parameters which originally would have been all parameters to the function, but because of the shift it has become parameters $2, $3, etc of the original parameters to the function.

Programmatically I do not really see what the purpose is in this case, since they could also have used:

Code:
function usage
{
  print -u2 "$1: usage: $@"
  exit 1
}

But I would have to say the original is easier to read and understand, so that may be the reason..

The other thing - it prints to stderr - is logical since the usage is often printed when a user uses a wrong syntax.

Last edited by Scrutinizer; 05-04-2014 at 10:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-04-2014
I've just realised I missed these lines.. Smilie

Code:
# Invoke function usage with arguments: script
# followed by arglist. Note the exit status!

But well.. now I understand better..

So any time I invoke the script, I'll have an output like this:

Code:
myscript: usage: myscript start stop refresh

And I can see this usage message which goes to stderr.

Well, I think the purpose is making something like this:

Code:
function usage
{
  prog="$1"; shift
  print -u2 "$prog: usage: $prog $@"
  exit 1
}

if [[ "$@" == "" ]];then
  usage script start stop refresh
fi

So only when I invoke the script without arguments I will be prompted with the usage message, which is also an error message.
# 4  
Old 05-04-2014
Quote:
Originally Posted by Scrutinizer
Programmatically I do not really see what the purpose is in this case, since they could also have used:

Code:
function usage
{
  print -u2 "$1: usage: $@"
  exit 1
}

As it is: no, they couldn't have done that. The original function is meant as a usage message and suppose it would have been called this way:

Code:
usage myscript -x -y -z

then the output in the original case would be:

Code:
myscript: usage: -x -y -z

whereas your function would have produced:

Code:
myscript: usage: myscript -x -y -z

You need to use the "shift" somehow to avoid the duplication of the first parameter (once in "$1", another as first part of "$@").

I hope this helps.

bakunin
# 5  
Old 05-05-2014
Hi Bakunin. No, actually . With your input both functions will produce:
Code:
myscript: usage: myscript -x -y -z


Code:
function usage
{
  prog="$1"; shift
  print -u2 "$prog: usage: $prog $@"
  exit 1
}

usage myscript -x -y -z

Output:
Code:
myscript: usage: myscript -x -y -z

# 6  
Old 05-05-2014
Quote:
Originally Posted by Scrutinizer
Hi Bakunin. No, actually . With your input both functions will produce:
Code:
myscript: usage: myscript -x -y -z

Oops. I have to admit i overlooked the second "$prog" completely. Struck of blindness, i presume. Sorry.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

How to express "shift+enter"?

Hi expert "enter" can be expressed by "\n" in python, how about "shift+enter"? Thanks (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. AIX

How to enable "TCP MD5 Signatures" and "https" on AIX?

I have searched many times but nothing found. Somebody help please :(:(:( (1 Reply)
Discussion started by: bobochacha29
1 Replies

4. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

5. Shell Programming and Scripting

AIX's "bc" Command: Doing a Conditional Expression?

Hello All, I'm trying to issue a conditional expression using "bc" on floating point numbers. I had this working on my linux box, but as soon as I transferred the file over to an AIX Server, it would not work anymore... This is the "WORKING" command on linux (below), which I can't seem to... (8 Replies)
Discussion started by: mrm5102
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

8. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

9. Shell Programming and Scripting

Sending "Shift-F1" to a process

Hi, I need to find out the ASCII code for ALT-F1. The reason is that I'm using Expect to interact with an application and although I have the codes for keys such as 'Escape' (/033); F1 (\033\117\120) and ALT-F1 (\033\133\064\071\176) I have no idea where to find the code for Shift-F1. The... (2 Replies)
Discussion started by: pbritta
2 Replies

10. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question