defining variables

Hey all, I was wondering if someone would take a look at this script I'm working on. I don't know if i have the syntax correct for my variables and if the for loop is written correctly.

any assistance would be greatly appreciated.

#!/usr/bin/bash

###########################################
# Written By: em
# Purpose: This script was written to make changes to a basic zone setup
# Date: 01 July 2008
###########################################

# My Variables
DMI_FILE="/etc/dmi/conf"
SNMP_FILE="/etc/snmp/conf"

##########
# Code
##########

if [[ -e $DMI_FILE ]]; then #check to see if the file exists
for conf in $($DMI_FILE)
do
mv conf conf.orig
done
else
print "$DMI_FILE does not exist"
exit
fi

echo "/etc/dmi/conf has been moved"

if [[ -e $SNMP_FILE ]]; then #check to see if the file exists
for conf in $($SNMP_FILE)
do
mv conf conf.orig
done
else
print "$SNMP_FILE does not exist"
exit
fi

echo "/etc/snmp/conf has been moved"

#
# disable services
#
/usr/sbin/svcadm disable svc:/network/finger:default
/usr/sbin/svcadm disable svc:/network/login:rlogin
/usr/sbin/svcadm disable svc:/network/shell:default
/usr/sbin/svcadm disable svc:/network/telnet:default
/usr/sbin/svcadm disable svc:/network/rpc/rstat:default
/usr/sbin/svcadm disable svc:/network/rpc/rusers:default
/usr/sbin/svcadm disable svc:/network/smtp:sendmail
/usr/sbin/svcadm disable svc:/network/ftp:default
/usr/sbin/svcadm disable svc:/system/filesystem/autofs:default

echo "The requested services have been disabled"

#
# disable automounter
#
/usr/bin/svcadm disable autofs

echo "automounter disabled"

#
# edit auto_master
#
vi /etc/auto_master

echo "Basic Zone Setup is complete"

Did you run the script? If you did you would have received error messages telling you what and where the errors were.

When posting code, please put it in

 tags.



if [[ -e $DMI_FILE ]]; then #check to see if the file exists

[/quote]

[indent]
[[ -e ... ]] is not standard and, in this case, offers nothing over the standard [ -e ... ].

There's an obvious mistake. Why are you using command substitution when the variable does not contain a command?

typo, i forgot the 'ls' in there.

here is are the changes that i made.


#!/usr/bin/ksh

if [ -e /etc/dmi/conf/ ]; then #check to see if /etc/dmi/conf exists
   mv conf conf.orig # move the file to .orig
else
   print "/etc/dmi/conf does not exist"
fi

if [ -e /etc/snmp/conf/ ]; then # check to see if /etc/snmp/conf exists
   mv conf conf.orig # move the conf to .orig
else
   print "/etc/snmp/conf does not exist"
fi

What are you trying to move? You haven't checked whether there is a file (or directory) called conf in the current directory.

How do you know that /etc/dmi/conf doesn't exist? You didn't test for it; you tested for a directory of that name.

The same comments apply to the next section as well.

Do you mean test, like so:

test -d /etc/dmi/conf
if [ "$?" -eq 0 ]
then
   print '/etc/dmi/conf does exist'
else
   print '/etc/dmi/conf does NOT exist'
fi

and when executed...
em23@sparky:~$ ./test.sh
/etc/dmi/conf does exist

test is a synomym for [.

That's the same as

test -d /etc/dmi/conf
if test "$?" -eq 0

It would normally be written as:

if test -d /etc/dmi/conf

Or:

if [ -d /etc/dmi/conf ]

here is the error that i get when i run the script.

mv: cannot access conf
mv: cannot access conf

so would the mv statement be written into this like so...

if [ -d /etc/dmi/conf/ ];
then
   print '/etc/dmi/conf/ does exist'
do
   mv "$?" conf.orig
else
   print '/etc/dmi/conf/ does NOT exist'
fi

nevermind. disregard my last. my test showed an error in the 'do' line.

Is the start of the script trying to rename both the files named in shell variables ${DMI_FILE} and ${SNMP_FILE} , rather than a file called "conf" in the current working directory ?
If so, then please try the construct below (untested) and remove the "echo" from the "mv" line if it does what you want. I've avoided having a shell variable called "conf" because it appears to be causing confusion! Where you had a shell variable called "conf" I have called it "filename". We are back to "bash" because I don't think that "test -e" is valid in ksh.

#!/usr/bin/bash

# My Variables
DMI_FILE="/etc/dmi/conf"
SNMP_FILE="/etc/snmp/conf"

for filename in "${DMI_FILE}" "${SNMP_FILE}"
do
        if [ -e "${filename}" ]
        then
                echo mv "${filename}" "${filename}.orig"
                echo "${filename} has been moved"
        else
                echo "${filename} does not exist"
                exit
        fi
done

actually, conf is a directory and i'm trying to move said directory (conf) to conf.orig which works just fine if i do mv /etc/dmi/conf /etc/dmi/conf.orig. but i'm working with multiple zones in solaris, and every setup I do I have a list of basic steps, which i'm trying to throw into a script.

okay, i get what you are saying.

Thanks

looks like that did the trick, methyl. :b:

thank you both for your feedback and suggestions.