Category Archives: shell

shellで複数のユーザを追加する方法

shellコード:
#!/bin/bash
#useradd andpassword
echo “please input username:”
read name
echo “please input number:”
read num
n=1
while [ $n -le $num ]
do
useradd $name$n
n=expr $n + 1
done
#/etc/shasow info
echo “please input passwd”
read passwd
m=1
while [ $m -le $num ]
do
echo $passwd |passwd –stdin $name$m >/dev/null
m=expr $m + 1
done

Shellでcsvファイルを読み込んでMySQLに更新する

shellコード:
#!/bin/bash
FILE=’20161204cft.csv’
MYSQL=’/usr/local/mysql/bin/mysql’
USER=’root’
PASSWD=’changfa989′
DB=’JIKEDB’

while read i
do
eval $(echo $i|awk -F’,’ ‘{ printf(“a=%s\nb=%d\nc=%d\nd=%d\ne=%d\nf=%s”,$1,$2,$3,$4,$5,$6); }’)
SQL=”update it_reportb set activeuser=$b,ctnpush=$c,wangmengnu=$d,wangmenguser=$e where name=’$a’ and date=’$f'”
$MYSQL -u$USER -p$PASSWD $DB -e “$SQL”
done < $FILE

shellでランダム乱数作成($RANDOM)方法

方法1
numbers=(122 134 132 154 160 189)

while true
do
index=$(($RANDOM % ${#numbers[*]}))
echo numbers[$index]=${numbers[$index]}
sleep 1
done

方法2
while true
do
RANDOM=$((10#$(date +%N)))
echo random=$RANDOM
sleep 1
done

方法3
random ()
{
local l_min=$1
local l_max=$2
local l_div=$3
local l_range=$(($l_max – $l_min + 1))

RANDOM=date +%N
local number=$((($RANDOM % $l_range + $l_min + $l_div – 1) / $l_div * $l_div))

echo $number
}

update_count ()
{
case $1 in
3) ((numbers[3]++)) ;;
6) ((numbers[6]++)) ;;
9) ((numbers[9]++)) ;;
12) ((numbers[12]++)) ;;
15) ((numbers[15]++)) ;;
18) ((numbers[18]++)) ;;
21) ((numbers[21]++)) ;;
24) ((numbers[24]++)) ;;
27) ((numbers[27]++)) ;;
30) ((numbers[30]++)) ;;
esac
}

count=0
readonly max_count=600
readonly g_min=1
readonly g_max=30
readonly g_div=3

while [ $count -lt $max_count ]
do
update_count random $g_min $g_max $g_div

((count++))
done

echo numbers[3]=${numbers[3]}
echo numbers[6]=${numbers[6]}
echo numbers[9]=${numbers[9]}
echo numbers[12]=${numbers[12]}
echo numbers[15]=${numbers[15]}
echo numbers[18]=${numbers[18]}
echo numbers[21]=${numbers[21]}
echo numbers[24]=${numbers[24]}
echo numbers[27]=${numbers[27]}
echo numbers[30]=${numbers[30]}

Shell(シェルスクリプト)でMySQLの作成、テーブルの作成、挿入、削除操作方法

参考コード:
#!/bin/bash

HOSTNAME=”XXX.XXX.XXX.157″
PORT=”3306″
USERNAME=”root”
PASSWORD=”888888″
DBNAME=”cftdbinfo”
TABLENAME=”cfttblname”

createdbSQL=”create database IF NOT EXISTS ${DBNAME}”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e “${createdbSQL}”
#テーブルの作成
createTblSQL=”create table IF NOT EXISTS ${TABLENAME} ( name varchar(20), id int(11) default 0 )”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${createTblSQL}”

#データの挿入
insertSQL=”insert into ${TABLENAME} values(‘shimara’,2)”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${insertSQL}”

#データのクエリ
selectSQL=”select * from ${TABLENAME}”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${selectSQL}”

#データを更新
update_sql=”update ${TABLENAME} set id=3″
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${update_sql}”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${selectSQL}”

#データの削除
deleteSQL=”delete from ${TABLENAME}”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${deleteSQL}”
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${selectSQL}”

Linuxでシェル(shell)を使ってmongodbを操作する方法

#vi /etc/init.d/mongodo
shellコード:
#!/bin/sh
#
#mongod – Startup script for mongod
#
# chkconfig: – 85 15
# description: Mongodb database.
# processname: mongod
# Source function library

. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# OPTIONS
OPTIONS=” –dbpath=/home/data/mongodb/ –logpath=/home/data/mongodb/mongodb.log –logappend &”
#mongod
mongod=”/usr/local/mongodb/bin/mongod”
lockfile=/var/lock/subsys/mongod
start()
{
echo -n $”Starting mongod: ”
daemon $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
}

stop()
{
echo -n $”Stopping mongod: ”
killproc $mongod -QUIT
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
}

restart () {
stop
start
}
ulimit -n 12000
RETVAL=0

case “$1” in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $lockfile ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo “Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}”
RETVAL=1
esac
exit $RETVAL

#chmod+x /etc/init.d/mongodo
操作方法
#service mongodb start|stop|restart
#/etc/init.d/mongodb start|stop|restart

shell入門–文字列を抜き出す方法

参考コード:
strCtn=”1,2,3,4,5,a”
echo ${strCtn##*,}
echo ${strCtn#*,}
echo ${strCtn%%,*}
echo ${strCtn%,*}

結果:
a
2,3,4,5,a
1
1,2,3,4,5

shellでランダム文字列を作成する方法

1.shellで20桁のランダム文字列を作成する
#!/bin/bash
randstr() {
index=0
str=””
for i in {a..z}; do arr[index]=$i; index=expr ${index} + 1; done
for i in {A..Z}; do arr[index]=$i; index=expr ${index} + 1; done
for i in {0..9}; do arr[index]=$i; index=expr ${index} + 1; done
for i in {1..20}; do str=”$str${arr[$RANDOM%$index]}”; done
echo $str
}

echo randstr
2.shellでランダムもじれるを作成
#!/bin/bash
MATRIX=”0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+=”
LENGTH=”9″
while [ “${n:=1}” -le “$LENGTH” ]
do
PASS=”$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}”
let n+=1
done
echo “$PASS”
exit 0

CentOSにmemcachedを起動するスクリプトコード

#vim /etc/init.d/memcached

サンプルコード:
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: – 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = “no” ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z “$DUNDARGS” ] && exit 0
MEMCACHED=”/usr/local/memcached/bin/memcached”
SERVER_IP=”172.17.200.98″
SERVER_PORT=”11211″
[ -f $MEMCACHED ] || exit 1
start()
{
echo -n $”Starting memcached: ”
daemon $MEMCACHED -u daemon -d -m 2048 -l $SERVER_IP -p $SERVER_PORT -P /tmp/memcached.pid
echo
}
stop()
{
echo -n $”Shutting down memcached: ”
killproc memcached
echo
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac
exit 0

CentOSでTomcat の起動スクリプトサンプルコード

コード下記:

#!/bin/bash
#
# chkconfig: – 95 15
# description: Tomcat start/stop/status script
#Location of JAVA_HOME (bin files)
export JAVA_HOME=

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/usr/local/tomcat

#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=www

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE=”Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}”

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
echo ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2
}

start() {
pid=$(tomcat_pid)
if [ -n “$pid” ];then
echo -e “\e[00;31mTomcat is already running (pid: $pid)\e[00m”
else
echo -e “\e[00;32mStarting tomcat\e[00m”
if [ user_exists $TOMCAT_USER = “1” ];then
su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
else
$CATALINA_HOME/bin/startup.sh
fi
status
fi
return 0
}

status(){
pid=$(tomcat_pid)
if [ -n “$pid” ];then
echo -e “\e[00;32mTomcat is running with pid: $pid\e[00m”
else
echo -e “\e[00;31mTomcat is not running\e[00m”
fi
}

stop() {
pid=$(tomcat_pid)
if [ -n “$pid” ];then
echo -e “\e[00;31mStoping Tomcat\e[00m”
$CATALINA_HOME/bin/shutdown.sh

let kwait=$SHUTDOWN_WAIT
count=0;
until [ ps -p $pid | grep -c $pid = ‘0’ ] || [ $count -gt $kwait ]
do
echo -n -e “\e[00;31mwaiting for processes to exit\e[00m\n”;
sleep 1
let count=$count+1;
done

if [ $count -gt $kwait ];then
echo -n -e “\n\e[00;31mkilling processes which didn’t stop after $SHUTDOWN_WAIT seconds\e[00m”
kill -9 $pid
fi
else
echo -e “\e[00;31mTomcat is not running\e[00m”
fi

return 0
}

user_exists(){
if id -u $1 >/dev/null 2>&1; then
echo “1”
else
echo “0”
fi
}

case $1 in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

status)
status
;;

*)
echo -e $TOMCAT_USAGE
;;
esac
exit 0

Linuxでプロセスごとのメモリ使用量を確認するシェルコード

コード下記
echo -e “PID\t\tMem\t\tProc_Name”
for pid in ls -l /proc | grep ^d | awk '{ print $NF }'| grep -v [^0-9]
do
if [ $pid -eq 1 ];then continue;fi
grep -q “Rss” /proc/$pid/smaps 2>/dev/null
if [ $? -eq 0 ];then
swap=$(grep Rss /proc/$pid/smaps \
| gawk ‘{ sum+=$2;} END{ print sum }’)
proc_name=$(ps aux | grep -w “$pid” | grep -v grep \
| awk ‘{ for(i=11;i<=NF;i++){ printf(“%s “,$i); }}’)
if [ $swap -gt 0 ];then
echo -e “${pid}\t${swap}\t${proc_name}”
fi
fi
done | sort -k2 -n | awk -F’\t’ ‘{
pid[NR]=$1;
size[NR]=$2;
name[NR]=$3;
}
END{
for(id=1;id<=length(pid);id++)
{
if(size[id]<1024)
printf(“%-10s\t%15sKB\t%s\n”,pid[id],size[id],name[id]);
else if(size[id]<1048576)
printf(“%-10s\t%15.2fMB\t%s\n”,pid[id],size[id]/1024,name[id]);
else
printf(“%-10s\t%15.2fGB\t%s\n”,pid[id],size[id]/1048576,name[id]);
}
}

2 / 512345