0

I'd like to print with 3 digits such as 001, 002, ... not 1, 2, ... in the c shell.

but when I use the below example, I've got just number without 3 digits.

So I want to know how to print the 3 digits number as 001 not 1.

how can I print it with 3 digits in the c shell?

#!/bin/csh -f

 set j = 1
 while ( $j <= 400 )
   echo "Welcome $j times"
   @ j++
 end

results

0

1

...

100

...

400

update

One more question. if I want to assign it as a variance from " printf 'Welcome %03d times' $j" itself. Like this,

 set j = 1
 set k

 while ( $j <= 400 )
   printf 'Welcome %03d times' $j
   k = printf 'Welcome %03d times' $j

   @ j++
 end

If I want to assign 3digit number into the k variance like this k=003 not k=3. what am I do ?

update2

when I ran the below code, I've got always 000 not increase.

 set j = 1 
 while ( $j <= 500 ) 
   echo "Welcome $j times" 
 set k = `perl -e 'print (sprintf ("%03d", $j))'` 
   echo "set k= $k " 
Community
  • 1
  • 1
hihi88
  • 75
  • 2
  • 6

2 Answers2

0
#!/bin/csh -f

 set j = 1
 while ( $j <= 400 )
   printf 'Welcome %03d times' $j
   @ j++
 end

not sure about csh, but it should work

Alex Kapustin
  • 1,869
  • 12
  • 15
0

for you first question, the answer is as Alexandr wrote:

#!/bin/csh -f

set j = 1
while ( $j <= 400 )
printf 'Welcome %03d times' $j
@ j++
end

for your second question, you can do something like this:

> set test = `perl -e 'print (sprintf ("%03d", 3))'`
> echo $test
003

see http://www.brendangregg.com/Guess/guess.csh
in order to pass vars to the perl one liner, see the below line
How do I best pass arguments to a Perl one-liner?

Community
  • 1
  • 1
user2141046
  • 862
  • 2
  • 7
  • 21
  • When I ran the below code I've got alwasy 000. set j = 1 while ( $j <= 500 ) echo "Welcome $j times" set k = `perl -e 'print (sprintf ("%03d", $j))'` echo "set k= $k " – hihi88 Apr 06 '17 at 05:40
  • that is because the $j is not in the perl space, but in the csh space. using $ENV will also not help you because you use `set` instead of `setenv`. try your code with `setenv j 1` and `perl -e 'print (sprintf ("%03d", $ENV{j}))'` instead, it should be fine – user2141046 Apr 06 '17 at 09:26