How to check arguments in shell???


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to check arguments in shell???
# 8  
Old 01-25-2013
Quote:
Originally Posted by Corona688
Also, 'echo' does not work that way.
Code:
...
    printf "%s\n" "$makefile" "$target"
...

should be:
Code:
printf "%s\n%s" "$makefile" "$target"

# 9  
Old 01-25-2013
Quote:
Originally Posted by Corona688
Also, 'echo' does not work that way.
\n works on sh shell at least, but truly, I tested on bash and it does not output the new line
# 10  
Old 01-25-2013
Thank you. I found out whats wrong. thanks.
# 11  
Old 01-26-2013
Quote:
Originally Posted by jlliagre
should be:
Code:
printf "%s\n%s" "$makefile" "$target"

When commandline printf runs out of arguments in the command string, it repeats the command string. Try this:

Code:
printf "%s\n" a b c d e f g h i j k l m n o p q r s t u v w x y z

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 01-26-2013
Quote:
Originally Posted by tukuyomi
\n works on sh shell at least, but truly, I tested on bash and it does not output the new line
There was a major difference in the behavior of the echo utility (or shell built-in) between 4.2BSD (from the University of California, Berkeley) and UNIX System V (from AT&T Bell Laboratories). In the UNIX System V echo there were no options, but the common backslash character (\) escapes for control characters were recognized and, in addition, \c suppressed the <newline> that otherwise follows the final argument in the output and ignores all characters following the \c in its arguments. In the 4.2BSD echo backslashes in the arguments were printed as is and there was one option (-n) that suppressed the trailing <newline>.

When the POSIX.2 Shell and Utilities standard was being written, this disparity in behavior was one of the two key disagreements between committee members. It was resolved by inventing the printf utility and leaving the behavior of echo unspecified if its 1st argument is -n or any argument contains a backslash. It is also one of the extremely few utilities that is not allowed to treat -- as an end of options indicator and is not allowed to have any options other than -n. (The default GNU echo violates these requirements.)

Since The Open Group's X/Open Portability Guide was combined with the POSIX standards by The Austin Group (making the POSIX Standards and The Single UNIX Specifications a single document with three different covers: one for The Open Group's Single UNIX Specifications, one for the IEEE Std. 1003 POSIX Standards, and one for the ISO/IEC IS 9945 POSIX Standards), systems claiming to support the POSIX X/Open System Interfaces option (with the XSI margin marking) are required to provide an echo that behaves the way UNIX System V echo behaved. POSIX conforming implementations that do not claim to support the XSI option are allowed to provide an echo that behaves the way the 4.2BSD echo behaved or the way the UNIX System V echo behaved.

When trying to write portable applications, use printf rather than echo if the first argument to echo could start with a hyphen (-) or if any of its arguments could contain a backslash.

Hope this helps,
Don

PS. The other key disagreement between the Berkeley and AT&T camps when developing the original POSIX.2 Standard was over cpio versus tar which lead to the invention of the pax utility. It is not a coincidence that pax is the Latin word for the English word peace.

This concludes today's ancient history lesson. Smilie
These 4 Users Gave Thanks to Don Cragun For This Post:
# 13  
Old 01-27-2013
Quote:
Originally Posted by Corona688
When commandline printf runs out of arguments in the command string, it repeats the command string.
You are right, I forgot that weird still standard feature.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script file check throws [: too many arguments

While I am trying to check the filename/s in IF statement of a shell script (RedHat Linux 6) I am getting below error: File check: filename_time2=`date --date='yesterday' +%Y-%m-%d` cd /location/of/the/files/to/copy if then cp server.log-$filename_time2* ../archive/new... (5 Replies)
Discussion started by: Dip
5 Replies

2. Shell Programming and Scripting

Shell scripting with passing arguments

Hi All, I am using the script for creating local queue and passing the arguments while running the script as below n=0 while do e=`expr $n + 3` echo 'DEFINE QL('$e') MAXDEPTH('$6') MAXMSGL('$7') DEFPSIST('$8') '$9'' | /apps/mqm_opt/bin/runmqsc $2 n=`expr $n + 1` done Running the... (5 Replies)
Discussion started by: Anusha M
5 Replies

3. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

4. Homework & Coursework Questions

Shell Scripting with Arguments

1. The problem statement, all variables and given/known data: Problem 1: I need to create a shell script the takes three arguments and echo's out "There once was a ____that____who like to_____" The arguments go where the blanks are. Problemt 2: -Do an LS and store the... (4 Replies)
Discussion started by: dw15
4 Replies

5. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

6. Shell Programming and Scripting

Check if passed arguments is users

i want to check passed arguments one by one and if it is user print home director of that user (3 Replies)
Discussion started by: testman84
3 Replies

7. Programming

read arguments from shell

I have to write a C program using sys call (read, no fread) to read from shell all the parameters, without know how many are them. I tryed in some ways, but I have no success. Any Idea? Can I use read to read from stdin? (1 Reply)
Discussion started by: DNAx86
1 Replies

8. Shell Programming and Scripting

Bash Shell - # of arguments

Hi!, How can I check the number of arguments passed from the shell in a bash shell ? (1 Reply)
Discussion started by: DNAx86
1 Replies

9. Shell Programming and Scripting

Shell script with arguments

Hi All, I need some help/ideas in coming up with a shell script. Basically, the script should install 1 or 2 or 3 packages based on the input arguments. For example, if I type in pkgscript.sh a1 a2 a3, it should install all the 3 scripts and pkgscript.sh a1 should install only a1. If a... (3 Replies)
Discussion started by: sankar6254
3 Replies

10. UNIX for Advanced & Expert Users

Arguments to a shell program

Hi List, Is it possible to pass one argument to a shell program eg) there is a shell program abc which takes one arguments abc one Due to some reasons I pass abc one two Now one,two must be considered as "one" argument to the shell programs. Any suggestions,hints are welcome. ... (3 Replies)
Discussion started by: csvenkata
3 Replies
Login or Register to Ask a Question