Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Using the Foreach loop, Needing help Post 302564801 by grahamn95 on Friday 14th of October 2011 04:04:09 PM
Old 10-14-2011
He is the script in full.
Code:
#!/bin/bash

#Script - Functions
NO_ARGS=0
CMD=$1
SRV=$2

# Text color variables
txtund=$(tput sgr 0 1)    # Underline
txtbld=$(tput bold)       # Bold
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
txtblu=$(tput setaf 4)    # Blue
txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
txtwht=$(tput setaf 7)    # White
txtrst=$(tput sgr0)       # Text reset

txtund=$(tput sgr 0 1)          # Underline
txtbld=$(tput bold)             # Bold

#Counter-Strike: Source - Functions

#Minecraft - Functions
UpdateURL=http://ci.bukkit.org/job/dev-CraftBukkit/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar

#Backup - Functions
files=minecraft/world
ext=.tar.gz
parameters=zcvf

hdateformat=$(date '+%d-%m-%Y-%H-%M')H$ext

backupdir=minecraft/backups
expire=3

#Global - Functions

#If the user inputted the detials incorrectly
usage() {
    echo "Usage of the TmF Engine Script"
    echo "./tmf.sh $FUNC {Server}"
}

#Single Startup
server_start() {
	START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f 1`
		if [ ! -n "$START" ]; then
            echo -e "${txtgrn}${txtbld}$SRV is starting up${txtrst}"
			if [ $SRV = "minecraft" ]; then
				cd $SRV
                ./$SRV.sh
                cd
			else
				cd $SRV/css
                ./$SRV.sh
                cd
			fi
		else
            echo -e "${txtred}${txtbld}Error: $SRV is already running!${txtrst}"
		fi
}

#Single Stop
server_stop() {
STOP=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f 1`
    if [ "$(screen -ls | grep $SRV)" == "" ] ; then
        echo -e "${txtred}${txtbld}Error: $SRV is not running!${txtrst}"
    else
        if [ $SRV = "minecraft" ]; then
            screen -x minecraft -p 0 -X stuff "Server shutting down$(echo -ne '\r')"
            screen -x minecraft -p 0 -X stuff "stop$(echo -ne '\r')"
            rm -R minecraft/server.log
            rm -R minecraft/worldedit.log
            cd
            echo -e "${txtgrn}${txtbld}$SRV is now offline${txtrst}"
        else
            sleep 5
            screen -dr $STOP -X quit
            echo -e "${txtgrn}${txtbld}$SRV is now offline${txtrst}"
        fi
    fi
}

#Update Check - Updates the correct server with the right parameters
server_update() {
    if [ $SRV = "minecraft" ]; then
        echo -e "${txtblu}${txtbld}Prompting users that update is incoming. Waiting for 60secs${txtrst}"
        screen -x minecraft -p 0 -X stuff "say Going down for an update in 60 seconds.$(echo -ne '\r')"
        sleep 55
        screen -x minecraft -p 0 -X stuff "say Going down for an update in 5 seconds.$(echo -ne '\r')"
        sleep 5
        screen -x minecraft -p 0 -X stuff "stop$(echo -ne '\r')"
        sleep 5
        server_update_MINECRAFT
    else
        server_stop
        sleep 5
        server_update_CSS
    fi
}

#Minecraft Update - Function
server_update_MINECRAFT() {
    PRC=`ps x | grep $PRC | grep -v grep | cut -d '?' -f 1`
    STILLRUN=`ps x | grep $PRC | grep -v grep | cut -d '?' -f 1`
        if [ "$STILLRUN" = "$PRC" ]; then
            echo -e "${txtred}${txtbld}Error: $SRV was unable to shutdown!${txtrst}"
            exit
        else
            cd minecraft
            wget -N $UpdateURL
            cd
            echo -e "${txtgrn}${txtbld}$SRV has been updated successfully${txtrst}"
        fi
}

#Counter-Strike: Source update function
server_update_CSS() {
    PRC=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f 1`
        if [ ! -n "$PRC" ]; then
            PRC=999999999999
        else
            kill -9 $PRC
            echo -e "${txtgrn}${txtbld}$SRV has been successfully shutdown${txtrst}"
        fi
    STILLRUN=`ps x | grep $PRC | grep -v grep | cut -d '?' -f 1`
        if [ "$STILLRUN" = "$PRC" ]; then
            echo -e "${txtred}${txtbld}Error: $SRV was unable to shutdown!${txtrst}"
            exit
        else
            echo "Updating $SRV now"
            if [[ $SRV = 'badwater' || $SRV = 'controlpoint' ]]; then
				/home/server/1hldsupdatetool/./steam -command update -game tf -dir "/home/server/$SRV"
			else
				/home/server/1hldsupdatetool/./steam -command update -game "counter-strike source" -dir "/home/server/$SRV"
			fi
			echo -e "${txtgrn}${txtbld}$SRV has been updated successfully${txtrst}"
			server_start
		fi
}

#Single Restart
server_restart() {
    server_stop
    sleep 10
    server_start
}

#Minecraft Backup - Function
server_minecraft_backup() {
        #Does the backup folder exist if not create it
    if [ -d $BackupDIR ]; then
        sleep 0
    else
        mkdir -p $BackupDIR
    fi

    #is the server online, if not dont backup
    if [ "$(screen -ls | grep minecraft)" == "" ] ; then
        exit
    else
        #Minecraft Server messages (Backup Starting and stopping world saving)
        screen -x minecraft -p 0 -X stuff "say Backup Started, Saving turned off.$(echo -ne '\r')"
        screen -x minecraft -p 0 -X stuff "save-off$(echo -ne '\r')"
        
        #Compressions or the Files
        tar $parameters $hdateformat $files
        
        #Move compressed files to the backups folder
        mv $hdateformat $backupdir
        
        #Remove old backups
        find $backupdir/ -name *H$ext -mtime +$expire -exec rm {} \;
        
        #Minecraft Server messages (Backup Finished and starting world saving)
        screen -x minecraft -p 0 -X stuff "say Backup Finished, Saving turned on.$(echo -ne '\r')"
        screen -x minecraft -p 0 -X stuff "save-on$(echo -ne '\r')"
    fi
}

server_status() {
if [ "$(screen -ls | grep aim)" == "" ] ; then
    echo -e "Aim --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Aim --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep badwater)" == "" ] ; then
    echo -e "Badwater --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Badwater --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep deathrun)" == "" ] ; then
    echo -e "Deathrun --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Deathrun --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep event)" == "" ] ; then
    echo -e "Event --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Event --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep jailbreak)" == "" ] ; then
    echo -e "Jailbreak --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Jailbreak --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep left4dead2)" == "" ] ; then
    echo -e "Left4Dead2 --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Left4Dead2 --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep minecraft)" == "" ] ; then
    echo -e "Minecraft --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "Minecraft --- ${txtgrn}${txtbld}Online${txtrst}"
fi
sleep 1
if [ "$(screen -ls | grep skillsurf)" == "" ] ; then
    echo -e "SkillSurf --- ${txtred}${txtbld}Offline${txtrst}"
else
    echo -e "SkillSurf --- ${txtgrn}${txtbld}Online${txtrst}"
fi
}

case $CMD in
	stop)
		server_stop
	;;
	start)
		server_start
	;;
	update)
		server_update
    ;;
	status)
		server_status
	;;
    restart)
        server_restart
    ;;
    backup)
        server_minecraft_backup
    ;;
	*)
		server_usage 
esac

exit 0

I am wanting it to Update / Start / Restart loads of servers using only 1 command.

For example

./tmf.sh start-all
./tmf.sh stop-all
./tmf.sh update-all
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

foreach loop question

Hello, I am new at this forum so please bare with me on this. Within a given directory, I have a list of files in which in each file, I would like to do a substitution. I would like to substitute the string mlcl to mll in each file using the foreach command. I dont quite get how to do that. If... (7 Replies)
Discussion started by: clipski
7 Replies

2. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies

3. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

4. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

5. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

6. UNIX for Advanced & Expert Users

Problem with foreach loop

Hi All, Is there any problem with the below 'foreach' loop? foreach risk_factor ($(cat "$rf_list")) where "rf_list=$SCRIPT/Utility/rflist.txt " I'm wondering, it is throwing below error message: syntax error at line 34: `(' unexpected Any idea/suggestions ? Thanks in advance /... (7 Replies)
Discussion started by: ganapati
7 Replies

7. Shell Programming and Scripting

Using sed with a foreach loop

So I am back again beating my head against the wall with a shell script and getting a headache! I want to change each year in a file (1980, 1981, 1982, 1983, etc.) to the same year followed by a tab. The input is "blah blah (1980) blah blah". I want to get "blah blah (1980 ) blah blah".... (2 Replies)
Discussion started by: Peggy White
2 Replies

8. Shell Programming and Scripting

foreach loop problem

Dear all, I wrote a script to download files and move files in directories according to their name. Now here is the problem: Both p101 and p360 data download successfully, but when I move them according to the year and month, only p101 data can be placed at the right location, p360,... (1 Reply)
Discussion started by: handsonzhao
1 Replies

9. UNIX for Dummies Questions & Answers

foreach loop in csh

Hi everyone I'm new to unix and encountered a small problem i couldnt find out a reason why it doesn't work..please help.. in my csh script when i tried to use the foreach loop like this: foreach x ( ls ) echo $x end when i tried to run it, it printed out 'ls' to the std out instead of... (3 Replies)
Discussion started by: ymc1g11
3 Replies

10. Shell Programming and Scripting

Foreach loop with two variables

I need to put together a script that will take the contents of two different files (database name and database owner) and put them in two variables within a line: foreach x (`cat /local/hd3/dba/tools/build_db_scripts/dbs`) foreach z (`cat /local/hd3/dba/tools/build_db_scripts/dbas`)... (6 Replies)
Discussion started by: deneuve01
6 Replies
All times are GMT -4. The time now is 02:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy