what the %s does percent sign-s mean?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers what the %s does percent sign-s mean?
# 1  
Old 12-08-2008
what the %s does percent sign-s mean?

context:

while reading tutorial on the read command, one of the first examples, demonstrating its use, follows as such:

$ x=abc ; printf "x is now '%s'. Enter new value: " $x ; read x

generating this output:

x is now 'abc'. Enter new value:

first, what does %s represent? I know in this context that it will obviously reflect the value of the just-defined variable x ('abc') but outside of this context??? And if the author wanted to display this variable, why did he use %s instead of $x???

Also for bonus stickers, explain the reason for spaces around semi-colons. I've seen it before, but I know semi-colons to inject end-of-line or carriage-return signal so I know them to work without the preceding or ensuing space?? Is my grasp accurate/correct? Maybe distinguishing why spacing vs not spacing and its effects would explain to me.

many thanks in advance, party people of the internet!
# 2  
Old 12-08-2008
Hi,
The printf statement takes arguments, and in this case the first argument is a string (between quotes and separate from the other arguments) containing formatting information as well. The first "placeholder" is in this case %s but it could be %d or some other description. The first %s says to take the first of the following arguments (the first one after the string) and print it as a string. Using %d would print it as a number. This is a very simple example. Most people would use $x in the string instead in this case. But the power of printf is that You can FORMAT the output, ie padding with spaces, padding numbers with zero's, use a fixed number of decimal places, etc. Space or not around semicolon is irrelevant as far as I know.

/Lakris
# 3  
Old 12-08-2008
Lakris,

Thanks for the explanation. I had done some reading and I understood what printf was intended to do, but I didn't realize that the $x after the quotes was intended to be the input for the %s, as you put it--A PLACEHOLDER!!

Now it all makes sense, thank you very much!! I cannot explain how thankful I am. Look forward to more posts from you.

--
Your friendly neighborhood programmar MARS!
# 4  
Old 12-08-2008
You're welcome!

/Lakris
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to auto restart java for 100 percent

I want the tomcat to restart when java goes 100% cpu utilize and remain on this , Get pid kill and start tomcat . top | grep java We can get pid and cpu utilize , But how can we do on run time. Please use code tags as required by forum rules! (1 Reply)
Discussion started by: kaushik02018
1 Replies

2. Shell Programming and Scripting

Calculate percent using values in 2 files

Trying to use file1 which is the actual counts in $2 associated with each $1 entry. The total of each $1 is in file2 with the total in $3. So when there is a match between $1 in file1 with $1 in file2, then the % is calculated using the $2 value of file1 and $3 value of file2. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

A Large Percent Problem

Hello everyone, I have two matrices at same sizes. I need to re-calculate the numbers in matrix A according to the percentages in martix B it is like matrix A is 10.00 20.00 30.00 40.00 60.00 70.00 80.00 90.00 20.00 30.00 80.00 50.00 martix B is 00.08 00.05 ... (2 Replies)
Discussion started by: miriammiriam
2 Replies

4. UNIX for Dummies Questions & Answers

Total cpu usage in percent(%)

How to determine the total percentage cpu usage of a server having 4 cpu cores(quad core)? (3 Replies)
Discussion started by: proactiveaditya
3 Replies

5. Shell Programming and Scripting

Tivoli Tape Zero percent

Hii all, If i insert zero percent tape in tivoli. Then tivoli will write data on it. But zero percent tape has some files inside, its not a scratch tape. Because if we recover data then tivoli can ask for zero percent tape. But when tivoli will write data on a zero percent tape ... (0 Replies)
Discussion started by: uusmankhalil
0 Replies

6. HP-UX

compare file percent sizes

I need to get a file size and compare it to a previous day file size. If it's larger or smaller by 50 percent I'll replace the new with the old. I know how to get the file sizes but do not know how to calculate if it's 50 percent difference. Thanks for your help. (2 Replies)
Discussion started by: jkuchar747
2 Replies

7. Shell Programming and Scripting

awk if percent % checking

hi everyone, # cat a a 10% b 25.5% c 91% d 50% # cat a | awk '$2 >= 90%; END {print $_}' awk: $2 > 90%; END {print $_} awk: ^ syntax error awk: each rule must have a pattern or an action part how to do only print when 2nd coln >= 90%. Thanks (6 Replies)
Discussion started by: jimmy_y
6 Replies

8. Shell Programming and Scripting

Find percent between sum of 2 columns awk help

Hi I'm new to this forum and I'm a beginner when it comes to shell and awk programming. But I have the following problem: I have 5 csv files (data1.csv, data2.csv, etc.) and need to calculate the average between the total sum of the 1st and 7 column. csv example:... (3 Replies)
Discussion started by: sapo51
3 Replies

9. Shell Programming and Scripting

Sign on/Sign off logging script

I'd like to make a script that I can execute every time I sign on to my linux box that keeps track of the time and allows to me to add a remark to a file. So basically once I log in, I run the script, and it outputs the date and time to a text file (log.txt). But that isn't my problem. I need... (1 Reply)
Discussion started by: Glider
1 Replies
Login or Register to Ask a Question