LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2013, 04:29 AM   #1
qrpengx
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Rep: Reputation: 0
Unhappy How to use "for" modify my shell script


hi~
I have a shell script.but it's too large.
How to use "for" modify this shell script.

#!/bin/sh
mono=`which mono`
Release_DIR="/opt/Release"
exe="${Release_DIR}/XXX.exe"
###########
server2_DIR="/opt/server2"
server3_DIR="/opt/server3"
server4_DIR="/opt/server4"
server5_DIR="/opt/server5"
server6_DIR="/opt/server6"
server7_DIR="/opt/server7"
server8_DIR="/opt/server8"
server9_DIR="/opt/server9"
server10_DIR="/opt/server10"
##########
s2=`netstat -ant|grep 0:5001|wc -l`
s3=`netstat -ant|grep 0:5002|wc -l`
s4=`netstat -ant|grep 0:5003|wc -l`
s5=`netstat -ant|grep 0:5004|wc -l`
s6=`netstat -ant|grep 0:5005|wc -l`
s7=`netstat -ant|grep 0:5006|wc -l`
s8=`netstat -ant|grep 0:5007|wc -l`
s9=`netstat -ant|grep 0:5008|wc -l`
s10=`netstat -ant|grep 0:5009|wc -l`
##########
if [ -d ${server2_DIR} ]; then
if [ $s2 -eq 0 ]; then screen -dmS S2 $mono $exe ${server2_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server3_DIR} ]; then
if [ $s3 -eq 0 ]; then screen -dmS S3 $mono $exe ${server3_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server4_DIR} ]; then
if [ $s4 -eq 0 ]; then screen -dmS S4 $mono $exe ${server4_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server5_DIR} ]; then
if [ $s5 -eq 0 ]; then screen -dmS S5 $mono $exe ${server5_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server6_DIR} ]; then
if [ $s6 -eq 0 ]; then screen -dmS S6 $mono $exe ${server6_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server7_DIR} ]; then
if [ $s7 -eq 0 ]; then screen -dmS S7 $mono $exe ${server7_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server8_DIR} ]; then
if [ $s8 -eq 0 ]; then screen -dmS S8 $mono $exe ${server8_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server9_DIR} ]; then
if [ $s9 -eq 0 ]; then screen -dmS S9 $mono $exe ${server9_DIR}/ > /dev/null 2>&1;fi
fi
if [ -d ${server10_DIR} ]; then
if [ $s10 -eq 0 ]; then screen -dmS S10 $mono $exe ${server10_DIR}/ > /dev/null 2>&1;fi
fi
 
Old 03-18-2013, 09:00 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
for n in 2 3 4 5 6 7 8 9 10; do
    dir=/opt/server$n
    if [ -d "$dir" ] && ! netstat -ant | grep -q 0:$(($n + 4999)); then
        screen -dmS "S$n" "$mono" "$exe" "$dir/" >/dev/null 2>&1
    fi
done
Also, don't use grep … | wc -l. This can almost always be replaced with grep -c … and very often what you really want is grep's exit code as used above.
 
Old 03-19-2013, 12:16 AM   #3
qrpengx
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for replying.
It's working
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
[SOLVED] Shell scripting, difference of "source script.sh" and "./script.sh" Squall90 Programming 4 07-31-2010 04:40 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 08:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration