trying to create printf script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trying to create printf script
# 1  
Old 04-14-2010
trying to create printf script

I am trying to make a script that randomly generates 4 sets of numbers with decimal points. Then it outputs that to a variable and a file. This is what I have:

Code:
#!/bin/bash

printf -v RANDOM_2 "%s\n" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM"
printf "%s\n" "$RANDOM_2" > ./bp1/bin/random

For some reason when I do these commands separate in terminal it works fine but together in a script it wont work right. Anyone know how to make this work?

`

Last edited by Franklin52; 04-14-2010 at 01:12 PM.. Reason: Please use code tags!
# 2  
Old 04-14-2010
You're using printf like echo, which it's not. 'printf "%s" a b c d' will only print a because you're only telling it to print one string with '%s'. You might as well just use echo and print all of them.

You don't even need a variable here. Just echo it right into the file.
Code:
echo "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" > ./bp1/bin/random

This may not be the best way to generate random numbers. If RANDOM is between 0 and 65536, you'll never have a decimal point higher than .65. Let's try printf again, properly, with:

Code:
#!/bin/bash
( for ((N=0; N<4; N++))
do
        printf " %d.%03d" "$((RANDOM%1000))" "$((RANDOM%1000))"
done ) > ./bp1/bin/random

This should generate random numbers between 0 and 1000, with three decimal places.

Last edited by Corona688; 04-14-2010 at 01:38 PM..
# 3  
Old 04-14-2010
Quote:
Originally Posted by Corona688
You're using printf like echo, which it's not. 'printf "%s" a b c d' will only print a because you're only telling it to print one string with '%s'. You might as well just use echo and print all of them.

You don't even need a variable here. Just echo it right into the file.
Code:
echo "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" > ./bp1/bin/random

This may not be the best way to generate random numbers. If RANDOM is between 0 and 65536, you'll never have a decimal point higher than .65. Let's try printf again, properly, with:

Code:
#!/bin/bash
( for ((N=0; N<4; N++))
do
        printf " %d.%03d" "$((RANDOM%1000))" "$((RANDOM%1000))"
done ) > ./bp1/bin/random

This should generate random numbers between 0 and 1000, with three decimal places.
I know this isn't the proper way to do it but I am currently in a Linux/Unix programming class and they want this to be done using printf and $RANDOM (which I've never done it this way before). I need a new variable and a file both with the same outputted data.
# 4  
Old 04-14-2010
Quote:
Originally Posted by Corona688
'printf "%s" a b c d' will only print a because you're only telling it to print one string with '%s'.
Actually, that will print out 'abcd' (without a trailing newline). printf reuses the format string until all arguments are exhausted.

Example:
Code:
$ printf 'Line %d: %s\n' 1 one 2 two 3 three
Line 1: one
Line 2: two
Line 3: three

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Shell Programming and Scripting

Using printf (or other?) to create variable fixed width text

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column... (10 Replies)
Discussion started by: farrenthorpe
10 Replies

3. Shell Programming and Scripting

Need help in printf in shell script

My requirement is need to add spaces to the string with the dynamic value to printf... this is a part of shell script ..which i have , the length is not static ... length=15 value="1234567890" printf "%-"$length"s\n" "$value"; The result it is printing is ... i am not sure y it is... (19 Replies)
Discussion started by: greenworld123
19 Replies

4. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

5. Shell Programming and Scripting

Help Create dynamic ksh script from a script

I am currently running 2 scripts to gather data for a 3rd script and would like to combine the 2 scripts into one. Having issues with the final output format. Note cannot post URL so replaced the http stuff with (name) in the examples All scripts contain #!/bin/ksh OS = Red Hat Enterprise... (0 Replies)
Discussion started by: pcpinkerton
0 Replies

6. Shell Programming and Scripting

printf/echo in a second script

This may be little confusing. I have Script1, which pulls data from the system and creates another script(lets say script2). While I run script1 I need to add printf/echo statements for script2, so that when I run script2 I see those statement. eg: script1 765 printf " display frame-$1 timeoffset... (2 Replies)
Discussion started by: miltonrods
2 Replies

7. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

8. Shell Programming and Scripting

problem with printf in shell script

i have written small script as follows: name="hi hello" printf "%-20s" $name This gives me strange output. -20s format is applied on both word of string. i.e it displays both word hi and hello in space of 20 length. I want to display entire string "hi hello" in length of 20 space. plz... (2 Replies)
Discussion started by: admc123
2 Replies

9. UNIX for Dummies Questions & Answers

Create a script

How to I put my find command string into a script. It is currently to long to be entered manually at command line. for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c... (1 Reply)
Discussion started by: TimHortons
1 Replies

10. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question