- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Incremental by 1 in the for loop
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 06:51 AM
тАО04-08-2009 06:51 AM
it is easy for 3 elements:
for i in 1 2 3; do echo $i; done
How I can make it for 100 elements without listing all the numbers? Thanks in advance.
Solved! Go to Solution.
- Tags:
- for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 06:55 AM
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 06:59 AM
тАО04-08-2009 06:59 AM
Re: Incremental by 1 in the for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 07:05 AM
тАО04-08-2009 07:05 AM
Re: Incremental by 1 in the for loop
> Thanks, any ideas with 'for' loop?
No, not with the shell.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 07:27 AM
тАО04-08-2009 07:27 AM
Re: Incremental by 1 in the for loop
for file in `ls -1 /home/mydir/data/IN*`
do
echo ${file}
cp ${file} /home/mydir/archive
done
if you are dealing with sequential numbering and do not want to write numbers one-by-one manually, you have no other choice than using the while construct, as 'for' directive does not work the same way in UNIX shells as it does on high level languages as BASIC.
Hope this helps
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 07:30 AM
тАО04-08-2009 07:30 AM
Re: Incremental by 1 in the for loop
No matter how much you love your hammer,
sometimes a screwdriver is a better tool for
the job.
Perhaps you could "echo ${N}" into a
variable, and use that result in a "for"
statement. (But that wouldn't make it the
right thing to do.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 08:12 AM
тАО04-08-2009 08:12 AM
Re: Incremental by 1 in the for loop
Steven makes a good point. If you want a better "hammer" you need to look elsewhere.
The 'sh-posix' manpages make it clear that a 'for' loop is the interation over a list unlike C's 'for' loop or even Perl's variations. In Perl, for example, I could write:
# perl -le 'for ($n=1;$n<=100;$n++) {print $n}'
...using the C-syntax for 'for'. However, I wouldn't do that when I could do:
# perl -le 'print for 1..100'
It's all about TIMTOWDI --- screwdrivers and hammers; but if all you have is a hammer, you have to use it as intended :-)
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 02:24 PM - edited тАО05-28-2012 01:27 AM
тАО04-08-2009 02:24 PM - edited тАО05-28-2012 01:27 AM
Re: Increment by 1 in the for loop
>JRF: N=$N+1
A typo, it should be: (( N = N + 1 )) or (( N += 1 ))
(Actually that initial assignment does work but you can't have spaces.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2009 10:56 PM
тАО04-08-2009 10:56 PM
Re: Incremental by 1 in the for loop
to generate a range of
numbers for further use within a command pipe or
shell script. This can be done with some simple
sh-code:
------------------------ CUT HERE ------------------
#! /bin/sh
# range - Generate of numbers.
lo=$1
hi=$2
while [ $lo -le $hi ]
do
echo -n $lo " "
lo=`expr $lo + 1`
done
------------------------ CUT HERE ------------------
It can now be used a way like:
for i in $(range 1 100); do
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 12:20 AM
тАО04-09-2009 12:20 AM
Re: Incremental by 1 in the for loop
You generate the numbers in a file with
a while loop (see above), but output >$TMPFILE
and then in your forloop you do
for i in $(cat $TIMPFILE)
do
typeset -i count
print $count
count=count+1
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 12:51 AM - edited тАО05-28-2012 01:25 AM
тАО04-09-2009 12:51 AM - edited тАО05-28-2012 01:25 AM
Re: Increment by 1 in the for loop
>Frank: To my mind it can be done.
Though in efficient, you could fix it as:
typeset -i count
for i in $(< $TMPFILE); do
print $count
(( count+=1 ))
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 04:21 AM
тАО04-09-2009 04:21 AM
Re: Incremental by 1 in the for loop
As an encompassing remark, efficiency may not seem very measureable in small scripts operating on minute amounts of data. Good practices there, however, lead to fast, scalable code that "thinks green".
Using 'typeset -i' creates an integer variable that makes arithmetic faster.
Using '(( N = N + 1 ))' to increment an integer by one lets the shell do the work without spawning a completely different process as when 'N=`expr $N + 1`' is used.
Writing 'X=$(< $FILE)' instead of 'X=$(cat $FILE)' is a subtle but faster optimization for capturing the contents of a file into a variable.
While, TMTOWTDI, there are fast and there are slow(er) paths to travel.
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 04:27 AM
тАО04-09-2009 04:27 AM
Re: Incremental by 1 in the for loop
if it would be a linux server you could just simply use the 'seq' command like this:
for i in $(seq 1000)
do
echo "This is the $i line."
done
Unix operates with beer.
- Tags:
- seq
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 04:50 AM
тАО04-09-2009 04:50 AM
Re: Incremental by 1 in the for loop
Where can I find the documentation for the "typeset" command.
I looked in man pages, typed "typeset -?" and "-h", looked in several books but still can't find it.
RayB
- Tags:
- typeset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 09:25 AM
тАО04-09-2009 09:25 AM
Re: Incremental by 1 in the for loop
> James, Where can I find the documentation for the "typeset" command.
The 'typeset' command is documented in shell manpages:
http://docs.hp.com/en/B3921-60631/sh-posix.1.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 07:40 PM
тАО04-09-2009 07:40 PM
Re: Incremental by 1 in the for loop
Another reason to use ksh's (( )) is that it supports 64 bit integers and expr(1) only 32.
- Tags:
- expr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2009 11:31 PM
тАО04-09-2009 11:31 PM
Re: Incremental by 1 in the for loop
revised to be more efficient:
------------------------ CUT HERE ------------------
#!/bin/sh
# range - Generate of numbers.
typeset -i lo=$1
typeset -1 hi=$2
while [ $lo -le $hi ]
do
echo -n $lo " "
(( lo=lo+1 ))
done
------------------------ CUT HERE ------------------
HTH,
Art