LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Leading blanks - problem (https://www.linuxquestions.org/questions/linux-newbie-8/leading-blanks-problem-807309/)

laki47 05-12-2010 01:55 AM

Leading blanks - problem
 
Can you please help me with the following:

# num_count=`ls /tmp/countdir |wc -l`
# echo "Number of files in /tmp/countdir is ($num_count)"
Number of files in /tmp/countdir is ( 5)

Why do I have 7 leading blanks before number 5?

How could I avoid that?

Thanks!

vikas027 05-12-2010 02:01 AM

Quote:

Originally Posted by laki47 (Post 3965406)
Can you please help me with the following:

# num_count=`ls /tmp/countdir |wc -l`
# echo "Number of files in /tmp/countdir is ($num_count)"
Number of files in /tmp/countdir is ( 5)

Why do I have 7 leading blanks before number 5?

How could I avoid that?

Thanks!

Use this code

Code:

num_count=`ls /tmp/countdir |wc -l`
num_count1=`echo $num_count | sed -e 's/^[ \t]*//'`
echo "Number of files in /tmp/countdir is ($num_count1)"


laki47 05-12-2010 02:04 AM

Quote:

Originally Posted by laki47 (Post 3965406)
Can you please help me with the following:

# num_count=`ls /tmp/countdir |wc -l`
# echo "Number of files in /tmp/countdir is ($num_count)"
Number of files in /tmp/countdir is ( 5)

Why do I have 7 leading blanks before number 5?

How could I avoid that?

Thanks!

Can I use (KORN SHELL):

typeset -i num_count
...

catkin 05-12-2010 03:08 AM

Quote:

Originally Posted by laki47 (Post 3965406)
Why do I have 7 leading blanks before number 5?

How could I avoid that?

Use my system! I could not reproduce the problem:
Code:

c@CW8:~$ num_count=`ls /tmp | wc -l`
c@CW8:~$ echo "Number of files in /tmp is ($num_count)"
Number of files in /tmp is (4)
c@CW8:~$ ls /tmp | wc -l
4

What is the output of ls /tmp/countdir |wc -l on your system?

Which shell are you using? If ksh, how have you declared num_count or is it implicitly declared as an ordinary variable?

EDIT: in ksh, to print the attributes of variable num_count try typeset -p | grep num_count (not tested; no ksh to test on).


All times are GMT -5. The time now is 03:04 AM.