LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2012, 12:13 AM   #1
learngeek
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Rep: Reputation: Disabled
How to generate shell script for 1 3 2 4 3 5 4 6..100


How do I code this in either
If or For or While?

I was told to watch out ofr that the numbers can go and up down. The syntax should look such as
1 , 3, 2, 4, 3 , 5, 4, 6 and so on in the output.

Thank you
 
Old 12-06-2012, 01:26 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Sounds a bit like homework; what have you done so far?
You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
Code:
man seq
 
Old 12-06-2012, 01:42 AM   #3
learngeek
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Codes for 1 3 2 4 3 5 4 6...100

n1=n+1
n2=n+2
while [ $n1 -le 100 ]
do
while [ $n2 -le 100 ]
do
echo -n "$n1"
n1++
n2++
done

output: I am trying to get up to 100 with numbers add to 3. Codes above did not work. What did I do wrong.
-------
1234
3456
etc.,
 
Old 12-06-2012, 05:08 PM   #4
User\ Name=`echo $USER`
LQ Newbie
 
Registered: Oct 2012
Location: Oklahoma
Distribution: Red Hat, Ubuntu, Mac OS X
Posts: 25

Rep: Reputation: 4
First of all, please use code tags when posting code.

Is this what you're looking for?

Code:
#!/bin/bash
n1=1
n2=3
temp=`mktemp tempfile.XXX`
while [ ${n2} -le 100 ]; do
   echo -n "${n1}, ${n2}, " >> ${temp} 
   let n1=${n1}+1
   let n2=${n2}+1
done
cuttemp=`cat ${temp}`
echo ${cuttemp%,*}
rm ${temp}
I'm sure there is a much cleaner way to do it rather than making a tempfile to get rid of the trailing comma, but I'm at work and had to rush this. But this will get you what I think you're looking for.

Last edited by User\ Name=`echo $USER`; 12-06-2012 at 05:27 PM.
 
Old 12-06-2012, 05:17 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Quote:
Originally Posted by User\ Name=`echo $USER` View Post
I'm sure there is a much cleaner way to do it rather than making a tempfile to get rid of the trailing comma, but I'm at work and had to rush this. But this will get you what I think you're looking for.
Do the first iteration of the loop outside the loop, then inside the loop make it a leading comma

Code:
#!/bin/bash
n1=1
n2=3
echo -n "$n1, $n2"
let n1=$n1+1
let n2=$n2+1

while [ $n2 -le 100 ]; do
   echo -n ", $n1, $n2"
   let n1=$n1+1
   let n2=$n2+1
done
echo ""

Last edited by suicidaleggroll; 12-06-2012 at 05:19 PM.
 
Old 12-06-2012, 05:25 PM   #6
User\ Name=`echo $USER`
LQ Newbie
 
Registered: Oct 2012
Location: Oklahoma
Distribution: Red Hat, Ubuntu, Mac OS X
Posts: 25

Rep: Reputation: 4
Quote:
Originally Posted by suicidaleggroll View Post
Do the first iteration of the loop outside the loop, then inside the loop make it a leading comma
Ah, very nice. Knew there was a cleaner way to do it.

Last edited by User\ Name=`echo $USER`; 12-06-2012 at 05:27 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
unix shell script to generate a bar graph pghosh06 Linux - Newbie 4 02-10-2011 04:01 AM
Generate Items for a case shell script Snake_Eyes_ Linux - General 8 12-12-2009 07:56 AM
shell script to auto process ten random files and generate logs novice82 Linux - Newbie 4 10-05-2009 08:08 AM
print 1 to 100 in a shell script sleeper0110 Programming 5 11-02-2008 05:55 AM
Generate a random number from a bourne shell script lothario Linux - Software 2 03-02-2007 12:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration