LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2006, 05:24 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Again I need your explaination to understand a program.


[ The name of this program is temp30.]
#!/bin/sh

echo "File Name\tType"

for i in *;
do
echo "$i\t\c"
if [ -d $i ]; then
echo "directory"
elif [ -h $i ]; then
echo "symbolic link"
elif [ -f $i ]; then
echo "file"
else
echo "unknown"
fi
done

------------------------------------------------------------

[The name of this program is temp31.]

#!/bin/sh

printf "%32s %s\n" "File Name" "File Type"

for i in *;
do
printf "%32s " "$i"
if [ -d "$i" ]; then
echo "directory"
elif [ -h "$i" ]; then
echo "symbolic link"
elif [ -f "$i" ]; then
echo "file"
else
echo "unknown"
fi;
done


---------------------------------------------------------------

[The name of this program is temp32.]

#!/bin/sh

printf "%-32s %s\n" "File Name" "File Type"

for i in *;
do
printf "%-32s " "$i"
if [ -d "$i" ]; then
echo "directory"
elif [ -h "$i" ]; then
echo "symbolic link"
elif [ -f "$i" ]; then
echo "file"
else
echo "unknown"
fi;
done

----------------------------------------------------------------

All 3 programs works fine but I can't fathom out a few things.

1] This figure 32 is mysterious. From where does it come? It could be 22 or 42. Why should it be 32?

2] This % sign is strange too. Is it a part of the 'printf' ?
What is '' % '' this sign doing?

3] For the best of my knowledge, the word 'printf' is in the language C. Could you blend C and scripting?

4] Look at the figure '' -32s ''. What is the (-) minus sign doing?
What is ( s ) doing?

I am eagelry wating to read your comments.

Last edited by Gins; 03-24-2006 at 06:08 AM.
 
Old 03-24-2006, 05:50 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

printf is indeed used in C, but also in awk, shellscripting, perl and probably lots of other programming 'languages'. In general you can use the C syntax.

For details look at: man 3 printf

To give you a bit of a headstart:

32 => width of the field. In this case "File Name" is placed in the 32 char field, alligned to the rigth side.
-32 => same as above, except that "File Name" is alligned on the left.

32 can be anything you want/need.

Hope this clears things up a bit.
 
Old 03-24-2006, 07:31 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thnak Drunna

I need more than what you have written to fathom out those things.
 
Old 03-24-2006, 03:05 PM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I need your help; so I urge one of our experts to look at this question.
 
Old 03-24-2006, 03:33 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
The only thing I did not answer, but is in the manpage:

% => Yes, that's part of printf

2 new examples:

Code:
$ printf "|%-10s|\n" "hello"
|hello     |

$ printf "|%15s|\n" "hello"
|          hello|
The parts between % and s (including) define your format. %s being the most basic (s can be different, see manpage for details). Between the % ans s there can be modifiers. In this example 10 or -15. 10 creates a field that's 10 characters wide, text will be placed on the rigth side. -15 creates a 15 character wide field and places text on the left side (the -).

The \n adds a linefeed, without one your cursor stays on the same line.
 
Old 03-24-2006, 03:36 PM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Now that you've hit 500 posts, it's time to learn how to ask questions. Writing the same vague question in huge red bold font isn't going to get you more answers. I'd highly recommend reading this guide before posting more questions.

If you're having problems with shell scripts, try the sh man page. This is a nice way of saying RTFM, but unfortunately it's the only type of answer this question should get.
 
Old 03-24-2006, 03:48 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Puffinman

Those man pages are not written for the laymen. I am a beginner. Those man pages have not written in a manner to address the problems facing a beginner.

I believe you are an advanced person in these programming.

My knowledge of progrmming is not good like you all.

The bottom line is that man pages are difficult for me to understand.

Yes, it is incorrect to write big letters according to the guide. I did that as I havn't received a positive reply for a few hours. I must learn those posted problems before going further. I am studying using some online teaching materials.

I will try my level best to refrain from writing big letters in the future.

Last edited by Gins; 03-24-2006 at 03:53 PM.
 
Old 03-24-2006, 04:19 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Druuna

Now I understand more. Thanks for helping me. I have a smattering of knowledge when it comes to these scripting. Those man pages are not written in user friendly manner. You explain to me in a very simple manner.

Those man pages are for the advanced programmers. I will be posting more.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
802.11a-i explaination(s)? Curiosity is all.. MasterC Linux - Wireless Networking 1 07-06-2007 01:56 AM
java string question(explaination please) arunvk Programming 6 03-23-2006 05:02 PM
Squid process. need explaination! mrpc_cambodia Red Hat 5 10-11-2004 11:18 AM
Explaination Of DNS Terms Joe_Astor Linux - Networking 7 04-11-2004 02:56 AM
could any 1 help me understand how to program? nleber01 Programming 2 03-07-2004 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration