LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Again I need your explaination to understand a program. (https://www.linuxquestions.org/questions/programming-9/again-i-need-your-explaination-to-understand-a-program-427957/)

Gins 03-24-2006 05:24 AM

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.

druuna 03-24-2006 05:50 AM

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.

Gins 03-24-2006 07:31 AM

Thnak Drunna

I need more than what you have written to fathom out those things.

Gins 03-24-2006 03:05 PM

I need your help; so I urge one of our experts to look at this question.

druuna 03-24-2006 03:33 PM

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.

puffinman 03-24-2006 03:36 PM

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.

Gins 03-24-2006 03:48 PM

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.

Gins 03-24-2006 04:19 PM

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.


All times are GMT -5. The time now is 09:12 AM.