4 Replies - 1589 Views - Last Post: 23 February 2013 - 12:51 PM Rate Topic: -----

#1 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Loop uptime questions (Bash)

Posted 28 January 2013 - 12:52 PM

As of now I have two questions but I will have a few more questions once these two have been resolved.
1. First how can I make the script output like this
1 Machine 1
2 Machine 2
3 Machine 3

instead of this? Where the rows of information get chopped up into separate rows which I don't want.
$ bash loop_uptime.sh 

1 Machine
2 1
3 Machine



2. Second question how can I control the spacing better between Number count and Machine name?
Without doing like this? Is there a cleaner and smarter way to do the same spacing control?
echo $n \ \ \ \ \ \ \ $host




.................................................

loop_uptime.sh
#!/bin/bash

n=0

LIST=$(cat ./machines.txt)

for host in $LIST; do
    let n++
    echo $n    $host

    if [ "$n" = "3" ]; then
        break
    fi
done




machines.txt
Machine 1
Machine 2
Machine 3
Machine 4
Machine 5
Machine 6
Machine 7
Machine 8
Machine 9
Machine 10
Machine 11
Machine 12
Machine 13
Machine 14
Machine 15



Is This A Good Question/Topic? 0
  • +

Replies To: Loop uptime questions (Bash)

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Loop uptime questions (Bash)

Posted 28 January 2013 - 06:20 PM

View PostOrochimaru, on 28 January 2013 - 08:52 PM, said:

1. First how can I make the script output like this
1 Machine 1
2 Machine 2
3 Machine 3

instead of this? Where the rows of information get chopped up into separate rows which I don't want.
$ bash loop_uptime.sh 

1 Machine
2 1
3 Machine


By default strings are separated by any white space, not just newlines. That's why you get the output you do. To change this, you can set IFS to $'\n'. Alternatively you can use read inside the loop and pipe the contents of the file into the loop instead of reading it into a variable beforehand.

Quote

2. Second question how can I control the spacing better between Number count and Machine name?
Without doing like this? Is there a cleaner and smarter way to do the same spacing control?
echo $n \ \ \ \ \ \ \ $host



Use quotes:

echo "$n       $host"



That being said, you can do the whole thing a lot easier by simply using head (to get the first three lines of a file) and nl (to number them).

This post has been edited by sepp2k: 28 January 2013 - 06:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Re: Loop uptime questions (Bash)

Posted 29 January 2013 - 10:09 AM

Thanks sepp2k you know Kung Fu! :smile2:/>/>

3. Third question what is the simplest for dummies way to right align the Number counting for my code?
This way of doing things "cat machines.txt | head -11 | nl" doesn't seem to offer as simple and intuitive way to format how my output should be presented.

$ bash loop_uptime_05.sh 
1        Machine 1
2        Machine 2
3        Machine 3
4        Machine 4
5        Machine 5
6        Machine 6
7        Machine 7
8        Machine 8
9        Machine 9
10        Machine 10
11        Machine 11


cat machines.txt | head -11 | nl

     1	Machine 1
     2	Machine 2
     3	Machine 3
     4	Machine 4
     5	Machine 5
     6	Machine 6
     7	Machine 7
     8	Machine 8
     9	Machine 9
    10	Machine 10
    11	Machine 11




#!/bin/bash

IFS=$'\n'

n=0


LIST=$(cat ./machines.txt | head -11)

for host in $LIST; do
    let n++
    echo "$n        $host"
done

#####################
echo
echo

echo "cat machines.txt | head -11 | nl"
echo
cat machines.txt | head -11 | nl



Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Loop uptime questions (Bash)

Posted 29 January 2013 - 07:12 PM

Take a look at the -n and -w flags. From man nl:

       -n, --number-format=FORMAT
              insert line numbers according to FORMAT
[...]
       -w, --number-width=NUMBER
              use NUMBER columns for line numbers
[...]
       FORMAT is one of:

       ln     left justified, no leading zeros

       rn     right justified, no leading zeros

       rz     right justified, leading zeros


This post has been edited by sepp2k: 29 January 2013 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Re: Loop uptime questions (Bash)

Posted 23 February 2013 - 12:51 PM

Reminder to my future self. :walkman:/>/>

http://linuxcommando...xt-file-on.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1