|
Home > Archive > Unix Shell > October 2004 > read/write array to file with csh (tcsh)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
read/write array to file with csh (tcsh)
|
|
| Lewis Getschel 2004-10-02, 9:12 pm |
| All-
I've gotten a good interactive shell-script running for creating
configuration files for a network monitoring system (nagios). It has a
lot of repetitive components, so this works well. I'm using tcsh (tcsh
6.12.00 (Astron) 2002-07-2) on a redhat 9 system. (No flames about
using tcsh please).
My problem is using the array properties of a variable for strings
that have a space in them.
It works fine when no spaces exist (like using win_ping instead of
"win ping") try entering 2+ lines that do/don't have spaces in them to
see it yourself.
If anyone can figure out a way to get spaces to work properly for 3
or more items I'd appreciate it (I managed to find a way to get 2 to
work, but it breaks for 3 or more (by placing double-quotes around the
input ("$<") and the variable when used ("$hostservices")
here is the code segment that is causing the problem (answer with
anything except a (null,Y, or y) to start the loop.
--------------- cut here ---------------
#!/bin/tcsh -x
# remove the "-x" above to NOT run in echo mode
# Lewis Getschel email:googlegroups.2.lmg@spamgourmet.com
#
# segment of code to show arrays subscripting problem
echo 1st part of program removed from here.
set null
echo -n "Keep these values? (Y/n)"
set changeservices = $<
if ( $changeservices != $null ) then
if ( $changeservices != 'Y') then
if ( $changeservices != 'y' ) then
set newservice = 'starthere'
set hostservices = ()
echo ' 'Cleared ALL Services! Enter the ones you
want\(1 per line \(no spaces\)\):
echo ' '\(The first service will be used as the
dependency\)
while ( "$newservice" != $null )
echo -n " What service to add? (null value to end)"
set newservice = "$<"
if ( "$newservice" != $null ) set hostservices =
($hostservices $newservice)
end
echo " OK, adding these services:"
echo -n ' '\($hostservices\)' '
echo
endif
endif
endif
# Confirm users desire to continue with these values
echo ---------------------------------------------------------
echo Values that will be used:
# I cut other variables out here
set current = 1
while ( $current <= $#hostservices )
echo ' '\[$current\]' ':$hostservices[$current]
@ current = $current + 1
end
echo
echo ---------------------------------------------------------
echo Press CTRL-C to cancel, ENTER to continue
set continue = $<
--------------- cut here ---------------
To write these values for re-use in the next run of the script, the
complete code has:
--------------- cut here ---------------
set current = 1
while ( $current <= $#hostservices )
echo $hostservices[$current] >> oldservices.txt
@ current = $current + 1
end
--------------- cut here ---------------
To read from the file next time, I use:
--------------- cut here ---------------
set hostservices = `cat oldservices.txt`
--------------- cut here ---------------
Thanks,
Lewis
| |
| Bruce Barnett 2004-10-02, 9:12 pm |
| googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
> if ( $changeservices != $null ) then
usually this is done like
if ( X"$changeservices" != X"$null" ) then
> if ( "$newservice" != $null ) set hostservices =
if ( X"$newservice" != X"$null" ) set hostservices =
An example input file would help.
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
| |
| Lewis Getschel 2004-10-02, 9:12 pm |
| Bruce Barnett <spamhater103+U040930202111@grymoire.com> wrote in message news:<cji82o$o2t$0$208.20.133.66@netheaven.com>...
> googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
>
>
> usually this is done like
>
> if ( X"$changeservices" != X"$null" ) then
Thanks for pointing this out, I use this format in my windows batch
files, I forgot to here.
> An example input file would help.
An example file isn't needed for this code, but here is the ouput of
this code and the oldservices.txt file I'm using in the complete
version.
***** run with 2 items (no spaces) in the array -----
% ./newspost
1st part of program removed from here.
Keep these values? (Y/n)n
Cleared ALL Services! Enter the ones you want(1 per line (no
spaces)):
(The first service will be used as the dependency)
What service to add? (null value to end)win_ping
What service to add? (null value to end)win_cpu
What service to add? (null value to end)
OK, adding these services:
(win_ping win_cpu)
---------------------------------------------------------
Values that will be used:
[1] :win_ping
[2] :win_cpu
---------------------------------------------------------
Press CTRL-C to cancel, ENTER to continue
***** run with 2 items (WITH spaces) in the array -----
% ./newspost
1st part of program removed from here.
Keep these values? (Y/n)n
Cleared ALL Services! Enter the ones you want(1 per line (no
spaces)):
(The first service will be used as the dependency)
What service to add? (null value to end)win ping
What service to add? (null value to end)win cpu
What service to add? (null value to end)
OK, adding these services:
(win ping win cpu)
---------------------------------------------------------
Values that will be used:
[1] :win
[2] :ping
[3] :win
[4] :cpu
---------------------------------------------------------
Press CTRL-C to cancel, ENTER to continue
As you can see, the spaces really mess up the array. It changes from
an array with 2 items the 1st time, to one with 4 items the 2nd time.
If I use 4 items (NO spaces, rest of input clipped from here) the
ouput is:
Values that will be used:
[1] :win_ping
[2] :win_cpu
[3] :win_disk_free
[4] :win_swap_file_used
Help, Please.
the input file for the full program is simply:
% cat oldservices.txt
win_ping
win_cpu
%
| |
| Bruce Barnett 2004-10-02, 9:13 pm |
| googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
> Thanks for pointing this out, I use this format in my windows batch
> files, I forgot to here.
>
>
> An example file isn't needed for this code, but here is the ouput of
> this code and the oldservices.txt file I'm using in the complete
> version.
I have to run, and don't have time to debug the script.
but if you have a problem with something like
set a = "b c"
$ set d = $a
$ echo $d
b
Instead, use the :q modifyier
set d = $a:q
$ echo $d
b c
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
| |
| Bruce Barnett 2004-10-03, 5:55 pm |
| googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
> if ( $changeservices != $null ) then
usually this is done like
if ( X"$changeservices" != X"$null" ) then
> if ( "$newservice" != $null ) set hostservices =
if ( X"$newservice" != X"$null" ) set hostservices =
An example input file would help.
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
| |
| Lewis Getschel 2004-10-04, 6:01 pm |
| Bruce Barnett <spamhater103+U040930202111@grymoire.com> wrote in message news:<cji82o$o2t$0$208.20.133.66@netheaven.com>...
> googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
>
>
> usually this is done like
>
> if ( X"$changeservices" != X"$null" ) then
Thanks for pointing this out, I use this format in my windows batch
files, I forgot to here.
> An example input file would help.
An example file isn't needed for this code, but here is the ouput of
this code and the oldservices.txt file I'm using in the complete
version.
***** run with 2 items (no spaces) in the array -----
% ./newspost
1st part of program removed from here.
Keep these values? (Y/n)n
Cleared ALL Services! Enter the ones you want(1 per line (no
spaces)):
(The first service will be used as the dependency)
What service to add? (null value to end)win_ping
What service to add? (null value to end)win_cpu
What service to add? (null value to end)
OK, adding these services:
(win_ping win_cpu)
---------------------------------------------------------
Values that will be used:
[1] :win_ping
[2] :win_cpu
---------------------------------------------------------
Press CTRL-C to cancel, ENTER to continue
***** run with 2 items (WITH spaces) in the array -----
% ./newspost
1st part of program removed from here.
Keep these values? (Y/n)n
Cleared ALL Services! Enter the ones you want(1 per line (no
spaces)):
(The first service will be used as the dependency)
What service to add? (null value to end)win ping
What service to add? (null value to end)win cpu
What service to add? (null value to end)
OK, adding these services:
(win ping win cpu)
---------------------------------------------------------
Values that will be used:
[1] :win
[2] :ping
[3] :win
[4] :cpu
---------------------------------------------------------
Press CTRL-C to cancel, ENTER to continue
As you can see, the spaces really mess up the array. It changes from
an array with 2 items the 1st time, to one with 4 items the 2nd time.
If I use 4 items (NO spaces, rest of input clipped from here) the
ouput is:
Values that will be used:
[1] :win_ping
[2] :win_cpu
[3] :win_disk_free
[4] :win_swap_file_used
Help, Please.
the input file for the full program is simply:
% cat oldservices.txt
win_ping
win_cpu
%
| |
| Lewis Getschel 2004-10-07, 2:56 am |
| Bruce Barnett <spamhater103+U041001231208@grymoire.com> wrote in message news:<cjl6f6$mbc$0$208.20.133.66@netheaven.com>...
> I have to run, and don't have time to debug the script.
> but if you have a problem with something like
>
> set a = "b c"
> $ set d = $a
> $ echo $d
> b
>
> Instead, use the :q modifyier
> set d = $a:q
> $ echo $d
> b c
Trying your suggestion didn't seem to help my script (although it does
work for this example you provided, I can't find a reference for what
this :q does though).
I may have to rewrite to make different numbered variables to make
this work properly with spaces in the variable strings (something
like):
set line1 = "this is line 1"
set num = 2
set line$num = "this is line 2"
set num = 3
set line$num = "this is line 3"
echo $line1
But This doesn't work:
% echo $line$num
line: Undefined variable.
So I might need to re-think this.
Thanks for your suggestions though. I really appreciate them.
Lewis
| |
| Bruce Barnett 2004-10-07, 2:56 am |
| googlegroups.2.lmg@spamgourmet.com (Lewis Getschel) writes:
> echo $line$num
Try
eval echo \$line$num
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
|
|
|
|
|