determine the number of spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting determine the number of spaces
# 1  
Old 02-18-2011
determine the number of spaces

Here is a weird question Smilie

i am trying to create a script written in bash that will create configuration files for nagios. As some of you aware is has to be written in the below format:

Code:
define service{
                   option1           value1
                   option2           value2
                   optionn           valuen
}

what i want to do is print the options along with their values in an organized way. but the length of the option names may differ so i am looking for a way to spesify how many spaces or tabs the script should echo for each line. Is there a way to do this?

example of what i dont want Smilie :

Code:
define service{
                   option1                value1
                   option2           value2
                   optionn                       valuen
}


Last edited by ppolianidis; 02-18-2011 at 12:00 PM.. Reason: spaces are deleted
# 2  
Old 02-18-2011
What your input looks like?
# 3  
Old 02-18-2011
i will use some options of nagios that i am familiar with. By now my script looks like this, but its far from coplete:

Code:
#!/bin/sh
#######################################################################
#This is a tool in order to create nagios checks using existing commands and existing contact groups#
#it can run with/without arguments


#######################INITIALIZATION##################################
NAG_PATH=/opt/nms/nagios
SERV_PATH=$NAG_PATH/etc/objects
TMP_CHECK_FILE=/opt/nms/nagios/etc/objects/SERVICES_TEMP.scg

OPTIONS=(name active_checks_enabled passive_checks_enabled parallelize_check obsess_over_service check_freshness notifications_enabled event_handler_enabled flap_detection_enabled failure_prediction_enabled process_perf_data retain_status_information retain_nonstatus_information contact_groups notification_interval is_volatile check_period normal_check_interval retry_check_interval max_check_attempts notification_period notification_options)
OPTION_VALUES=(generici-service 1 1 1 1 0 1 1 1 1 1 1 1 technical-support 10 0 24x7 5 15s 3 24x7 "w,u,c,r,f")
echo -e "define service{"
for (( i = 0; i < ${#OPTIONS[*]}; i++ ))
do
{
        echo -e "\t\t${OPTIONS[$i]}\t\t\t${OPTION_VALUES[$i]}"
}
done
echo "}"
exit 0

# 4  
Old 02-18-2011
Use the printf formatting option (ksh) or (awk)
# 5  
Old 02-18-2011
my problem is that if the length of the option names differ more then 8 characters, the second colomn is... "not a colomn" Smilie
# 6  
Old 02-18-2011
Hum... I suppose you can generate strings like this:

Code:
#!/bin/bash
# ksh will also work

TABS=3
STR="longer-than-8"
((TABS -= (${#STR}/8) ))
[ "${TABS}" -lt 1 ] && TABS=1

while [ "$TABS" -gt 0 ]
do
        TAB="${TAB}"$'\t'
done

echo "${STR}${TAB}"

---------- Post updated at 10:36 AM ---------- Previous update was at 10:35 AM ----------

Or, what they probably meant with printf is to use spaces instead of tabs:


Code:
# Will uniformly space these 20 apart
printf "%20s" a b qweyoiuyuioyuiy hargle fleeb
echo

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-21-2011
thanks guys, i managed to do what i wanted with your help Smilie
This User Gave Thanks to ppolianidis For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for an undetermined number of spaces

I would like to find an undetermined number of spaces and shorten them to one space. I am running Debian in mksh. Script running in #!/bin/sh. Sorry to not include all code. The program is too big and involves an online file... too much hassle to solve a simple issue. Ex., I start with... (11 Replies)
Discussion started by: bedtime
11 Replies

2. UNIX for Beginners Questions & Answers

How do I determine the best number to use for the bs (block size) operand of the dd command?

When I create a bootable Linux distro installation USB drive, I use this command: sudo dd if=/Path/to/linux_distro.iso of=/dev/rdisk<disk number> bs=<number of bytes> When I look it up, I've seen variations of people choosing 4M, and I think 8M, 2M, and maybe even 1M. If I leave the operand... (4 Replies)
Discussion started by: Quenz
4 Replies

3. Shell Programming and Scripting

Determine the frequency of each number within a text file

I'd like to determine the frequency that each number occurs within a text file. I know how to do this for a single number, but not for a set or list of numbers. Here's what I have grep -o 01 file.txt | wc -l Also, it's important that the numbers are two digits instead of 1, meaning that... (8 Replies)
Discussion started by: it5ju5talx
8 Replies

4. UNIX for Dummies Questions & Answers

grep any number of spaces

which one of the following is the correct expression to ignore line with multiple spaces after any string cat file | grep -v "xyz *$" or cat file | grep -v "xyz*$" do i need "*" to specify the sapce or " *" will do? (2 Replies)
Discussion started by: manishma71
2 Replies

5. Shell Programming and Scripting

construct a string with X number of spaces

I'd like to create a variable with the value of X number of space( no Perl please), printf seems to work, but , in following example,10 spaces becomes 1 space when assinged to a variable, Why? other solutions are welcome. $printf "=%10s=\n" = = $var=$(printf "=%10s=\n") echo... (4 Replies)
Discussion started by: honglus
4 Replies

6. UNIX for Advanced & Expert Users

How to determine the number of NFS threads RUNNING on the system

Hi, Anyone can tell me how to get the number of NFS threads RUNNING on the system for Solaris 10? Someone told me for Solaris 9, the method is "echo "*svc$<svcpool" | adb -k. But, I've tried to google the method for Solaris 10 and did not find the corresponding method, please help... (1 Reply)
Discussion started by: wang.caiqi
1 Replies

7. Shell Programming and Scripting

Determine number of checklist in zenity dynamically...

Hi, In my Shell Script i am counting the duplicate IPs in LAN,...After counting i have to show in checklist in zenity which one to delete from the LAN........so initially i dont know no. of duplicate IPs in the LAN....Hence i can determine how many check list needed..... Duplicate IPs... (3 Replies)
Discussion started by: shivarajM
3 Replies

8. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies

9. Shell Programming and Scripting

$A is a number / any other string? How to determine ?

I have a variable (say $A) and while passing it gets either a number or some other string. Now how can test (with if else) whether the variable is just a ne or something else ? Thanks a lot to all in advance C Saha (2 Replies)
Discussion started by: csaha
2 Replies

10. HP-UX

determine number of xterm connected

i more than one hp-ux 11.0 application server. some of these application server has an xterminal attached to them. ncd900 and ncd400. i would like to know how many xterm is connected to an application server. without physically checking each xterm, can i identify how many xterm? thanks alot. (0 Replies)
Discussion started by: inquirer
0 Replies
Login or Register to Ask a Question