Find the answer to your Linux question:
Results 1 to 9 of 9
Hi. I'm trying to create a post install lockdown script to apply to multiple systems with various flavors of linux/unix (all have bash installed) and I want it to look ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10

    bash conditional statements question

    Hi. I'm trying to create a post install lockdown script to apply to multiple systems with various flavors of linux/unix (all have bash installed) and I want it to look in the rcx.d folders for specific files and move them from Snnxxxx to snnxxx in order to disable them.

    I'm currently using a script as a base and i just need to do some modifications.
    This how that part of the script looks now:

    Code:
    cd /etc/rc3.d;
    for i in 16boot.server 50apache 75seaport 76snmpdx 77dmi 81volmgt 82initsma 84appserv 90samba; 
    do mv ./S$i ./s$i
    done
    The only problem is, that the files in the rc3.d folder varies from system to system and it keeps spitting out errors when these files don't exist, which makes it a problem because it's hard to know what it did or did not do. And to manually check every system will miss the whole point of a system wide script.

    What I am trying to get to is a conditional statement or something similar that will check the existence of the files (like if [ -e ]) and then execute the mv command only for the existing files. is it possible to do a loop command within the if command? Or is there an easier way to do it?

    Thanks in advance

  2. #2
    Just Joined! hunter_thom's Avatar
    Join Date
    Apr 2010
    Posts
    89
    So, if I understand correctly, the only scripts you want to rename are the ones you have in that loop? If so, why not try this:

    Code:
    cd /etc/rc3.d;
    for i in 16boot.server 50apache 75seaport 76snmpdx 77dmi 81volmgt 82initsma 84appserv 90samba; do
    if [ -f "./S$i" ]; then
        mv ./S$i ./s$i
    fi
    done
    That will loop over the names you provided and only rename if they exist.

  3. #3
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    you should be able to throw in an if statement to check first.

    Code:
    ....
    do
    if [ -e ./S$i ] 
        mv ./S$i ./s$i
    fi
    done
    not tested, but you get the idea

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10
    Thamks alot for the help, however the issue is that on some systems (Solaris 10) for some reason The apache exist as S50apache & s50apache and it will not overwrite if the files are identical
    Code:
    mv: ./S50apache and ./s50apache are identical
    of course i can do a solaris only script, and that wont a huge hassle, but i have this thing that if i run in to a problem i MUST find the right solution. its a mental disorder .

    Ok I know i can solve it the manual way, but how do i do it the right way so i will have the solution in the future when i will have a need for it.

    Thanks again.


    Quote Originally Posted by markcole View Post
    you should be able to throw in an if statement to check first.

    Code:
    ....
    do
    if [ -e ./S$i ] 
        mv ./S$i ./s$i
    fi
    done
    not tested, but you get the idea

  5. #5
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Try using the -f flag with move so it doesn't complain if it would still fit your intention.

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10
    I don't know why, but the -f does not work in solaris neither does -i (???)

    Solaris is knida screwed up system compared to the open source *nix, it works everywhere else.

    I guess I'll have to do what I really hate to do, just forget about it .

    btw, -f is default with mv cmd, and it is mentioned in man mv, anyone here use solaris and can test if that is the case on their system too?

    Quote Originally Posted by markcole View Post
    Try using the -f flag with move so it doesn't complain if it would still fit your intention.
    Last edited by rabbidroid; 12-23-2010 at 02:03 PM. Reason: addition

  7. #7
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    How about doing a rm on the s50 version before trying the mv?

  8. #8
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10
    yea, thought about that but that wont be a good idea if i run this on a system that does not have the s50 file.

    Quote Originally Posted by markcole View Post
    How about doing a rm on the s50 version before trying the mv?

  9. #9
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    You can check and see if it exists first. Then take necessary steps after.

    Code:
    if [ -e ./s$i ] 
        rm ./s$i
    fi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...