creating printf statement using user arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting creating printf statement using user arguments
# 1  
Old 04-20-2012
creating printf statement using user arguments

I am writing a script in bash and want to perform the operation

I check number of arguments and make a print statement with the passes arguments

If I pass 3 arguments I will do

Code:
printf "$frmt" "$1" "$2" "$3"

If I have 4 arguments I do
Code:
printf "$frmt" "$1" "$2" "$3" "$4"

etc
# 2  
Old 04-20-2012
Hi,

you can try also with:
Code:
echo $*

# 3  
Old 04-20-2012
Ahhh Yes.

In my situation I will be using "$@"

How can I modify this to exclude the last arguments, ot the last two arguments?

---------- Post updated at 08:53 AM ---------- Previous update was at 08:32 AM ----------

I have now coded the following:

Code:
length=$(($#-2))
 array="${@:1:$length}"
 echo "All Arguments except last two"
 printf "$frmt" "$array"
 

echo "All Arguments"
 printf "$frmt" "$@"


The problem is this:


"$@" allows the each parameter to become a single word, so that "$@" is equivalent to "$1" "$2", because some arguments will have contain embedded blanks.



However calling $array does not allow me to do things as "$@" does, even though the array manages to drop the last two arguments.

---------- Post updated at 09:23 AM ---------- Previous update was at 08:53 AM ----------

I managed to make it work using the following



Code:
printf "$frmt" "${@:1:$(($#-2))}"

# 4  
Old 04-20-2012
Hi you could do this:
Code:
printf "$frmt" "${@:1:$#-2}"

Assigning to that array "array" would go like this
Code:
array=( "${@:1:$length}" )


Last edited by Scrutinizer; 04-20-2012 at 11:44 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-20-2012
Quote:
Originally Posted by Scrutinizer
Hi you could do this:
Code:
printf "$frmt" "${@:1:$#-2}"

Assigning to that array "array" would go like this
Code:
array=( "${@:1:$length}" )

That's very neat Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If statement arguments

I'm stuck on a particular problem and need some guidance. I have a file with a name and a phone number in it (teledir.txt). I need to do a $# in a separate script to take a positional parameter and check to see if it is in the file. To quote the question: If one argument is supplied, check... (6 Replies)
Discussion started by: Eric7giants
6 Replies

2. Shell Programming and Scripting

Arguments in usage statement

Hello, I have a question regarding the usage statement of a script. I have 2 parameters "--pto" and "--pto_list". To start the script I will need one of them. Both together are not possible. How this would be printed out within a usage statement? My suggestion would be: Usage:... (4 Replies)
Discussion started by: API
4 Replies

3. Shell Programming and Scripting

Printf statement for currency conversion

hi all, I had my script as a=qw b=rter c=fdfd curency=1000 printf"${curency} $a $b $c" > filename can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?(i.e for any number) (14 Replies)
Discussion started by: hemanthsaikumar
14 Replies

4. Shell Programming and Scripting

How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output.... below is my code : Code Credits :RudiC awk -F, 'NF==2 {next} {ITM=$1 AMT=$2+0 CNT=$3+0 TOTA+=$2 ... (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

5. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. UNIX for Dummies Questions & Answers

Creating textfile from arguments

Hi all I'm new to this forum and I really hope someone can help me out with a (for you guys) fairly easy question. I want to have a script that takes 1-14 arguments, all numbers 1-14, say myscript 1 3 4 5 7 what the script shall do is basically generare a textfile that contains 16... (5 Replies)
Discussion started by: Tobbev
5 Replies

7. Shell Programming and Scripting

Creating if statement for nfs

What would be correct syntax in a script to check to see if a /export/nfs/home mount point is mounted and if not stop/abort? OS is Solaris 10 using standard VI ksh shell. Here is the script. Please tell me where you see the problem. #!/bin/bash # # This script is run as root to sync AMI PROD... (0 Replies)
Discussion started by: soupbone38
0 Replies

8. Shell Programming and Scripting

case statement for different cmd arguments

Hello friends, I have a boubt passing different arguments at a time for any one option in below code. I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified. for i and r - install and re-install -... (4 Replies)
Discussion started by: pd2
4 Replies

9. Shell Programming and Scripting

Printf statement

The printf statement pay_amount=$(printf "%013.3f" "$4") working perfectly at one path(xxx/home/rsh) and showing error (printf: 216.000: invalid number) at another path(/opt/xxxx/xxxx). what will be the reason? thanks in advance (4 Replies)
Discussion started by: reeta_shri
4 Replies

10. Shell Programming and Scripting

if statement - how can I do 2 arguments?

I am new to shell, and I am trying to do a if statement like the following: if ; then basically it works fine if both arguments of the if are met, however the next elif is: elif ; then if the conditions of the elif are met, then it says "final1.sh: line 67: [: too many arguments" ... (6 Replies)
Discussion started by: Darklight
6 Replies
Login or Register to Ask a Question