Shell 案例
列举实际工作以及生活中用到过的 shell 脚本。
自动安装服务
bash
#!/bin/bash
#This script is only applicable to the MAC,by xujianhui
echo -e " \033[35m ******* Program installation start ******** \033[0m"
cd ~/Desktop
#unzip -o liangyu-for-mac.zip
#指定存储dmg文件的路径,一定要将其放置Desktop
dmgdir=~/Desktop/liangyu-for-mac/release/Software
#指定存储dmg文件的路径,一定要将其放置Desktop
wardir=~/Desktop/liangyu-for-mac/release/20161012_v3.2.5/Server
sqldir=~/Desktop/liangyu-for-mac/release/20161012_v3.2.5/Server
phpconfigdir=/Applications/XAMPP/xamppfiles/phpmyadmin
mysqlconfigdir=/Applications/XAMPP/htdocs/Liangyu_edu/app/config
cp $sqldir/uninstallsoft.sh ~/Desktop
cp $sqldir/updatepwd.sql ~/Desktop
#挂载dmg文件
#/usr/bin/hdiutil attach $dmgdir/jdk-8u40-macosx-x64.1427945120.dmg
#挂载的文件路径都在/Volumes/下
#安装XAMPP
function install_XAMPP() {
xamppDir=`(ls /Applications/XAMPP/ |grep xamppfiles | wc -l) 2>/dev/null`
if [ $xamppDir -ne 1 ];then #此处的1指的是wc -l得来的文件个数,而不是状态码1
echo -e " \033[31m ******* Xampp is not installed, Now will take a few minutes to install it, please wait a moment ! ******* \033[0m"
/usr/bin/hdiutil attach $dmgdir/xampp-osx-5.6.8-0-installer.dmg
cd /Volumes/XAMPP/XAMPP.app/Contents/MacOS
echo "xjh911128" | sudo -S ./installbuilder.sh
#sleep 280
echo -e " \033[36m ******** Xampp installation is complete ! ******** \033[0m"
else
echo -e " \033[36m ******** Xampp is already installed ! ******** \033[0m"
fi
}
install_XAMPP
#判断是否安装jdk并根据系统位数安装相应的jdk版本,如果jdk版本号低于1.8就安装jdk1.8原来的jdk并不卸载,更改jdk的环境变量
function install_Jdk() {
/usr/bin/hdiutil attach $dmgdir/jdk-8u40-macosx-x64.1427945120.dmg
/usr/bin/expect <<\EOF
set timeout 50
set passwd xjh911128
set passwderror 0
cd /Volumes/JDK\ 8\ Update\ 40/
spawn sudo installer -pkg JDK\ 8\ Update\ 40.pkg -target LocalSystem
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "password is error,please try again!"
exit
}
set timeout 100
set passwderror 1
send "$passwd\r"
exp_continue
timeout {
puts "connect is timeout!"
exit 3
}
#expect eof
EOF
}
#设置当前用户的jdk环境变量
function setJdkEnv() {
userFile=~/.bash_profile
if [ ! -f "$userFile" ];then
echo -e " \033[31m ******* $userFile does not exist,now will touch it!******* \033[0m"
touch "$userFile"
echo "#Set the JDK environment variables!" >> "$userFile"
echo "export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home" >> "$userFile"
echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> "$userFile"
echo "export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> "$userFile"
source ~/.bash_profile
echo -e " \033[36m ******** Complete JDK environment variable Settings ******** \033[0m"
else
echo "#Set the JDK environment variables!" >> "$userFile"
echo "export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home" >> "$userFile"
echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> "$userFile"
echo "export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> "$userFile"
source ~/.bash_profile
echo -e " \033[32m ******** Complete JDK environment variable Settings ! ******** \033[0m"
fi
}
function checkJdkVersion() {
if [[ `expr $(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }' | cut -b 1-3) \< 1.8` -eq 1 ]];then
setJdkEnv
else
echo -e " \033[36m ******* Your JDK version is "$(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }')" ******* \033[0m"
fi
}
#不能用which java判断是否安装jdk,因为mac os都默认安装了jdk1.6,但是没有配置环境变量。which java指的是jdk的安装路径。但真正的jdk安装路径并不在此,用ls -lh /usr/bin/java查看jdk真正的安装路径
#which java > /dev/null
java -version 2>/dev/null
if [ $? -ne 0 ];then
#查看默认用户安装程序目录有没有以jdk1.8开头的文件,用此来判断是不是安装了jdk1.8
ls -lh /Library/Java/JavaVirtualMachines/jdk1.8* > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e " \033[31m *******The JDK 1.8 is not installed, to install the JDK 1.8!******* \033[0m"
install_Jdk
setJdkEnv
echo -e " \033[36m ******** JDK 1.8 installation has been completed ******** \033[0m"
else
setJdkEnv
echo -e " \033[32m \033[05m ******* Your JDK version is "$(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }')" ******* \033[0m"
fi
else
#已安装jdk如果jdk版本号低于1.8就安装jdk1.8,并设置当前用户的jdk变量为jdk1.8.通过expr来判断小数大小,expr $1 $2 如果expr返回值为1时表示$1>$2,expr返回值为0时表示$1<$2.
if [[ `expr $(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }' | cut -b 1-3) \< 1.8` -eq 1 ]];then
# > /dev/null 2>&1标准输出和标准错误都不在屏幕显示。
ls -lh /Library/Java/JavaVirtualMachines/jdk1.8* > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e " \033[33m ******* Your JDK version below 1.8, will install JDK 1.8! ******* \033[0m"
install_Jdk
setJdkEnv
echo -e " \033[32m \033[05m ******* Install JDK 1.8 has been completed and update the JDK environment variables ! ******* \033[0m"
else
#checkJdkVersion
setJdkEnv
echo -e " \033[35m ******** The updated current JDK environment variables for JDK 1.8 ! ******** \033[0m"
echo -e " \033[33m ******* You have installed JDK "$(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }')" ******* \033[0m"
fi
else
echo -e " \033[36m ******* Your JDK version is "$(java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }')" ! ******* \033[0m"
fi
fi
#/usr/bin/hdiutil detach /Volumes/JDK\ 8\ Update\ 40/
cp -r $wardir/Liangyu_edu.zip /Applications/XAMPP/htdocs
cd /Applications/XAMPP/htdocs
unzip -o Liangyu_edu.zip
cp ~/Desktop/liangyu-for-mac/checkProcessStatus.sh /Applications/XAMPP/htdocs/Liangyu_edu
#cp ~/Desktop/liangyu-for-mac/checkActivemqStatus.sh /Applications/XAMPP/htdocs/Liangyu_edu
cd Liangyu_edu
#将windows上的文件编码格式(gbk dos 通过:set ff?查看,会显示dos及unix格式)改为linux的文件编码格式(utf-8)
cat install_server | tr -d '\r' > install_server1
rm -rf install_server
mv install_server1 install_server
cat start_server | tr -d '\r' > start_server1
rm -rf start_server
mv start_server1 start_server
cat update_server | tr -d '\r' > update_server1
rm -rf update_server
mv update_server1 update_server
chmod a+x install_server
chmod a+x start_server
chmod a+x update_server
chmod a+x checkProcessStatus.sh
#chmod a+x checkActivemqStatus.sh
sed -i".bak" 's/3306/8092/g' php.ini
sed -i".bak" '52s/80/8091/' httpd.conf
sed -i".bak" '20s/3306/8092/' /Applications/XAMPP/etc/my.cnf
sed -i '' '28s/3306/8092/' /Applications/XAMPP/etc/my.cnf
echo "xjh911128" | sudo -S /Applications/XAMPP/xamppfiles/xampp stopapache
sleep 10
echo "xjh911128" | sudo -S /Applications/XAMPP/xamppfiles/xampp start
#增加xampp安全性设置
echo -e " \033[36m ******* Began to software security configuration ******** \033[0m"
/usr/bin/expect <<\EOF
set timeout 100
set passwd liangyu_admin2016
set userpwd xjh911128
spawn sudo /Applications/XAMPP/xamppfiles/xampp security
expect {
"Password:"
{
send "$userpwd\r"
}
}
expect {
"*yes*"
{
send "yes\r"
expect "*assword:"
send "$passwd\r"
}
}
expect {
"Password (again):"
{
send "$passwd\r"
}
}
expect {
"*yes*" {
send "yes\r"
}
}
expect {
"*yes*" {
send "yes\r"
}
}
expect {
"*assword:" {
send "$passwd\r"
}
}
expect {
"Password (again):"
{
send "$passwd\r"
}
}
expect {
"*yes*" {
send "yes\r"
}
}
expect {
"*assword:" {
send "$passwd\r"
}
}
expect {
"Password (again):"
{
send "$passwd\r"
}
}
expect {
"*yes*" {
send "yes\r"
}
}
expect {
"*assword:" {
send "$passwd\r"
}
}
expect {
"Password (again):"
{
send "$passwd\r"
}
}
#注释脚本的内容 :<<'注释内容' 此方法在expect脚本中不适用,可在shell里适用
expect eof
EOF
sleep 30
echo -e " \033[32m \033[05m ******* The end of the software security configuration ******** \033[0m"
/Applications/XAMPP/bin/mysql -u root -pliangyu_admin2016 < /Applications/XAMPP/htdocs/Liangyu_edu/createdb.sql
#/Applications/XAMPP/bin/mysql -u root < /Applications/XAMPP/htdocs/Liangyu_edu/createdb.sql
sed -i '' '3d' /Applications/XAMPP/htdocs/Liangyu_edu/install_server
sed -i '' 's/sudo/echo "xjh911128" | sudo -S/g' /Applications/XAMPP/htdocs/Liangyu_edu/install_server
sleep 60
#修改mysql密码
#/Applications/XAMPP/bin/mysqladmin -u root password liangyu_admin2016
#如果已经修改过了密码请用如下命令修改
#mysqladmin -u root -p password newpasswd
#为了在网页中打开phpmyadmin界面也要输入密码
sudo sed -i".bak" '29s/.*/$cfg['Servers'][$i]['auth_type'] = '\'http\'';/' $phpconfigdir/config.inc.php
#修改phpmyadmin的密码,也即mysql数据库密码
sudo sed -i '' '31s/.*/$cfg['Servers'][$i]['password'] = '\'liangyu_admin2016\'';/' /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php
#修改database.php文件中的mysql密码为新设置的密码
sed -i".bak" '60s/.*/'\'password\'' => '\'liangyu_admin2016\'',/' $mysqlconfigdir/database.php
#添加端口配置文件
sed -i '' '61 i\
'\'port\'' => 8092,
' /Applications/XAMPP/htdocs/Liangyu_edu/app/config/database.php
#自动输入y
/usr/bin/expect <<\EOF
set timeout 300
set inputcommand y
cd /Applications/XAMPP/htdocs/Liangyu_edu
spawn ./install_server
expect {
"*command?*" {
send "$inputcommand\r"
exp_continue
}
expect eof
}
EOF
echo -e " \033[35m ******* End of the program installation ******** \033[0m"
#修改管理后台admin账号密码
#sed -i".bak" '14s/123456/liangyu_admin2016/' /Applications/XAMPP/htdocs/Liangyu_edu/app/databas/seeds/UserTableSeeder.php
/Applications/XAMPP/bin/mysql -u root -pliangyu_admin2016 -e "use liangyu_education;update liangyu_users set password='\$2a\$06\$OuV4A309xMud9K4Oc3PHoOOkBRfiK4HA7iwleSExN6J.K9TQd96GG' where username='admin';flush privileges;"
echo "xjh911128" | sudo -S /Applications/XAMPP/xamppfiles/xampp restart
/Applications/XAMPP/htdocs/Liangyu_edu/apache-activemq/bin/activemq start
rm -rf __MACOSX
#echo '*******添加开机启动项 ********'
echo "xjh911128" | sudo -S cp $sqldir/com.checkProcessStatus.plist /Library/LaunchDaemons
echo "xjh911128" | sudo -S launchctl load -w /Library/LaunchDaemons/com.checkProcessStatus.plist
echo -e " \033[36m ******** Complete add boot startup ******** \033[0m"
卸载服务
bash
#!/bin/bash
sqldir=~/Desktop
updatepwd="update mysql.user set password=PASSWORD('') where user='root';"
/Applications/XAMPP/bin/mysql -u root -pliangyu_admin2016 -e "drop database liangyu_education;drop database cdcol;exit;"
/Applications/XAMPP/bin/mysql -u root -pliangyu_admin2016 mysql < $sqldir/updatepwd.sql
sudo /Applications/XAMPP/xamppfiles/xampp stop
rm -rf /Applications/XAMPP/htdocs/Liangyu_edu*
sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/
rm -rf /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php.bak
sed -i '' '29s/http/config/' /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php
sed -i '' '31s/liangyu_admin2016//' /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php
#/Applications/XAMPP/bin/mysql -u root -pliangyu_admin2016 -e "drop database liangyu_education;drop database cdcol;use mysql;update user set password=PASSWORD('') where user='root';flush privileges;"
sudo launchctl unload -w /Library/LaunchDaemons/com.xampp.startServer.plist
rm -rf $sqldir/updatepwd.sql
rm -rf $sqldir/uninstallsoft.sh
安装jdk
bash
function install_Jdk() {
/usr/bin/expect <<\EOF
set timeout 50
set passwd xjh911128
set passwderror 0
cd /Volumes/JDK\ 8\ Update\ 40/
spawn sudo installer -pkg JDK\ 8\ Update\ 40.pkg -target LocalSystem
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "password is error,please try again!"
exit
}
set timeout 100
set passwderror 1
send "$passwd\r"
exp_continue
}
timeout {
puts "connect is timeout!"
exit 3
}
#expect eof
}
EOF
}
linux 添加用户
bash
#!/bin/bash
for user in "001 002 003"
do
useradd $user
echo "xjh911128" | passwd --stdin $user
echo "$user create success!"
done
mac添加用户
bash
#!/bin/bash
# 1班学生信息
class1=(S01 S02 S03 S04 S05 S06 S07 S08 S09 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S20 S21 S22 S23 S24 S25 S26 S27 S28 S29 S30 S31 S32 S33 S34 S35 S36 S37 S38 S39 S40 S41 S42 S43 S44 S45 T01 T02 T03 T04 T05 )
class1_realName=(1班学生1 1班学生2 1班学生3 1班学生4 1班学生5 1班学生6 1班学生7 1班学生8 1班学生9 1班学生10 1班学生11 1班学生12 1班学生13 1班学生14 1班学生15 1班学生16 1班学生17 1班学生18 1班学生19 1班学生20 1班学生21 1班学生22 1班学生23 1班学生24 1班学生25 1班学生26 1班学生27 1班学生28 1班学生29 1班学生30 1班学生31 1班学生32 1班学生33 1班学生34 1班学生35 1班学生36 1班学生37 1班学生38 1班学生39 1班学生40 1班学生41 1班学生42 1班学生43 1班学生44 1班学生45 1班老师1 1班老师2 1班老师3 1班老师4 1班老师5 )
class1_userID=(505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 )
# 2班学生信息
class2=(S46 S47 S48 S49 S50 S51 S52 S53 S54 S55 S56 S57 S58 S59 S60 S61 S62 S63 S64 S65 S66 S67 S68 S69 S70 S71 S72 S73 S74 S75 S76 S77 S78 S79 S80 S81 S82 S83 S84 S85 S86 S87 S88 S89 S90 T06 T07 T08 T09 T10 )
class2_realName=(2班学生1 2班学生2 2班学生3 2班学生4 2班学生5 2班学生6 2班学生7 2班学生8 2班学生9 2班学生10 2班学生11 2班学生12 2班学生13 2班学生14 2班学生15 2班学生16 2班学生17 2班学生18 2班学生19 2班学生20 2班学生21 2班学生22 2班学生23 2班学生24 2班学生25 2班学生26 2班学生27 2班学生28 2班学生29 2班学生30 2班学生31 2班学生32 2班学生33 2班学生34 2班学生35 2班学生36 2班学生37 2班学生38 2班学生39 2班学生40 2班学生41 2班学生42 2班学生43 2班学生44 2班学生45 2班老师1 2班老师2 2班老师3 2班老师4 2班老师5 )
class2_userID=(556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 )
#创建class1组,组ID是501
sudo dscl . create /Groups/class1 PrimaryGroupID 501
sudo dscl . create /Groups/class1 RealName 1班
echo "will add the Students of class 1 information!"
for ((i=0; i<${#class1[@]}; i++))
do
sudo dscl . -create /Users/${class1[i]}
sudo dscl . -create /Users/${class1[i]} UniqueID ${class1_userID[i]}
sudo dscl . -create /Users/${class1[i]} RealName ${class1_realName[i]}
sudo dscl . -create /Users/${class1[i]} PrimaryGroupID 501
sudo dscl . -create /Users/${class1[i]} UserShell /bin/bash
sudo dscl . -create /Users/${class1[i]} NFSHomeDirectory /Users/${class1[i]}
#sudo dscl . -create /Users/${class1[i]} UserShell /bin/bash
sudo dscl . -passwd /Users/${class1[i]} 123456
sudo dscl . -append /Groups/class1 GroupMembership ${class1[i]}
echo "The Students of class 1 ${class1[i]} added complete"
done
#创建class2班,组ID是502
sudo dscl . create /Groups/class2 PrimaryGroupID 502
sudo dscl . create /Groups/class2 RealName 2班
echo "will add the Students of class 2 information!"
for ((i=0; i<${#class2[@]}; i++))
do
sudo dscl . -create /Users/${class2[i]}
sudo dscl . -create /Users/${class2[i]} UniqueID ${class2_userID[i]}
sudo dscl . -create /Users/${class2[i]} RealName ${class2_realName[i]}
sudo dscl . -create /Users/${class2[i]} PrimaryGroupID 502
sudo dscl . -create /Users/${class2[i]} UserShell /bin/bash
sudo dscl . -create /Users/${class2[i]} NFSHomeDirectory /Users/${class2[i]}
#sudo dscl . -create /Users/${class2[i]} UserShell /bin/bash
sudo dscl . -passwd /Users/${class2[i]} 123456
sudo dscl . -append /Groups/class2 GroupMembership ${class2[i]}
echo "The Students of class 2 ${class2[i]} added complete"
done
检查进程状态
bash
#!/bin/bash
#检查mysql进程状态
if [ "$(ps aux|grep mysql |grep -v grep |grep -v sudo | awk '{print $2}')" != "" ];then
echo -e " \033[35m mysql is running! \033[0m"
else
echo -e " \033[35m mysql is not running,now will autostart it! \033[0m"
/Applications/XAMPP/xamppfiles/xampp startmysql
#sleep 10
fi
#检查apache进程状态及admin、teacher、student这三个网页的状态码,如果是200就正常。
url="http://localhost:8091/admin/ http://localhost:8091/teacher/ http://localhost:8091/student/"
for i in $url
do
http_code=$(curl -o /dev/null -s -m 30 --connect-timeout 30 -w %{http_code} $i)
if [[ "$http_code" -eq "200" && "$(ps aux|grep httpd |grep -v grep | awk '{print $2}')" != "" ]];then
#set echo color to green
echo -e " \033[36m $i\tstatus:$http_code---->apache is running! \033[0m"
else
echo -e " \033[31m apache is not running,now will autostart it! \033[0m"
/Applications/XAMPP/xamppfiles/xampp startapache
#sleep 15
echo -e " \033[36m http://localhost:8091/admin `curl -o /dev/null -s -m 30 --connect-timeout 30 -w %{http_code} http://localhost:8091/admin/` \033[0m"
echo -e " \033[32m http://localhost:8091/teacher `curl -o /dev/null -s -m 30 --connect-timeout 30 -w %{http_code} http://localhost:8091/teacher/` \033[0m"
echo -e " \033[36m http://localhost:8091/student `curl -o /dev/null -s -m 30 --connect-timeout 30 -w %{http_code} http://localhost:8091/student/` \033[0m"
echo -e " \033[31m If the above three url output httpd_code not 200, please check your apache configuration...... \033[0m"
break
fi
done
#检查ftp进程
if [ "$(ps aux|grep ftp |grep -v grep | awk '{print $2}')" != "" ];then
echo -e " \033[32m \033[05m ftp is running! \033[0m"
else
echo -e " \033[31m ftp is not running,now will autostart it! \033[0m"
/Applications/XAMPP/xamppfiles/xampp startftp
#sleep 10
fi
#检查activemq进程
if [ "$(ps aux|grep activemq | grep -v grep | awk '{print $2}')" != "" ];then
echo -e " \033[33m activemq is running! \033[0m"
else
echo -e " \033[32m activemq is not running,now,will autostart it! \033[0m"
/Applications/XAMPP/htdocs/Liangyu_edu/apache-activemq/bin/macosx/activemq start
#sleep 10
fi
owncloud赋权脚本
bash
#!/bin/bash
ocpath='/Applications/XAMPP/htdocs/owncloud'
htuser='daemon'
htgroup='daemon'
rootuser='root'
printf "Creating possible missing Directories\n"
mkdir -p $ocpath/data
mkdir -p $ocpath/assets
mkdir -p $ocpath/updater
printf "chmod Files and Directories\n"
find ${ocpath}/ -type f -print0 | xargs -0 chmod 0640
find ${ocpath}/ -type d -print0 | xargs -0 chmod 0750
printf "chown Directories\n"
chown -R ${rootuser}:${htgroup} ${ocpath}/
chown -R ${htuser}:${htgroup} ${ocpath}/apps/
chown -R ${htuser}:${htgroup} ${ocpath}/assets/
chown -R ${htuser}:${htgroup} ${ocpath}/config/
chown -R ${htuser}:${htgroup} ${ocpath}/data/
chown -R ${htuser}:${htgroup} ${ocpath}/themes/
chown -R ${htuser}:${htgroup} ${ocpath}/updater/
chmod +x ${ocpath}/occ
printf "chmod/chown .htaccess\n"
if [ -f ${ocpath}/.htaccess ]
then
chmod 0644 ${ocpath}/.htaccess
chown ${rootuser}:${htgroup} ${ocpath}/.htaccess
fi
if [ -f ${ocpath}/data/.htaccess ]
then
chmod 0644 ${ocpath}/data/.htaccess
chown ${rootuser}:${htgroup} ${ocpath}/data/.htaccess
fi
批量添加用户
批量添加20个用户,用户名为user01-20,密码为 user 后面跟5个随机字符
bash
#!/bin/bash
#description: useradd
for i in `seq -f"%02g" 1 20`;do
useradd user$i
echo "user$i:`echo $RANDOM|md5sum|cut -c 1-5`"|passwd –stdinuser$i >/dev/null 2>&1
done
- 判断192.168.1.0/24网络里,当前在线的IP有哪些
bash
#!/bin/bash
for ip in `seq 1 255`
do
{
ping -c 1 192.168.1.$ip > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo 192.168.1.$ip UP
else
echo 192.168.1.$ip DOWN
fi
}&
done
wait
检查脚本错误
判断一个指定的脚本是否是语法错误,如果有错误,则提醒用户键入Q或者q无视错误并退出其它任何键可以通过vim打开这个指定的脚本
bash
#!/bin/bash
read -p "please input check script-> " file
if [ -f $file ]; then
sh -n $file > /dev/null 2>&1
if [ $? -ne 0 ]; then
read -p "You input $file syntax error,[Type q to exit or Type vim to edit]" answer
case $answer in
q | Q)
exit 0
;;
vim )
vim $file
;;
*)
exit 0
;;
esac
fi
else
echo "$file not exist"
exit 1
fi
清理日志
bash
#!/bin/bash
# Created by e, generated at 2017-01-03 16:20:37 +0800
# created olddir folder if necessary, then run logroate
# will save rotated log.gz into olddir
START_TIME=$(date +%s)
#-------------------------------------------------------------------------------------
SCRIPT_DIR=''; pushd "$(dirname "$(readlink -f "$BASH_SOURCE")")" > /dev/null && { SCRIPT_DIR=$PWD; popd > /dev/null; }
FIND_PROG="/bin/find"
CHOWN_PROG="/bin/chown"
LOGROTATE_PROG="/usr/sbin/logrotate"
XARGS_PROG="/usr/bin/xargs"
YES_PROG="/usr/bin/yes"
LOG_FOLDER="/Data/logs""/"
KARLTEST_LOGROTATE_HISTORY='/Data/logs/karltest_log_rotate_history.log'
TEMP_DIR="archived"
QCLOUD_PATTERN="10.*"
AWS_PATTERN="172.*"
DIR_OWNER="karltestlog"
run () {
local qcloud_res=$( ${FIND_PROG} ${LOG_FOLDER} -type d -name ${QCLOUD_PATTERN})
local aws_res=$( ${FIND_PROG} ${LOG_FOLDER} -type d -name ${AWS_PATTERN})
local process_cmd=$1
${YES_PROG} | clean_dummy_file ${LOG_FOLDER}
for fpath in ${qcloud_res}; do
${process_cmd} ${fpath}/${TEMP_DIR}
done
for fpath in ${aws_res}; do
${process_cmd} ${fpath}/${TEMP_DIR}
done
}
confirm () {
# you can use "/usr/bin/yes | confirm "
read -p "$1 (y/N)?:" yes
if [ "$yes" == "y" ]; then
echo " "
return 0
else
echo "$(date) Cancelled by user. Nothing to do."
return 1
fi
}
create_folder () {
local fpath=$1
if [ ! -d ${fpath} ]; then
echo "$(date) creating folder ${fpath} ..."
mkdir ${fpath}
fi
if [ $? -eq 0 ]; then
chown ${DIR_OWNER}:${DIR_OWNER} -R ${fpath}
fi
}
remove_folder () {
local fpath=$1
if [ -d ${fpath} ]; then
echo "$(date) Try to delete folder ${fpath}, current directory contents:"
/bin/ls ${fpath} -l --color=auto
confirm "Are you sure to delete"
[ $? -eq 0 ] && rm -rf ${fpath}
fi
}
clean_dummy_file () {
local check_path=$1'/'
#Sample: find /tmp -size 0 -print0 |xargs -0 rm
echo "$(date) Try to clean dummy files in folder: ${check_path}"
echo "$(date) Dummy files: "
local log_pattern="\( -name '*.log' -o -name '*.gz' \)"
local cmd_show="${FIND_PROG} ${check_path} -type f -size 0 ${log_pattern}"
local cmd_rm="${FIND_PROG} ${check_path} -type f -size 0 ${log_pattern} -print0 | ${XARGS_PROG} -0r rm"
local dummy_files=$( eval ${cmd_show} )
local total_count=$( echo ${dummy_files} | wc -w)
confirm "Are you sure to delete these empty/dummy files, total files: ${total_count}"
if [ $? -eq 0 ]; then
echo "$(date) Deleting ${total_count} empty/dummy files..." >> ${KARLTEST_LOGROTATE_HISTORY} 2>&1
echo ${dummy_files} >> ${KARLTEST_LOGROTATE_HISTORY} 2>&1
eval ${cmd_rm}
fi
}
logrotate_run () {
if [ ! -f $1 ]; then
echo "$(date) Invalid logrotate config, skip rotating!"
return 1
else
echo "$(date) Try to run ${LOGROTATE_PROG}..."
${LOGROTATE_PROG} $1 $2 >> ${KARLTEST_LOGROTATE_HISTORY} 2>&1
return $?
fi
}
moveold_one () {
# 默认检查比当天日期小的,同时最近1天没有修改过的文件,文件名是*.log和*.gz
# find $1 -maxdepth 1 \( -name '*.log' -o -name '*.gz' \) -type f ! -path '*20161209*' -mtime +0
# args:
# check_path: 全路径
# temp_path: 全路径
local check_path=$1
local temp_path=$2
#local today='20161217'
local today=$(date +%Y%m%d)
local cmd="${FIND_PROG} ${check_path} -maxdepth 1 \( -name '*.log' -o -name '*.gz' \) -type f ! -path '*${today}*' -mtime +0 "
local oldfiles=$( eval ${cmd} )
if [[ -z "${oldfiles// }" ]]; then
echo "$(date) Found 0 matched files under ${check_path}, nothing to do."
return 0
else
local total_count=$( echo ${oldfiles} | wc -w )
echo "$(date) Try to move ${total_count} old files to temp folder: ${temp_path}"
echo ${oldfiles} | xargs -n1 /bin/ls -lh --color=auto
confirm "$(date) Are you sure to move these ${total_count} files?"
if [ $? -eq 0 ]; then
for ofile in ${oldfiles}; do
echo "$(date) moving file: ${ofile} to ${temp_path}..."
mv -f ${ofile} ${temp_path}
done
fi
fi
}
moveold () {
# 移动比某一天日期小的所有文件到temp文件夹下。
# 移动log文件:判断文件名里时间tag,如20161215,把比今天20161217小两天的全部移走
# 方法1: 用ls *.log | grep -v ".*20161217.*" 来找出对应文件
# 方法2: 用 find . -name "*.log" -mtime 2 来找出最近修改的,然后排除掉这些
# 第一种方法:可以控制文件名匹配格式,因为如果某个log文件被vim 修改过,就不满足第二种的条件。
# 第二种方法更加保险,避免文件正在被写入的时候移走。
# 所以需要结合两种判断:先找出文件名不是今天日期的(用find找,可以得到全路径),
# 然后再在这些文件里过滤出最近1天没有修改的。
local qcloud_res=$( ${FIND_PROG} ${LOG_FOLDER} -type d -name ${QCLOUD_PATTERN})
local aws_res=$( ${FIND_PROG} ${LOG_FOLDER} -type d -name ${AWS_PATTERN})
for fpath in ${qcloud_res}; do
# 先保证temp folder存在,create_folder
create_folder ${fpath}/${TEMP_DIR}
${YES_PROG} | moveold_one ${fpath} ${fpath}/${TEMP_DIR}
done
for fpath in ${aws_res}; do
create_folder ${fpath}/${TEMP_DIR}
${YES_PROG} | moveold_one ${fpath} ${fpath}/${TEMP_DIR}
done
}
# $1 - choice
# $2 - logroate conf file
# $3 - logroate debug mode
case "$1" in
create)
run 'create_folder'
logrotate_run $2 $3
moveold
;;
moveold)
$1
;;
delete)
run 'remove_folder'
;;
clean)
clean_dummy_file $2
;;
*)
echo "$(date) Usage: $0 {create|moveold|delete|clean} [logrotate conf file] [-d]" >&2
exit 2
;;
esac
END_TIME=$(date +%s)
RUN_TIME=$(( $END_TIME - $START_TIME ))
echo "$(date) Total run time" ${RUN_TIME} "s"
exit 0
.bashrc自动添加sshkey脚本
bash
findAgent=`find /tmp/ -type s -name agent.\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*'`
if [ $? -ne 0 ];then
eval `ssh-agent`
#ln -sf "$SSH_AUTH_SOCK" $findAgent
ssh_keys=`find $HOME/.ssh/ -name "id_rsa*" |grep -v pub`
ssh_agent_keys=$(ssh-add -l | awk '{key=NF-1; print $key}')
ssh_keys_count=`find $HOME/.ssh/ -name "id_rsa*" |grep -v pub |wc -l`
ssh_agent_keys_count=$(ssh-add -l | awk '{key=NF-1; print $key}' |wc -l)
for k in "${ssh_keys}"; do
for l in "${ssh_agent_keys}"; do
if [ $ssh_keys_count != $ssh_agent_keys_count ];then
if [ "$k" != "$l" ]; then
ssh-add $k > /dev/null 2>&1
fi
fi
done
done
else
export SSH_AUTH_SOCK=$findAgent
ssh_keys=`find $HOME/.ssh/ -name "id_rsa*" |grep -v pub`
ssh_agent_keys=$(ssh-add -l | awk '{key=NF-1; print $key}')
ssh_keys_count=`find $HOME/.ssh/ -name "id_rsa*" |grep -v pub |wc -l`
ssh_agent_keys_count=$(ssh-add -l | awk '{key=NF-1; print $key}' |wc -l)
for k in "${ssh_keys}"; do
for l in "${ssh_agent_keys}"; do
if [ $ssh_keys_count != $ssh_agent_keys_count ];then
if [ "$k" != "$l" ]; then
ssh-add $k > /dev/null 2>&1
fi
fi
done
done
fi
同步阿里云yum镜像脚本
bash
#!/bin/bash
datetime=`date +"%Y-%m-%d"`
echo 开始同步centos6
reposync -n --repoid=base6 -p /home/yum/centos/6/os/x86_64/Packages || echo "ERROR: $datetime centos6Base update failed" >>/var/log/centosrepo.log
reposync -n --repoid=updates6 -p /home/yum/centos/6/updates/x86_64/Packages || echo "ERROR: $datetime centos6Updates update failed" >>/var/log/centosrepo.log
reposync -n --repoid=extras6 -p /home/yum/centos/6/extras/x86_64/Packages || echo "ERROR: $datetime centos6Extras update failed" >>/var/log/centosrepo.log
reposync -n --repoid=epel6 -p /home/yum/epel/6/x86_64/Packages || echo "ERROR: $datetime epel6 update failed" >>/var/log/centosrepo.log
echo 开始同步centos7
reposync -n --repoid=base7 -p /home/yum/centos/7/os/x86_64/Packages || echo "ERROR: $datetime centos7Base update failed" >>/var/log/centosrepo.log
reposync -n --repoid=updates7 -p /home/yum/centos/7/updates/x86_64/Packages || echo "ERROR: $datetime centos7Updates update failed" >>/var/log/centosrepo.log
reposync -n --repoid=extras7 -p /home/yum/centos/7/extras/x86_64/Packages || echo "ERROR: $datetime centos7Extras update failed" >>/var/log/centosrepo.log
reposync -n --repoid=epel7 -p /home/yum/epel/7/x86_64/Packages || echo "ERROR: $datetime epel7 update failed" >>/var/log/centosrepo.log
status=`cat /var/log/centosrepo.log |grep ERROR| wc -l`
if [ "$status" -eq 0 ];then
createrepo --update /home/yum/centos/6/os/x86_64/
createrepo --update /home/yum/centos/6/updates/x86_64/
createrepo --update /home/yum/centos/6/extras/x86_64/
createrepo --update /home/yum/epel/6/x86_64/
createrepo --update /home/yum/centos/7/os/x86_64/
createrepo --update /home/yum/centos/7/updates/x86_64/
createrepo --update /home/yum/centos/7/extras/x86_64/
createrepo --update /home/yum/epel/7/x86_64/
else
echo "somthing was wrong,please check the /var/log/centosrepo.log file :)"
nginx日志切割脚本
bash
#!/bin/bash
# Rotate the nginx logs
LOGSBAK_PATH=/usr/local/nginx/logs/BAK
CUR_LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${CUR_LOGS_PATH}/nginx_access.log ${LOGSBAK_PATH}/nginx_access_${YESTERDAY}.log
kill -USER1 $(cat /usr/local/nginx/logs/nginx.pid)
vnserver启动脚本
bash
#!/bin/sh
### BEGIN INIT INFO
# Provides: vncserver
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop vncserver
### END INIT INFO
# More details see:
# http://www.penguintutor.com/linux/vnc
### Customize this entry
# Set the USER variable to the name of the user to start vncserver under
export USER='pi'
### End customization required
eval cd ~$USER
case "$1" in
start)
# 启动命令行。此处自定义分辨率、控制台号码或其它参数。
su $USER -c '/usr/bin/vncserver -depth 16 -geometry 1024x768 :1'
echo "Starting VNC server for $USER "
;;
stop)
# 终止命令行。此处控制台号码与启动一致。
su $USER -c '/usr/bin/vncserver -kill :1'
echo "vncserver stopped"
;;
*)
echo "Usage: /etc/init.d/vncserver {start|stop}"
exit 1
;;
esac
exit 0
自动修改html并自动发送邮件
bash
#!/bin/bash
# description: You Can Use This Scrpt To Modify Logo 、Copyright Infomation !
# Author: Echoxu
# Datetime: 2019-08-28 21:16:00
#install tar zip wget mailx
fileName=`basename $(pwd)`
destFile=$(pwd)/report.html
function delPDF ()
{
rm -rf report.pdf
echo -e " \033[31m \treport.pdf deleted ! \033[0m"
}
function getLogoFile ()
{
cd web
wget https://cache.echoxu.cn/blogimg/images.tar.gz -O - | tar xfz -
echo -e " \033[35m \tThe file has been downloaded to $fileName/web ! \033[0m"
}
function modifyInfo ()
{
cd -
wget -c https://cache.echoxu.cn/blogimg/modifyAfter.txt
sed -i '36,46d' $destFile
tac modifyAfter.txt | while read line
do
echo "$line" | sed -i "35i\\$line" $destFile
done
echo -e " \033[36m \tModified logo 、copyright and other information ! \033[0m"
}
function compressFile ()
{
read -p "Please enter the name of the file to be renamed :" RFName
tar -zcf $RFName.tar.gz --exclude="modifyInfo.sh" --exclude="modifyAfter.txt" --exclude="$RFName.tar.gz" .
zip -qr $RFName.zip . -x "modifyInfo.sh" -x "modifyAfter.txt" -x "$RFName.tar.gz"
echo -e " \033[36m \tModified logo 、copyright and other information ! \033[0m"
}
function getFileLink ()
{
# use qiniu cdn and you should install qshell
echo "----------------------------------"
echo ""
echo "开始上传文件到七牛!"
echo ""
echo "----------------------------------"
cp *.zip /home/echoxu/software/vuepress/docs/.vuepress/public/images
cp *.tar.gz /home/echoxu/software/vuepress/docs/.vuepress/public/images
find /home/echoxu/software/vuepress/docs/.vuepress/public/images -type f -cmin 120 | xargs -i mv {} /home/echoxu/software/docV1imgbak/
qshell qupload2 --thread-count=10 --rescan-local --src-dir=/home/echoxu/software/vuepress/docs/.vuepress/public/images --bucket=blog --success-list=/home/echoxu/software/docV1imgbak/success.txt --failure-list=/home/echoxu/software/docV1imgbak/failure.txt --overwrite-list=/home/echoxu/software/docV1imgbak/overwrite.txt --up-host="http://upload.qiniu.com"
echo "----------------------------------"
echo ""
echo "成功上传文件到七牛!"
echo ""
echo "----------------------------------"
dir=/home/echoxu/software/vuepress/docs/.vuepress/public/images
rm -rf /home/echoxu/software/vuepress/getmdlinkBefore.txt 2> /dev/null
for filename in ${dir}/*;do
echo $filename >> /home/echoxu/software/vuepress/getmdlinkBefore.txt
done
rm -rf /home/echoxu/software/vuepress/getmdlinkAfter.txt 2> /dev/null
cat /home/echoxu/software/vuepress/getmdlinkBefore.txt | sed 's/\/home\/echoxu\/software\/vuepress\/docs\/\.vuepress\/public\/images/https:\/\/cache.echoxu.cn/blogimg/g' >> /home/echoxu/software/vuepress/getmdlinkAfter.txt
#echo "文件链接地址已保存到getmdlinkAfter.txt,请打开getmdlinkAfter.txt并复制里面的内容"
echo -e " \033[36m \tupload file successed ! \033[0m"
}
function mailTo ()
{
# 邮件配置参考: https://www.iteblog.com/archives/2027.html?from=like
# https://www.cnblogs.com/imweihao/p/7250500.html
# mail -s "主题" 收件地址 -a 附件 < 文件(邮件正文.txt)
read -p "Please enter the title of mail:" MailTitle
read -p "Please enter the destAddress of mail:" destMail
#echo "生物分析" | mail -s "$MailTitle" $destMail -a ./*.zip
mail -s "$MailTitle" $destMail < /home/echoxu/software/vuepress/getmdlinkAfter.txt
echo -e " \033[36m \tsend mail success ed ! \033[0m"
}
function delFile ()
{
echo ""
}
main () {
delPDF
getLogoFile
modifyInfo
compressFile
getFileLink
mailTo
}
main
将以字母开头的文件批量改为以数字命名的文件名
bash
#!/bin/bash
#生成测试数据用touch {a..z} {1..10}
dir=/home/echoxu/test
cd $dir
startCount=`ls |grep '^[0-9]'|sort -rn|head -1` #获取现有最大数字开头的文件名
let startCount+=1 #以后数字命名的文件的开始下标
for i in `ls |grep '^[a-z]'`;
do
mv $i $startCount
let startCount+=1
done
批量修改文件名
bash
#!/bin/bash
#author: echoxu
#date: 2019-10-30 20:03:06
dir=/vhost
cd $dir
function sedMP4(){
for filename in *.MP4
do mv $filename `echo $filename |sed -e s/\.MP4/\.mp4/g`
# rename MP4 mp4 *
done
}
function modifyName(){
startCount=`ls |grep '^[0-9]'|sort -rn|head -1|awk -F "." '{print $1}'` #获取现有最大数字开头的文件名
let startCount+=1 #以后数字命名的文件的开始下标
for i in `ls |grep '^[a-z]'|egrep -v "index.html|modifyName.sh"`
do
mv $i ${startCount}.mp4
let startCount+=1
done
}
function generateHTML(){
cat > index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Video List</title>
</head>
<body>
EOF
for filename in `ls *.mp4 | sort -n`
do
echo -e "<p><a href=\"http://192.168.2.103:81/$filename\" target=\"_blank\">$filename</a></p>" >> /vhost/index.html
done
echo '</body>' >> /vhost/index.html
echo '</html>' >> /vhost/index.html
}
case "$1" in
MP42mp4)
sedMP4
;;
mdName)
modifyName
;;
geHTML)
generateHTML
;;
*)
echo "Usage: $0 {MP42mp4|mdName|geHTML}"
exit 2
;;
esac
批量修改文件1
bash
#!/bin/bash
#author: echoxu
#date: 2019-10-30 20:03:06
function checkDir() {
if [ $# != 1 ];then
echo "请输入 mp4 文件所在的目录路径"
exit 1;
fi
}
function modifyName(){
startCount=1
#startCount=`ls |grep '^[0-9]'|sort -rn|head -1|awk -F "." '{print $1}'` #获取现有最大数字开头的文件名
let startCount+=1 #以后数字命名的文件的开始下标
for i in $1/*
do
mv $i ${startCount}.mp4
let startCount+=1
done
}
function main() {
checkDir
modifyName
}
main
ffmpeg 合并视频
bash
#!/bin/bash
if [ $# != 1 ];then
echo "请输入ts文件所在的目录路径"
exit 1;
fi
for i in $1/*
do
tsName=`basename $i`
echo "file $tsName" >> $1/ffmpeg.txt
done
UUID=$(cat /proc/sys/kernel/random/uuid)
ffmpeg -loglevel quiet -f concat -safe 0 -i $1/ffmpeg.txt -vcodec copy -acodec copy $1/index_$UUID.mp4
windows中批量上传图片到七牛
bash
#!/bin/bash
dir=D:\\ProgramFiles\\vuepress\\docs\\.vuepress\\public\\images
# 如果图片暂存区为空时不执行上传操作
if [ $(ls -A ${dir} | wc -l) != 0 ]; then
echo "----------------------------------"
echo ""
echo "开始上传图片到七牛!"
echo ""
echo "----------------------------------"
qshell qupload2 --thread-count=10 --rescan-local --src-dir="D:\\ProgramFiles\\vuepress\\docs\\.vuepress\\public\\images" --bucket=blog --success-list=D:\\ProgramFiles\\vuepress\\docV1imgbak\\success.txt --failure-list=D:\\ProgramFiles\\vuepress\\docV1imgbak\\failure.txt --overwrite-list=D:\\ProgramFiles\\vuepress\\docV1imgbak\\overwrite.txt --up-host="http://upload.qiniu.com"
else
echo "图片暂存区为空,请待添加图片后再执行上传操作"
exit 1
fi
# 当上面的操作顺利执行时才会触发替换图片地址等操作
if [ $? -eq 0 ]; then
cat /dev/null >D:\\ProgramFiles\\vuepress\\getmdlinkBefore.txt
# 获取需要上传的图片的本地路径并记录到getmdlinkBefore.txt中,内容格式: "D:\ProgramFiles\vuepress\docs\.vuepress\public\images\test.txt"
for filename in $(ls ${dir}); do
echo ${dir}\\$filename >>D:\\ProgramFiles\\vuepress\\getmdlinkBefore.txt
done
cat /dev/null >D:\\ProgramFiles\\vuepress\\getmdlinkAfter.txt
# 将记录的本地图片路径替换成markdown格式的图片链接,格式为: 
cat D:\\ProgramFiles\\vuepress\\getmdlinkBefore.txt | sed 's/D:\\ProgramFiles\\vuepress\\docs\\.vuepress\\public\\images\\/\!\[wwwechoxu\]\(https:\/\/image.echoxu.cn\//g' | sed 's/$/\)/g' >>D:\\ProgramFiles\\vuepress/getmdlinkAfter.txt
echo "----------------------------------"
echo ""
echo "图片链接地址已保存到getmdlinkAfter.txt,请打开getmdlinkAfter.txt并复制里面的内容到vuepress中使用."
echo ""
echo "----------------------------------"
# 清空存放图片的目录,这样就不会重复上传已经上传过的文件
cd ${dir}
mv * D:\\ProgramFiles\\vuepress\\docV1imgbak\\
echo "----------------------------------"
echo ""
echo "已清空图片暂存目录!"
echo ""
echo "----------------------------------"
echo "----------------------------------"
echo ""
echo "成功上传图片到七牛!"
echo ""
echo "----------------------------------"
else
echo "上传失败"
exit 1
fi