How to read one row at time using shell script?

Hello Team,

I need information on how to read one row at time using shell script. For example i have below data.

service-description                   servername    warning  critical  mountpoint
disk-usage-tmp  generic-service  test1               80       90        /tmp
disk-usage-var  generic-service   test1               80       90        /var
disk-usage-data  generic-service  test1               80       90       /data

I want to read one row at time from above and create like below configuration for each service-desription

define service{
       service_description       disk-usage-tmp
       use                          generic-service
       host_name                 test1
       check_command             check_ssh_disk!80!90!/tmp
      
}

Br,Pradeep

exec 9<my.file
while in=`line <&9`
do
  set $in
  in=$n
  command with positional parameter $in
done

is one way. If you have $4 instead of $n you will get the 4th item on the line, space delimited.

something along these lines....
awk -f gh.awk myInputFile where gh.awk is:

FNR==1 {n=split("service_description use host_name check_command",hA, FS);next }
{
  print "define service{"
  for(i=1;i<=n; i++)
    print "\t" hA, (i!=n)?$i:("check_ssh_disk!"$(NF-2)"!"$(NF-1)"!"$NF"\n}")
}

These are two different things, yes?

To read a line into a variable VAR do:

while read VAR ; do
     echo the read line is: $VAR         # this is an example how to use the variable
done < /path/to/your/input.file

If your line has several words you can use the shells field-splitting abilities to read the line into several variables. The first "field" goes to the first variable, the second to the second variable, etc. If there are less input words than variables the last variables will be empty. If there are more input words the last variable will get all the words which are not assigned up to there.

I suggest you try the following to sunderstand the mechanism: create a file with the following content:

word1
word1 word2
word1 word2 word3
word1 word2 word3 word4
word1 word2 word3 word4 word5

Now try this code with the saved file:

while read VAR1 VAR2 VAR3 ; do
     echo VAR1=\"$VAR1\" VAR2=\"$VAR2\" VAR3=\"$VAR3\"
done < /path/to/input.file

As a final addition you can use/modify the shell-internal IFS-variable to influence the way the shell defines what a "word" is. Per default the IFS value is a space, which is why in the above example "word1" was going into one variable and "word2" into the next. Suppose you have this file:

first value here=second value here=third value here

you could read that in, split not at spaces but at equal signs like this:

while IFS="=" read VAR1 VAR2 VAR3 ; do
     echo VAR1=\"$VAR1\" VAR2=\"$VAR2\" VAR3=\"$VAR3\"
done < /path/to/input.file

I hope this helps.

bakunin

How about this:

while read desc use server warn crit mount
do
   if [ "$desc" != "service-description" ]
   then
       printf "define service{\n"
       printf "\t%s\t%s\n" "service_description" "$desc"
       printf "\t%s\t%s\n" "use" "$use"
       printf "\t%s\t%s\n" "host_name" "$server"
       printf "\t%s\t%s\n" "check_command" "check_ssh_disk!$warn!$crit!$mount"
       printf "\n}\n"
   fi
done < infile
1 Like

Hello, Thank you all for reply. using Chubler_XL script, I am getting my output.

Try also

{ read X; while read SD US HN WM CM CC X
 do cat <<EOF
define service{
        service_description        $SD
        use        $US
        host name  $HN
        check_command      check_ssh_disk!${WM}!${CM}!$CC
}
EOF
        done; } < file
define service{
	 service_description	disk-usage-tmp
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/tmp
}
define service{
	 service_description	disk-usage-var
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/var
}
define service{
	 service_description	disk-usage-data
	 use	generic-service
	 host name	test1
	 check_command	check_ssh_disk!80!90!/data
}

@wbport: On my linux system, line closes stdin after reading one single line of text, so your proposal in post#2 will enter an infinite loop...

1 Like