添加脚本

This commit is contained in:
Zhang Peng 2019-05-10 11:17:57 +08:00
parent 78a9ebf663
commit a786dd2b37
166 changed files with 5223 additions and 43 deletions

118
README.md
View File

@ -1,47 +1,8 @@
# Linux 教程
## [Linux 命令](docs/linux/cli)
## :shell: 脚本
> 根据应用场景,将常见 Linux 命令分门别类的一一介绍。
>
> 如果想快速学习,推荐参考这篇文章:[命令行的艺术(转载)](docs/linux/cli/命令行的艺术.md)
1. [查看 Linux 命令帮助信息](docs/linux/cli/01.查看Linux命令帮助信息.md) - 关键词:`help`, `whatis`, `info`, `which`, `whereis`, `man`
2. [Linux 文件目录管理](docs/linux/cli/02.Linux文件目录管理.md) - 关键词:`cd`, `ls`, `pwd`, `mkdir`, `rmdir`, `tree`, `touch`, `ln`, `rename`, `stat`, `file`, `chmod`, `chown`, `locate`, `find`, `cp`, `mv`, `rm`
3. [Linux 文件内容查看命令](docs/linux/cli/03.Linux文件内容查看编辑.md) - 关键词:`cat`, `head`, `tail`, `more`, `less`, `sed`, `vi`, `grep`
4. [Linux 文件压缩和解压](docs/linux/cli/04.Linux文件压缩和解压.md) - 关键词:`tar`, `gzip`, `zip`, `unzip`
5. [Linux 用户管理](docs/linux/cli/05.Linux用户管理.md) - 关键词:`groupadd`, `groupdel`, `groupmod`, `useradd`, `userdel`, `usermod`, `passwd`, `su`, `sudo`
6. [Linux 系统管理](docs/linux/cli/06.Linux系统管理.md) - 关键词:`reboot`, `exit`, `shutdown`, `date`, `mount`, `umount`, `ps`, `kill`, `systemctl`, `service`, `crontab`
7. [Linux 网络管理](docs/linux/cli/07.Linux网络管理.md) - 关键词:关键词:`curl`, `wget`, `telnet`, `ip`, `hostname`, `ifconfig`, `route`, `ssh`, `ssh-keygen`, `firewalld`, `iptables`, `host`, `nslookup`, `nc`/`netcat`, `ping`, `traceroute`, `netstat`
8. [Linux 硬件管理](docs/linux/cli/08.Linux硬件管理.md) - 关键词:`df`, `du`, `top`, `free`, `iotop`
9. [Linux 软件管理](docs/linux/cli/09.Linux软件管理.md) - 关键词:`rpm`, `yum`, `apt-get`
## [Linux 工具](docs/linux/tool)
- [Git](docs/linux/tool/git)
- [Vim](docs/linux/tool/vim.md)
## [Linux 脚本](docs/lang)
- [一篇文章让你彻底掌握 shell 语言](docs/lang/shell.md)
- [一篇文章让你彻底掌握 python 语言](docs/lang/python.md)
## [Linux 运维](docs/linux/ops)
### Linux 服务器运维
- [Linux 典型运维应用](docs/linux/ops/linux典型运维应用.md)
- [samba 使用详解](docs/linux/ops/samba使用详解.md)
### 软件安装、配置、调优
> :bulb: **说明**
>
> 这里总结了一些常用开发软件的安装、配置、使用指南。
>
> 此外,提供了一键安装、配置脚本,:point_right: [**软件安装配置脚本**](https://github.com/dunwu/linux-tutorial/tree/master/codes/linux/ops/soft)。
>
> [环境部署工具](https://github.com/dunwu/linux-tutorial/tree/master/codes/deploy) :适合开发、运维人员,在 [CentOS](https://www.centos.org/) 机器上安装常用命令工具或开发软件。
以下内容均含配套执行脚本,方便日常的 linux 运维。
| 软件 | 说明 |
| ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
@ -60,4 +21,77 @@
| [Zookeeper 安装](docs/linux/soft/zookeeper.md) | 分布式系统协调软件 |
| [Nacos 安装](linux/soft/nacos.md) | 微服务发现、管理 |
## [Docker](docs/docker)
## :memo: 知识点
- [Linux 命令教程](docs/linux/cli)
- [Docker 教程](docs/docker)
- [Git 教程](docs/linux/tool/git)
- [Vim](docs/linux/tool/vim.md)
- 脚本语言
- [一篇文章让你彻底掌握 shell 语言](docs/lang/shell.md)
- [一篇文章让你彻底掌握 python 语言](docs/lang/python.md)
- 运维
- [Linux 典型运维应用](docs/linux/ops/linux典型运维应用.md)
- [samba 使用详解](docs/linux/ops/samba使用详解.md)
## :books: 学习资源
### 命令行检索
- [Linux命令大全](http://man.linuxde.net/)
- [linux-command](https://jaywcjlove.gitee.io/linux-command/)
### 社区网站
- [Linux 中国](https://linux.cn/) - 各种资讯、文章、技术
- [实验楼](https://www.shiyanlou.com/) - 免费提供了 Linux 在线环境,不用在自己机子上装系统也可以学习 Linux超方便实用。
- [鸟哥的 linux 私房菜](http://linux.vbird.org/) - 非常适合 Linux 入门初学者看的教程。
- [Linux 公社](http://www.linuxidc.com/) - Linux 相关的新闻、教程、主题、壁纸都有。
- [Linux Today](http://www.linuxde.net) - Linux 新闻资讯发布Linux 职业技术学习!。
### 知识相关
- [Linux 思维导图整理](http://www.jianshu.com/p/59f759207862)
- [Linux 初学者进阶学习资源整理](http://www.jianshu.com/p/fe2a790b41eb)
- [Linux 基础入门(新版)](https://www.shiyanlou.com/courses/1)
- [【译】Linux 概念架构的理解](http://www.jianshu.com/p/c5ae8f061cfe) [En](http://oss.org.cn/ossdocs/linux/kernel/a1/index.html)
- [Linux 守护进程的启动方法](http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html)
- [Linux 编程之内存映射](https://www.shiyanlou.com/questions/2992)
- [Linux 知识点小结](https://blog.huachao.me/2016/1/Linux%E7%9F%A5%E8%AF%86%E7%82%B9%E5%B0%8F%E7%BB%93/)
- [10 大白帽黑客专用的 Linux 操作系统](https://linux.cn/article-6971-1.html)
### 软件工具
- [超赞的 Linux 软件](https://www.gitbook.com/book/alim0x/awesome-linux-software-zh_cn/details) Github 仓库[Zh](https://github.com/alim0x/Awesome-Linux-Software-zh_CN) [En](https://github.com/VoLuong/Awesome-Linux-Software)
- [程序员喜欢的 9 款最佳的 Linux 文件比较工具](http://os.51cto.com/art/201607/513796.htm)
- [提高 Linux 开发效率的 5 个工具](http://www.codeceo.com/article/5-linux-productivity-tools.html)
- [你要了解的 11 款面向 Linux 系统的一流备份实用工具](http://os.51cto.com/art/201603/508027.htm)
- [16 个很有用的在线工具](http://www.simlinux.com/archives/264.html)
- Adobe 软件的最佳替代品 [原文在这里](https://linux.cn/article-8928-1.html)
- [Evince (Adobe Acrobat Reader)](https://wiki.gnome.org/Apps/Evince) 一个“支持多种文档格式的文档查看器”,可以查看 PDF还支持各种漫画书格式
- [Pixlr (Adobe Photoshop)](https://pixlr.com/) 一个强大的图像编辑工具
- [Inkscape (Adobe Illustrator)](https://inkscape.org/zh/) 一个专业的矢量图形编辑器
- [Pinegrow Web Editor (Adobe Dreamweaver)](https://pinegrow.com/) 一个可视化编辑制作 HTML 网站
- [Scribus (Adobe InDesign)](https://www.scribus.net/) 一个开源电子杂志制作软件
- [Webflow (Adobe Muse)](https://webflow.com/) 一款可以帮助用户不用编码就可以快速创建网站的谷歌浏览器插件。
- [Tupi (Adobe Animate)](http://www.maefloresta.com/portal/) 一款可以创建 HTML5 动画的工具。
- [Black Magic Fusion (Adobe After Effects)](https://www.blackmagicdesign.com) 一款先进的合成软件,广泛应用于视觉特效、广电影视设计以及 3D 动画设计等领域。
### 中国开源镜像站点
- 阿里云开源镜像站http://mirrors.aliyun.com/
- 网易开源镜像站http://mirrors.163.com/
- 搜狐开源镜像站http://mirrors.sohu.com/
- 北京交通大学http://mirror.bjtu.edu.cn/ \<教育网荐\>
- 兰州大学http://mirror.lzu.edu.cn/ \<西北高校 FTP 搜索引擎\>
- 厦门大学http://mirrors.xmu.edu.cn/
- 上海交通大学http://ftp.sjtu.edu.cn/
- 清华大学http://mirrors.tuna.tsinghua.edu.cn/
- http://mirrors6.tuna.tsinghua.edu.cn/
- http://mirrors4.tuna.tsinghua.edu.cn/
- 中国科学技术大学http://mirrors.ustc.edu.cn/
- http://ipv4.ustc.edu.cn/ \<教育网、电信\>
- http://ipv6.ustc.edu.cn/ \<IPv6 only\>
- 东北大学http://mirror.neu.edu.cn/
- 浙江大学http://mirrors.zju.edu.cn/
- 东软信息学院http://mirrors.neusoft.edu.cn/

View File

@ -0,0 +1,164 @@
#!/bin/bash
#mail:xuel@anchnet.com
#data:2017/9/7
#AutoInstall ELK scripts
#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1
clear
echo "##########################################"
echo "# Auto Install ELK. ##"
echo "# Press Ctrl + C to cancel ##"
echo "# Any key to continue ##"
echo "##########################################"
read -p
software_dir="/usr/local/software"
elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz"
kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz"
logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz"
filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz"
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`
#wget software
wget_fun() {
if [ ! -d ${software_dir} ];then
mkdir -p ${software_dir} && cd ${software_dir}
else
cd ${software_dir}
fi
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
do
wget -c $software
done
clear
}
#initial system:install java wget;set hostname;disable firewalld
init_sys() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0
yum install -y java-1.8.0-openjdk wget net-tools
hostnamectl set-hostname elk-server
systemctl stop firewalld
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65536
* hard nofile 65536
* soft nGproc 65536
* hard nproc 65536
EOF
}
#install elasticsearch
install_elasticsearch() {
cd $software_dir
tar zxf elasticsearch-5.4.1.tar.gz
mv elasticsearch-5.4.1 /usr/local/elasticsearch
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
useradd elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p
if [ ${sys_mem} -eq 0 ];then
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF
cluster.name: my-application
node.name: elk-server
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-server"]
EOF
su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &"
}
#install logstash
install_logstash() {
cd $software_dir
tar -zxf logstash-5.4.1.tar.gz
mv logstash-5.4.1 /usr/local/logstash
cat>/usr/local/logstash/config/01-syslog.conf<<EOF
input {
beats {
port => "5044"
}
}
output {
elasticsearch {
hosts => "127.0.0.1:9200"
}
stdout { codec => rubydebug }
}
EOF
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null
}
#install filebeat
install_filebeat() {
cd $software_dir
tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz
mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF
filebeat.prospectors:
- input_type: log
paths:
- /var/log/*.log
output.logstash:
hosts: ["127.0.0.1:5044"]
EOF
cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null
}
#install kibana
install_kibana() {
cd $software_dir
tar -zxf kibana-5.4.1-linux-x86_64.tar.gz
mv kibana-5.4.1-linux-x86_64 /usr/local/kibana
cat >> /usr/local/kibana/config/kibana.yml <<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200"
EOF
nohup /usr/local/kibana/bin/kibana & >/dev/null
}
check() {
port=$1
program=$2
check_port=`netstat -lntup|grep ${port}|wc -l`
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l`
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then
action "${program} run is ok!" /bin/true
else
action "${program} run is error!" /bin/false
fi
}
main() {
init_sys
wget_fun
install_elasticsearch
install_filebeat
install_logstash
install_kibana
echo -e "\033[32m Checking Elasticsearch...\033[0m"
sleep 20
check :9200 "elasticsearch"
echo -e "\033[32m Checking Logstash...\033[0m"
sleep 2
check ":9600" "logstash"
echo -e "\033[32m Checking Kibana...\033[0m"
sleep 2
check ":5601" "kibana"
action "ELK install is success!" /bin/true
echo "url:http://$IP:5601"
}
main

View File

@ -0,0 +1,53 @@
#!/bin/bash
# auth:kaliarch
# func:sys info check
# version:v1.0
# sys:centos6.x/7.x
set -e
[ $(id -u) -gt 0 ] && exit 1
# cpu使用超过百分之多少进行限制
PEC_CPU=80
# 限制进程使用百分之多少,如果程序为多线程单个cpu限制为85如果为多核心就需要按照比例写例如cpu为2c像限制多线程占比80%就写170
LIMIT_CPU=85
# 日志
LOG_DIR=/var/log/cpulimit/
# 超过阀值进程pid
PIDARG=$(ps -aux |awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}')
CPULIMITCMD=$(which cpulimit)
install_cpulimit() {
[ ! -d /tmp ] && mkdir /tmp || cd /tmp
wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz
tar -zxf v0.2.tar.gz
cd cpulimit-0.2 && make
[ $? -eq 0 ] && cp src/cpulimit /usr/bin/
}
do_cpulimit() {
[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR}
for i in ${PIDARG};
do
MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}')
echo ${MSG}
[ ! -d /tmp ] && mkdir /tmp || cd /tmp
nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} &
echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log
done
}
main() {
hash cpulimit
if [ $? -eq 0 ];then
do_cpulimit
else
install_cpulimit && do_cpulimit
fi
}
main

View File

@ -0,0 +1,26 @@
#!/bin/bash
# function:自定义rm命令每天晚上定时清理
CMD_SCRIPTS=$HOME/.rm_scripts.sh
TRASH_DIR=$HOME/.TRASH_DIR
CRON_FILE=/var/spool/cron/root
BASHRC=$HOME/.bashrc
[ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR}
cat > $CMD_SCRIPTS <<EOF
PARA_CNT=\$#
TRASH_DIR=$TRASH_DIR
for i in \$*; do
DATE=\$(date +%F%T)
fileName=\$(basename \$i)
mv \$i \$TRASH_DIR/\$fileName.\$DATE
done
EOF
sed -i "s@$(grep 'alias rm=' $BASHRC)@alias rm='bash ${CMD_SCRIPTS}'@g" $BASHRC
source $HOME/.bashrc
echo "0 0 * * * rm -rf $TRASH_DIR/*" >> $CRON_FILE
echo "删除目录:$TRASH_DIR"
echo "删除脚本:$CMD_SCRIPTS"
echo "请执行:source $BASHRC 来加载文件或退出当前shell重新登录"

View File

@ -0,0 +1,66 @@
#!/bin/bash
#
# Daily_Archive - Archive designated files & directories
######################################################
#
# Gather Current Date
#
DATE=`date +%y%m%d`
#
# Set Archive File Name
#
FILE=archive$DATE.tar.gz
#
# Set Configuration and Destination File
#
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
DESTINATION=/home/tiandi/archive/$FILE
#
##################### Main Script ###############
#
# Check Backup Config file exists
#
if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then
echo
else
echo
echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file"
echo
exit
fi
#
# Build the names of all the files to backup
#
FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File
#
read FILE_NAME # Read 1st record
#
while [ $? -eq 0 ]
do
# Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ]
then
# If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME"
else
# If file doesn't exist, issue warning
echo
echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file."
echo
fi
#
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record.
done
###########################################################
#
# Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#

View File

@ -0,0 +1,73 @@
#!/bin/bash
#
# Hourly_Archive - Every hour create an archive
######################################################
#
# Set Configuration and Destination File
#
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
#
# Gather Current Date,Month & Time
#
DAY=`date +%d`
MONTH=`date +%m`
TIME=`date +%k%M`
#
# Set Base Archive Destination Location
#
BASEDEST=/home/tiandi/archive/hourly
#
# Create Archive Destination Directory
mkdir -p $BASEDEST/$MONTH/$DAY
#
# Build Archive Destination File Name
DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz
#
##################### Main Script ###############
#
# Check Backup Config file exists
#
if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then
echo
else
echo
echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file"
echo
exit
fi
#
# Build the names of all the files to backup
#
FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File
#
read FILE_NAME # Read 1st record
#
while [ $? -eq 0 ]
do
# Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ]
then
# If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME"
else
# If file doesn't exist, issue warning
echo
echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file."
echo
fi
#
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record.
done
###########################################################
#
# Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#

View File

@ -0,0 +1,225 @@
#!/bin/bash
# auth:kaliarch
# func:sys info check
# version:v1.0
[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本" && exit 1
sysversion=$(rpm -q centos-release|cut -d- -f3)
line="-------------------------------------------------"
[ -d logs ] || mkdir logs
sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-`date +%Y%m%d`.txt"
# 获取系统cpu信息
function get_cpu_info() {
Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)
Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')
CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
CPU_Arch=$(uname -m)
cat <<EOF
CPU信息:
物理CPU个数:$Physical_CPUs
逻辑CPU个数:$Virt_CPUs
每CPU核心数:$CPU_Kernels
CPU型号:$CPU_Type
CPU架构:$CPU_Arch
EOF
}
# 获取系统内存信息
function get_mem_info() {
check_mem=$(free -m)
MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KB
MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KB
let MemUsed=MemTotal-MemFree
MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
report_MemTotal="$((MemTotal/1024))""MB" #内存总容量(MB)
report_MemFree="$((MemFree/1024))""MB" #内存剩余(MB)
report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" #内存使用率%
cat <<EOF
内存信息:
${check_mem}
EOF
}
# 获取系统网络信息
function get_net_info() {
pri_ipadd=$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')
pub_ipadd=$(curl ifconfig.me -s)
gateway=$(ip route | grep default | awk '{print $3}')
mac_info=$(ip link| egrep -v "lo"|grep link|awk '{print $2}')
dns_config=$(egrep -v "^$|^#" /etc/resolv.conf)
route_info=$(route -n)
cat <<EOF
IP信息:
系统公网地址:${pub_ipadd}
系统私网地址:${pri_ipadd}
网关地址:${gateway}
MAC地址:${mac_info}
路由信息:
${route_info}
DNS 信息:
${dns_config}
EOF
}
# 获取系统磁盘信息
function get_disk_info() {
disk_info=$(fdisk -l|grep "Disk /dev"|cut -d, -f1)
disk_use=$(df -hTP|awk '$2!="tmpfs"{print}')
disk_inode=$(df -hiP|awk '$1!="tmpfs"{print}')
cat <<EOF
磁盘信息:
${disk_info}
磁盘使用:
${disk_use}
inode信息:
${disk_inode}
EOF
}
# 获取系统信息
function get_systatus_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/redhat-release)
sys_kernel=$(uname -r)
sys_hostname=$(hostname)
sys_selinux=$(getenforce)
sys_lang=$(echo $LANG)
sys_lastreboot=$(who -b | awk '{print $3,$4}')
sys_runtime=$(uptime |awk '{print $3,$4}'|cut -d, -f1)
sys_time=$(date)
sys_load=$(uptime |cut -d: -f5)
cat <<EOF
系统信息:
系统: ${sys_os}
发行版本: ${sys_release}
系统内核: ${sys_kernel}
主机名: ${sys_hostname}
selinux状态: ${sys_selinux}
系统语言: ${sys_lang}
系统当前时间: ${sys_time}
系统最后重启时间: ${sys_lastreboot}
系统运行时间: ${sys_runtime}
系统负载: ${sys_load}
EOF
}
# 获取服务信息
function get_service_info() {
port_listen=$(netstat -lntup|grep -v "Active Internet")
kernel_config=$(sysctl -p 2>/dev/null)
if [ ${sysversion} -gt 6 ];then
service_config=$(systemctl list-unit-files --type=service --state=enabled|grep "enabled")
run_service=$(systemctl list-units --type=service --state=running |grep ".service")
else
service_config=$(/sbin/chkconfig | grep -E ":on|:启用" |column -t)
run_service=$(/sbin/service --status-all|grep -E "running")
fi
cat <<EOF
服务启动配置:
${service_config}
${line}
运行的服务:
${run_service}
${line}
监听端口:
${port_listen}
${line}
内核参考配置:
${kernel_config}
EOF
}
function get_sys_user() {
login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config)
sudo_config=$(egrep -v "^#|^$" /etc/sudoers |grep -v "^Defaults")
host_config=$(egrep -v "^#|^$" /etc/hosts)
crond_config=$(for cronuser in /var/spool/cron/* ;do ls ${cronuser} 2>/dev/null|cut -d/ -f5;egrep -v "^$|^#" ${cronuser} 2>/dev/null;echo "";done)
cat <<EOF
系统登录用户:
${login_user}
${line}
ssh 配置信息:
${ssh_config}
${line}
sudo 配置用户:
${sudo_config}
${line}
定时任务配置:
${crond_config}
${line}
hosts 信息:
${host_config}
EOF
}
function process_top_info() {
top_title=$(top -b n1|head -7|tail -1)
cpu_top10=$(top b -n1 | head -17 | tail -11)
mem_top10=$(top -b n1|head -17|tail -10|sort -k10 -r)
cat <<EOF
CPU占用top10:
${top_title}
${cpu_top10}
内存占用top10:
${top_title}
${mem_top10}
EOF
}
function sys_check() {
get_cpu_info
echo ${line}
get_mem_info
echo ${line}
get_net_info
echo ${line}
get_disk_info
echo ${line}
get_systatus_info
echo ${line}
get_service_info
echo ${line}
get_sys_user
echo ${line}
process_top_info
}
sys_check > ${sys_check_file}

View File

@ -0,0 +1,194 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:elasticsearch 5.4.1/6.0.1/6.3.1安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_elasticsearch.log
env_file=/etc/profile.d/elasticsearch.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}elasticsearch/config/elasticsearch.yml
sysversion=$(rpm -q centos-release|cut -d- -f3)
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`
hostname=elk-server
clear
echo "##########################################"
echo "# #"
echo "# 安装 elasticsearch 5.4.1/6.0.1/6.3.1 #"
echo "# #"
echo "##########################################"
echo "1: Install elasticsearch 5.4.1"
echo "2: Install elasticsearch 6.0.1"
echo "3: Install elasticsearch 6.3.1"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4|5}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 配置主机名,第一个为主机名
config_hostname() {
if [ ${sysversion} -eq 6 ];then
hostname $1
elif [ ${sysversion} -eq 7 ];then
hostnamectl set-hostname $1
else
echo "`date +%F' '%H:%M:%S` hostname $1 config fail">> ${install_log_path}${install_log_name}
fi
}
config_limits() {
output_msg "配置limits"
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
EOF
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf
sysctl -p >/dev/null 2>&1
}
# 添加配置文件
add_config() {
cat > $1 << EOF
cluster.name: my-application
node.name: ${hostname}
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["$hostname"]
EOF
}
config_user() {
useradd $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $1 user add success">> ${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $1 user add fail">> ${install_log_path}${install_log_name} && exit 1
fi
chown ${1}.${1} ${install_path}elasticsearch/ -R
}
config_jvm() {
if [ ${sys_mem} -eq 0 ];then
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
config_hostname $hostname
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}elasticsearch
add_config $software_config_file
check_dir ${install_path}elasticsearch/{data,logs}
config_user elasticsearch
config_env ${install_path}elasticsearch/bin
config_limits
config_jvm
echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'"
}
main

View File

@ -0,0 +1,136 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:filebeat 5.6.1/6.1.3/6.3.2 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_filebeat.log
env_file=/etc/profile.d/filebeat.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}filebeat/filebeat.yml
clear
echo "##########################################"
echo "# #"
echo "# 安装 filebeat 5.6.1/6.1.3/6.3.2 #"
echo "# #"
echo "##########################################"
echo "1: Install filebeat 5.6.1"
echo "2: Install filebeat 6.1.3"
echo "3: Install filebeat 6.3.2"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 添加配置文件
add_config() {
cat> $1 <<EOF
filebeat.prospectors:
- input_type: log
paths:
- /var/log/*.log
output.logstash:
hosts: ["127.0.0.1:5044"]
EOF
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}filebeat
add_config ${software_config_file}
}
main

View File

@ -0,0 +1,155 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:git 2.0.0/2.10.0/2.18.0 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_git.log
env_file=/etc/profile.d/git.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
#software_config_file=${install_path}
clear
echo "##########################################"
echo "# #"
echo "# 安装 git 2.0.0/2.10.0/2.18.0 #"
echo "# #"
echo "##########################################"
echo "1: Install git 2.0.0"
echo "2: Install git 2.10.0"
echo "3: Install git 2.18.0"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/git/git-2.0.0.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/git/git-2.10.0.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/git/git-2.18.0.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# yum 安装软件包,可传入多个软件包,用空格隔开
yum_install_software() {
output_msg "yum 安装软件"
yum -y install $* >/dev/null 2>${install_log_path}${install_log_name}
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S`yum install $* 完成" >>${install_log_path}${install_log_name}
else
exit 1
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 编译安装git,传入$1 为解压后软件包的名称
source_install_git() {
output_msg "编译安装git"
mv ${install_path}${1} ${install_path}tmp${1}
cd ${install_path}tmp${1} && make prefix=${install_path}git all >/dev/null 2>&1
if [ $? -eq 0 ];then
make prefix=${install_path}git install >/dev/null 2>&1 echo "`date +%F' '%H:%M:%S` git source install success ">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` git source install fail!">>${install_log_path}${install_log_name} && exit 1
fi
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
check_yum_command make make
yum_install_software curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
source_install_git ${software_name}
mv /usr/bin/git /usr/bin/git.bak
rm -fr ${download_path}
config_env ${install_path}git/bin
}
main

View File

@ -0,0 +1,110 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:grafana 5.1.0/5.1.5/5.2.2 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_grafana.log
env_file=/etc/profile.d/grafana.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
clear
echo "##########################################"
echo "# #"
echo "# 安装 grafana 5.1.0/5.1.5/5.2.2 #"
echo "# #"
echo "##########################################"
echo "1: Install grafana 5.1.0"
echo "2: Install grafana 5.1.5"
echo "3: Install grafana 5.2.2"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 安装grafana插件,传入安装的插件的名称
install_grafana_plugins() {
output_msg "grafana插件安装"
check_yum_command grafana-cli
grafana-cli plugins install $* >/dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` grafana plugins $* install success!">>${install_log_path}${install_log_name} && exit 1
fi
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
for filename in `ls $download_path`;do
yum -y install $download_path$filename >/dev/null 2>&1
done
install_grafana_plugins alexanderzobnin-zabbix-app
}
main

View File

@ -0,0 +1,139 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:kafka 0.10.2/0.11.0/1.1.0/2.0.0 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_kafka.log
env_file=/etc/profile.d/kafka.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}kafka/config/server.properties
clear
echo "##########################################"
echo "# #"
echo "# 安装kafka 0.10.2/0.11.0/1.1.0/2.0.0 #"
echo "# #"
echo "##########################################"
echo "1: Install kafka 0.10.2"
echo "2: Install kafka 0.11.0"
echo "3: Install kafka 1.1.0"
echo "4: Install kafka 2.0.0"
echo "5: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-0.10.2.2.tgz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-0.11.0.3.tgz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-1.1.0.tgz"
elif [ "${softversion}" == "4" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kafka/kafka_2.12-2.0.0.tgz"
elif [ "${softversion}" == "5" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4|5}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ] || [ "${file##*.}" == "tgz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 添加配置文件
add_config() {
cat> $1 <<EOF
broker.id=1 #kafka集群标识不能相同第一台是1以此类推其他都一样。
log.dirs=/usr/local/kafka/kafka-logs
host.name=127.0.0.1 #主机ip
#zookeeper.connect=127.0.0.1:2181 #zookeeper连接
EOF
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tgz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}kafka
add_config ${software_config_file}
check_dir ${install_path}kafka/kafka-logs
config_env ${install_path}kafka/bin
}
main

View File

@ -0,0 +1,134 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:kibana 6.0.1/6.2.4/6.3.1 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_kibana.log
env_file=/etc/profile.d/kibana.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}kibana/config/kibana.yml
clear
echo "##########################################"
echo "# #"
echo "# 安装 kibana 6.0.1/6.2.4/6.3.1 #"
echo "# #"
echo "##########################################"
echo "1: Install kibana 6.0.1"
echo "2: Install kibana 6.2.4"
echo "3: Install kibana 6.3.1"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.0.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.2.4-linux-x86_64.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.3.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 添加配置文件
add_config() {
cat> $1 <<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200"
EOF
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s ${install_path}$software_name ${install_path}kibana
add_config ${software_config_file}
config_env ${install_path}kibana/bin
}
main

View File

@ -0,0 +1,141 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:logstash 5.4/6.1/6.3 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_logstash.log
env_file=/etc/profile.d/logstash.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}logstash/config/01-syslog.conf
clear
echo "##########################################"
echo "# #"
echo "# 安装 logstash 5.4/6.1/6.3 #"
echo "# #"
echo "##########################################"
echo "1: Install logstash-5.4"
echo "2: Install logstash-6.1"
echo "3: Install logstash-6.3"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-5.4.1.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.1.3.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.3.2.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 添加配置文件
add_config() {
cat> $1 <<EOF
input {
beats {
port => "5044"
}
}
output {
elasticsearch {
hosts => "127.0.0.1:9200"
}
stdout { codec => rubydebug }
}
EOF
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}logstash
add_config ${software_config_file}
config_env ${install_path}logstash/bin
}
main

View File

@ -0,0 +1,125 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:maven 3.0.5/3.3.9/3.5.4 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_maven.log
env_file=/etc/profile.d/maven.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
#software_config_file=${install_path}filebeat/filebeat.yml
clear
echo "##########################################"
echo "# #"
echo "# 安装 maven 3.0.5/3.3.9/3.5.4 #"
echo "# #"
echo "##########################################"
echo "1: Install maven 3.0.5"
echo "2: Install maven 3.2.9"
echo "3: Install maven 3.5.4"
echo "4: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.0.5-bin.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.3.9-bin.tar.gz"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/maven/apache-maven-3.5.4-bin.tar.gz"
elif [ "${softversion}" == "4" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'-bin.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}maven
config_env ${install_path}maven/bin
}
main

View File

@ -0,0 +1,126 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:tomcat 6.0/7.0/8.5/9.0 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_tomcat.log
env_file=/etc/profile.d/tomcat.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
clear
echo "##########################################"
echo "# #"
echo "# 安装 tomcat 6.0/7.0/8.5/9.0 #"
echo "# #"
echo "##########################################"
echo "1: Install tomcat-6.0"
echo "2: Install tomcat-7.0"
echo "3: Install tomcat-8.5"
echo "4: Install tomcat-9.0"
echo "5: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-6.0.9.zip"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-7.0.79.zip"
elif [ "${softversion}" == "3" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-8.5.20.zip"
elif [ "${softversion}" == "4" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/tomcat/apache-tomcat-9.0.0.M26.zip"
elif [ "${softversion}" == "5" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3|4|5}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
check_yum_command unzip unzip
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.zip' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}tomcat
config_env ${install_path}tomcat/bin
}
main

View File

@ -0,0 +1,137 @@
#!/bin/bash
# auth:kaliarch
# version:v1.0
# func:zookeeper 3.4/3.5 安装
# 定义安装目录、及日志信息
. /etc/init.d/functions
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
download_path=/tmp/tmpdir/
install_log_name=install_zookeeper.log
env_file=/etc/profile.d/zookeeper.sh
install_log_path=/var/log/appinstall/
install_path=/usr/local/
software_config_file=${install_path}zookeeper/conf/zoo.cfg
clear
echo "##########################################"
echo "# #"
echo "# 安装 zookeeper 3.4/3.5 #"
echo "# #"
echo "##########################################"
echo "1: Install zookeeper 3.4"
echo "2: Install zookeeper 3.5"
echo "3: EXIT"
# 选择安装软件版本
read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/zookeeper/zookeeper-3.4.12.tar.gz"
elif [ "${softversion}" == "2" ];then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/zookeeper/zookeeper-3.5.4-beta.tar.gz"
elif [ "${softversion}" == "3" ];then
echo "you choce channel!"
exit 1;
else
echo "input Error! Place input{1|2|3}"
exit 0;
fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() {
for msg in $*;do
action $msg /bin/true
done
}
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() {
output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0
else
yum -y install $2 >/dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi
}
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() {
output_msg "目录检查"
for dirname in $*;do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done
}
# 下载文件并解压至安装目录,传入url链接地址
download_file() {
output_msg "下载源码包"
mkdir -p $download_path
for file in $*;do
wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() {
output_msg "解压源码"
for file in $*;do
if [ "${file##*.}" == "gz" ];then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name}
else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1
fi
done
}
# 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() {
output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name}
}
# 添加配置文件
add_config() {
cat> $1 <<EOF
tickTime=2000 #服务之间或者客户端与服务段之间心跳时间
initLimit=10 #Follower启动过程中从Leader同步所有最新数据的时间
syncLimit=5 #Leader与集群之间的通信时间
dataDir=/usr/local/zookeeper/data #zookeeper存储数据
datalogDir=/usr/local/zookeeper/logs #zookeeper存储数据的日志
clientPort=2181 #zookeeper默认端口
#server.1=127.0.0.1:2888:3888 #集群配置
EOF
}
main() {
check_dir $install_log_path $install_path
check_yum_command wget wget
download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do
extract_file ${download_path}$filename
done
rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}zookeeper
add_config ${software_config_file}
check_dir "${install_path}zookeeper/data" "${install_path}zookeeper/logs"
echo "1">${install_path}zookeeper/data/myid
config_env ${install_path}zookeeper/bin
}
main

View File

@ -0,0 +1,246 @@
#!/bin/sh
###################################################################################
# 此脚本用于检测 git 状态
# Copy from https://github.com/fboender/multi-git-status
###################################################################################
# MIT license
if [ -t 1 ]; then
# Our output is not being redirected, so we can use colors.
C_RED="\033[1;31m"
C_GREEN="\033[1;32m"
C_YELLOW="\033[1;33m"
C_BLUE="\033[1;34m"
C_PURPLE="\033[1;35m"
C_CYAN="\033[1;36m"
C_RESET="$(tput sgr0)"
fi
C_OK="$C_GREEN"
C_LOCKED="$C_RED"
C_NEEDS_PUSH="$C_YELLOW"
C_NEEDS_PULL="$C_BLUE"
C_NEEDS_COMMIT="$C_RED"
C_NEEDS_UPSTREAM="$C_PURPLE"
C_UNTRACKED="$C_CYAN"
C_STASHES="$C_YELLOW"
DEBUG=0
usage () {
cat << EOF >&2
Usage: $0 [-w] [-e] [-f] [--no-X] [DIR] [DEPTH=2]
Scan for .git dirs under DIR (up to DEPTH dirs deep) and show git status
-w Warn about dirs that are not Git repositories
-e Exclude repos that are 'ok'
-f Do a 'git fetch' on each repo (slow for many repos)
You can limit output with the following options:
--no-push
--no-pull
--no-upstream
--no-uncommitted
--no-untracked
--no-stashes
EOF
}
# Handle commandline options
WARN_NOT_REPO=0
EXCLUDE_OK=0
DO_FETCH=0
NO_PUSH=0
NO_PULL=0
NO_UPSTREAM=0
NO_UNCOMMITTED=0
NO_UNTRACKED=0
NO_STASHES=0
while [ \! -z "$1" ]; do
# Stop reading when we've run out of options.
[ "$(echo "$1" | cut -c 1)" != "-" ] && break
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 1
fi
if [ "$1" = "-w" ]; then
WARN_NOT_REPO=1
fi
if [ "$1" = "-e" ]; then
EXCLUDE_OK=1
fi
if [ "$1" = "-f" ]; then
DO_FETCH=1
fi
if [ "$1" = "--no-push" ]; then
NO_PUSH=1
fi
if [ "$1" = "--no-pull" ]; then
NO_PULL=1
fi
if [ "$1" = "--no-upstream" ]; then
NO_UPSTREAM=1
fi
if [ "$1" = "--no-uncommitted" ]; then
NO_UNCOMMITTED=1
fi
if [ "$1" = "--no-untracked" ]; then
NO_UNTRACKED=1
fi
if [ "$1" = "--no-stashes" ]; then
NO_STASHES=1
fi
shift
done
if [ -z "$1" ]; then
ROOT_DIR="."
else
ROOT_DIR="$1"
fi
if [ -z "$2" ]; then
DEPTH=2
else
DEPTH="$2"
fi
# Find all .git dirs, up to DEPTH levels deep
find -L "$ROOT_DIR" -maxdepth "$DEPTH" -type d | while read -r PROJ_DIR
do
GIT_DIR="$PROJ_DIR/.git"
# If this dir is not a repo, and WARN_NOT_REPO is 1, tell the user.
if [ \! -d "$GIT_DIR" ]; then
if [ "$WARN_NOT_REPO" -eq 1 ] && [ "$PROJ_DIR" != "." ]; then
printf "${PROJ_DIR}: not a git repo\n"
fi
continue
fi
[ $DEBUG -eq 1 ] && echo "${PROJ_DIR}"
# Check if repo is locked
if [ -f "$GIT_DIR/index.lock" ]; then
printf "${PROJ_DIR}: ${C_LOCKED}Locked. Skipping.${C_RESET}\n"
continue
fi
# Do a 'git fetch' if requested
if [ "$DO_FETCH" -eq 1 ]; then
git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" fetch -q >/dev/null
fi
# Refresh the index, or we might get wrong results.
git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" update-index -q --refresh >/dev/null 2>&1
# Find all remote branches that have been checked out and figure out if
# they need a push or pull. We do this with various tests and put the name
# of the branches in NEEDS_XXXX, seperated by newlines. After we're done,
# we remove duplicates from NEEDS_XXX.
NEEDS_PUSH_BRANCHES=""
NEEDS_PULL_BRANCHES=""
NEEDS_UPSTREAM_BRANCHES=""
for REF_HEAD in $(cd "$GIT_DIR/refs/heads" && find . -type 'f' | sed "s/^\.\///"); do
# Check if this branch is tracking an upstream (local/remote branch)
UPSTREAM=$(git --git-dir "$GIT_DIR" rev-parse --abbrev-ref --symbolic-full-name "$REF_HEAD@{u}" 2>/dev/null)
EXIT_CODE="$?"
if [ "$EXIT_CODE" -eq 0 ]; then
# Branch is tracking a remote branch. Find out how much behind /
# ahead it is of that remote branch.
CNT_AHEAD_BEHIND=$(git --git-dir "$GIT_DIR" rev-list --left-right --count "$REF_HEAD...$UPSTREAM")
CNT_AHEAD=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $1 }')
CNT_BEHIND=$(echo "$CNT_AHEAD_BEHIND" | awk '{ print $2 }')
[ $DEBUG -eq 1 ] && echo "CNT_AHEAD_BEHIND: $CNT_AHEAD_BEHIND"
[ $DEBUG -eq 1 ] && echo "CNT_AHEAD: $CNT_AHEAD"
[ $DEBUG -eq 1 ] && echo "CNT_BEHIND: $CNT_BEHIND"
if [ "$CNT_AHEAD" -gt 0 ]; then
NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD"
fi
if [ "$CNT_BEHIND" -gt 0 ]; then
NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD"
fi
# Check if this branch is a branch off another branch. and if it needs
# to be updated.
REV_LOCAL=$(git --git-dir "$GIT_DIR" rev-parse --verify "$REF_HEAD" 2>/dev/null)
REV_REMOTE=$(git --git-dir "$GIT_DIR" rev-parse --verify "$UPSTREAM" 2>/dev/null)
REV_BASE=$(git --git-dir "$GIT_DIR" merge-base "$REF_HEAD" "$UPSTREAM" 2>/dev/null)
[ $DEBUG -eq 1 ] && echo "REV_LOCAL: $REV_LOCAL"
[ $DEBUG -eq 1 ] && echo "REV_REMOTE: $REV_REMOTE"
[ $DEBUG -eq 1 ] && echo "REV_BASE: $REV_BASE"
if [ "$REV_LOCAL" = "$REV_REMOTE" ]; then
: # NOOP
else
if [ "$REV_LOCAL" = "$REV_BASE" ]; then
NEEDS_PULL_BRANCHES="${NEEDS_PULL_BRANCHES}\n$REF_HEAD"
fi
if [ "$REV_REMOTE" = "$REV_BASE" ]; then
NEEDS_PUSH_BRANCHES="${NEEDS_PUSH_BRANCHES}\n$REF_HEAD"
fi
fi
else
# Branch does not have an upstream (local/remote branch).
NEEDS_UPSTREAM_BRANCHES="${NEEDS_UPSTREAM_BRANCHES}\n$REF_HEAD"
fi
done
# Remove duplicates from NEEDS_XXXX and make comma-seperated
NEEDS_PUSH_BRANCHES=$(printf "$NEEDS_PUSH_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
NEEDS_PULL_BRANCHES=$(printf "$NEEDS_PULL_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
NEEDS_UPSTREAM_BRANCHES=$(printf "$NEEDS_UPSTREAM_BRANCHES" | sort | uniq | tr '\n' ',' | sed "s/^,\(.*\),$/\1/")
# Find out if there are unstaged, uncommitted or untracked changes
UNSTAGED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-index --quiet HEAD -- 2>/dev/null; echo $?)
UNCOMMITTED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" diff-files --quiet --ignore-submodules --; echo $?)
UNTRACKED=$(git --work-tree "$(dirname "$GIT_DIR")" --git-dir "$GIT_DIR" ls-files --exclude-standard --others)
cd "$(dirname "$GIT_DIR")" || exit
STASHES=$(git stash list | wc -l)
cd "$OLDPWD" || exit
# Build up the status string
IS_OK=0 # 0 = Repo needs something, 1 = Repo needs nothing ('ok')
STATUS_NEEDS=""
if [ \! -z "$NEEDS_PUSH_BRANCHES" ] && [ "$NO_PUSH" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PUSH}Needs push ($NEEDS_PUSH_BRANCHES)${C_RESET} "
fi
if [ \! -z "$NEEDS_PULL_BRANCHES" ] && [ "$NO_PULL" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_PULL}Needs pull ($NEEDS_PULL_BRANCHES)${C_RESET} "
fi
if [ \! -z "$NEEDS_UPSTREAM_BRANCHES" ] && [ "$NO_UPSTREAM" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_UPSTREAM}Needs upstream ($NEEDS_UPSTREAM_BRANCHES)${C_RESET} "
fi
if [ "$UNSTAGED" -ne 0 ] || [ "$UNCOMMITTED" -ne 0 ] && [ "$NO_UNCOMMITTED" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_NEEDS_COMMIT}Uncommitted changes${C_RESET} "
fi
if [ "$UNTRACKED" != "" ] && [ "$NO_UNTRACKED" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_UNTRACKED}Untracked files${C_RESET} "
fi
if [ "$STASHES" -ne 0 ] && [ "$NO_STASHES" -eq 0 ]; then
STATUS_NEEDS="${STATUS_NEEDS}${C_STASHES}$STASHES stashes${C_RESET} "
fi
if [ "$STATUS_NEEDS" = "" ]; then
IS_OK=1
STATUS_NEEDS="${STATUS_NEEDS}${C_OK}ok${C_RESET} "
fi
# Print the output, unless repo is 'ok' and -e was specified
if [ "$IS_OK" -ne 1 ] || [ "$EXCLUDE_OK" -ne 1 ]; then
printf "${PROJ_DIR}: $STATUS_NEEDS\n"
fi
done

View File

@ -0,0 +1,247 @@
#!/bin/bash
#
# Delete_User - Automates the 4 steps to remove an account
#
#################################################################
#
# Define Functions
#
#################################################################
function get_answer {
#
unset ANSWER
ASK_COUNT=0
#
while [ -z "$ANSWER" ] # while no answer is given, keep asking
do
ASK_COUNT=$[ $ASK_COUNT + 1 ]
#
case $ASK_COUNT in # If user gives no answer in time allowed
2)
echo
echo "Please answer the question."
echo
;;
3)
echo
echo "One last try... please answer the question."
echo
;;
4)
echo
echo "Since you refuse to answer the question..."
echo "exiting program."
echo
#
exit
;;
esac
#
echo
#
if [ -n "$LINE2" ]
then
echo $LINE1 # Print 2 lines
echo -e $LINE2" \c"
else # Print 1 line
echo -e $LINE1" \c"
fi
#
# Allow 60 seconds to answer before time-out
read -t 60 ANSWER
done
#
# Do a little variable clean-up
#
unset LINE1
unset LINE2
#
} #end of get_answer function
#
#################################################################
function process_answer {
#
case $ANSWER in
y|Y|YES|yes|yEs|yeS|YEs|yES)
# If user answers "yes".do nothing.
;;
*)
# If user answers anything but "yes", exit script
echo
echo $EXIT_LINE1
echo $EXIT_LINE2
echo
exit
;;
esac
#
# Do a little variable clean-up
unset EXIT_LINE1
unset EXIT_LINE2
#
} #End of process_answer function
#
################################################################
#
# End of Function Definitions
#
############### Main Script #################################
#
# Get name of User Account to check
#
echo "Step #1 - Determine User Account name to delete "
echo
LINE1="Please enter the username of the user"
LINE2="account you wish to delete from system:"
get_answer
USER_ACCOUNT=$ANSWER
#
# Double check with script user that this is the correct User Account
#
LINE1="Is $USER_ACCOUNT the user account"
LINE2="you wish to delete from the system?[ y/n ]:"
get_answer
#
############################################################
#
# Check that USER_ACCOUNT is really an account on the system
#
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
#
if [ $? -eq 1 ] # If the account is not found, exit script
then
echo
echo "Account, $USER_ACCOUNT, not found."
echo "Leaving the script..."
echo
exit
fi
#
echo
echo "I found this record:"
echo $USER_ACCOUNT_RECORD
echo
#
LINE1="Is this the correct User Account?[y/n]:"
get_answer
#
#
# Call process_answer function:
# if user answers anything but "yes", exit script
#
EXIT_LINE1="Because the account, $USER_ACCOUNT, is not "
EXIT_LINE2="the one you wish to delete, we are leaving the script..."
process_anser
#
##############################################################
#
# Search for any running processes that belong to the User Account
#
echo
echo "Step #2 - Find process on system belonging to user account"
echo
echo "$USER_ACCOUNT has the following processes running: "
echo
#
ps -u $USER_ACCOUNT #List the processes running
#
case $? in
1) # No processes running for this User Account
#
echo "There are no processes for this account currently running."
echo
;;
0) # Processes running for this User Account.
# Ask Script User if wants us to kill the processes.
#
unset ANSWER # I think this line is not needed
LINE1="Would you like me to kill the process(es)? [y/n]:"
get_answer
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES) # if user answer "yes",
#kill User Account processes
#
echo
#
# Clean-up temp file upon signals
#
trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
#
# List user processes running
ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
#
exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input
#
read USER_PROCESS_REC # First record will be blank
read USER_PROCESS_REC
#
while [ $? -eq 0 ]
do
# obtain PID
USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1 )
kill -9 $USER_PID
echo "Killed process $USER_PID"
read USER_PROCESS_REC
done
#
echo
#
rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report
;;
*) # If user answers anything but "yes", do not kill.
echo
echo "Will not kill the process(es)."
echo
;;
esac
;;
esac
###################################################################################
#
# Create a report of all files owned by User Account
#
echo
echo "Step #3 - Find files on system belonging to user account"
echo
echo "Creating a report of all files owned by $USER_ACCOUNT."
echo
echo "It is recommended that you backup/archive these files."
echo "and then do one of two things:"
echo " 1) Delete the files"
echo " 2) Change the files' ownership to a current user account."
echo
echo "Please wait. This may take a while..."
#
REPORT_DATE=`date +%y%m%d`
REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
#
find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null
#
echo
echo "Report is complete."
echo "Name of report: $REPORT_FILE"
echo "Location of report: `pwd`"
echo
################################################################
#
# Remove User Account
echo
echo "Step #4 - Remove user account"
echo
#
LINE1="Do you wish to remove $USER_ACCOUNT's account from system? [y/n]:"
get_answer
#
# Cass process_answer function:
# if user answers anything but "yes", exit script
#
EXIT_LINE1="Since you do not wish to remove the user account."
EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..."
process_answer
#
userdel $USER_ACCOUNT # delete user account
echo
echo "User account, $USER_ACCOUNT, has been removed"
echo
#

View File

@ -0,0 +1,31 @@
#!/bin/bash
#
# Big_Users - find big disk space users in various directories
#############################################################
#Parameters for script
#
CHECK_DIRECTORIES="/var/log /home" #directories to check
#
######################### Main Script #######################
#
DATE=$(date '+%m%d%y') #Date for report file
#
exec > disk_space_$DATE.rpt #Make report file Std Output
#
echo "Top Ten Disk Space Usage" #Report header for while report
echo "for $CHECK_DIRECTORIES Directories"
#
for DIR_CHECK in $CHECK_DIRECTORIES #loop to du directories
do
echo ""
echo "The $DIR_CHECK Directory:" #Title header for each directory
#
# Creating a listing of top ten disk space users
du -S $DIR_CHECK 2>/dev/null |
sort -rn |
sed '{11,$D; =}' |
sed 'N; s/\n/ /' |
gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
#
done #End of for loop for du directories
#

View File

@ -0,0 +1,21 @@
#!/bin/bash
# send data to the the table in the MYSQL database
MYSQL=`which mysql`
if [ $# -ne 2 ]
then
echo "Usage:mtest2 emplid lastname firstname salary"
else
#脚本变量一定要用双引号,字符串变量使用单引号
statement=" insert into em_admin values(NULL, '$1', $2)"
$MYSQL emwjs -u test <<EOF
$statement
EOF
if [ $? -eq 0 ]
then
echo Data successfully added
else
echo Problem adding data
fi
fi

View File

@ -0,0 +1,18 @@
#!/bin/bash
#redirecting SQL output to a variable
MYSQL=`which mysql`
dbs=`$MYSQL emwjs -u test -Bse 'show tables;'`
for db in $dbs
do
echo $db
done
#使用xml输出数据
$MYSQL emwjs -u test -X -e 'select * from em_admin'
#使用table标签输出数据
$MYSQL emwjs -u test -H -e 'select * from em_admin'

View File

@ -0,0 +1,13 @@
#!/bin/bash
#连接数据库
mysql=`which mysql
`
#发送单个命令
$mysql emwjs -u test -e "show databases;"
#发送多个命令
$mysql emwjs -u test <<EOF
show tables;
select * from em_admin;
EOF

View File

@ -0,0 +1,8 @@
#!/bin/bash
#退出状态码最大为255超过则进行模运算
#testing the exit status
var1=10
var2=20
var3=$[ $var1 + $var2]
echo The answer is $var3
exit 5

View File

@ -0,0 +1,8 @@
#!/bin/bash
#An example of using the expr command
var1=10
var2=20
var3=`expr $var2 / $var1`
echo "The result is $var3"

View File

@ -0,0 +1,15 @@
#!/bin/bash
var1=10.45
var2=43.67
var3=33.2
var4=71
var5=`bc <<EOF
scale=4
a1 = $var1 * $var2
b1 = $var3 * $var4
a1 + b1
EOF
`
echo The final answer for this mess is $var5

View File

@ -0,0 +1,7 @@
#!/bin/bash
var1=10
var2=50
var3=45
var4=$[$var1 * ($var2 - $var3)]
echo 'The final result is '$var4

View File

@ -0,0 +1,9 @@
#!/bin/bash
#testing variables
days=10
guest="Katie"
echo "$guest logged in $days days age"
guest="Katie2"
days=5
echo "$guest logged in $days days age"

View File

@ -0,0 +1,5 @@
#!/bin/bash
#using the backtick character 会把反引号里面当作一条命令来执行
testing=`date`
echo "The date and time are:$testing"

View File

@ -0,0 +1,6 @@
#!/bin/bash
var1=100
var2=45
var3=`echo "scale=4; $var1 / $var2" | bc`
echo The answer for this is $var3

View File

@ -0,0 +1,12 @@
#!/bin/bash
#This script displays the date and who's logged on
#如果想在同一行显示
#echo -n -e 'The time is:\n\n'
echo The time is:
date
echo The one who has been logged is:
who

View File

@ -0,0 +1,10 @@
#!/bin/bash
#display user information from system
echo "User info fro userId:$USER"
echo UID:$UID
echo HOME:$HOME
#换行
echo -e '\n'
echo 'The cost of the item is \$15'

View File

@ -0,0 +1,5 @@
#!/bin/bash
#copy the /usr/bin directory listing to a log file
today=`date +%y%m%d`
ls /usr/bin -al > log.$today

View File

@ -0,0 +1,5 @@
#!/bin/bash
# testing the at command
at -f 4.sh 22:10

View File

@ -0,0 +1,16 @@
#!/bin/bash
# testing signal trapping
trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT SIGTERM
echo this is a test program
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 5
count=$[ $count+1 ]
done

View File

@ -0,0 +1,13 @@
#!/bin/bash
# trapping the script exit
trap "echo byebye" EXIT
count=1
while [ $count -le 5 ]
do
echo "Loop #$count"
sleep 3
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,16 @@
#!/bin/bash
# removeing a set trap
trap "echo byebye" EXIT
count=1
while [ $count -le 5 ]
do
echo "Loop #$count"
sleep 3
count=$[ $count + 1 ]
done
#移除捕捉
trap - EXIT
echo "I just removed the trap"

View File

@ -0,0 +1,19 @@
#!/bin/bash
BEGIN {
print "The latest list of users and shells"
print "Userid Shell"
print "------ -----"
FS=":"
}
{
print $1 " " $7
}
END {
print "This concludes the listing"
}
#执行gawk命令截取/etc/passwd输出
#gawk -f gawk.sh /etc/passwd

View File

@ -0,0 +1,12 @@
#!/bin/bash
function myprint()
{
printf "%-16s - %s", $1, $4
}
function myrand(limit)
{
return int(limit * rand())
}

View File

@ -0,0 +1,6 @@
#!/bin/bash
BEGIN{FS="\n"; RS=""}
{
myprint()
}

View File

@ -0,0 +1,2 @@
BEGIN{FS=","; print n}
{print $n}

View File

@ -0,0 +1,4 @@
10
5
123
50

View File

@ -0,0 +1,47 @@
#!/bin/bash
#使用内建变量
# NF 当前记录的字段个数
# NR 到目前为止读的记录数量
#下面的程序在每行开头输出行号,并在最后输出文件的总字段数
gawk '{ total+=NF; print NR, $0 }END{ print "Total: ", total}'
gawk 'BEGIN {testing="This is a test"; print testing; testing=45; print testing}'
#处理数字值
gawk 'BEGIN{x=4; x= x*2+3; printx}'
#处理数组
gawk 'BEGIN{capital["Ill"] = "SprintField"; print capital["Ill"]}'
#遍历数组变量
gawk 'BEGIN{
var["a"] = 1
var["g"] = 2
var["m"] = 3
for( test in var)
{
print "Index:",test,"- Value:",var[test]
}
}'
print "------"
#删除数组变量
gawk 'BEGIN{
var["a"] = 1
var["g"] = 2
for (test in var)
{
print "Index:",test," - Value:", var[test]
}
delete var["g"]
print "----"
for (test in var)
{
print "Index;",test," - Value:", var[test]
}
}'

View File

@ -0,0 +1,57 @@
#!/bin/bash
#正则表达式
gawk 'BEGIN{FS=","}
/11/{print $1}
' test
#if-else语句
gawk '{
if($1 > 20)
{
x=$1*20
print x
}
else
{
x=$1/2
print x
}
}' test
#while 语句
gawk '{
total = 0
i=1
while(i<4)
{
total+=$i
i++
}
avg = total/3
print "Average:".avg
}' test
#do-while语句
gawk '{
total=0
i=1
do
{
total += $i
i++
}while(total < 150)
print total }' test
#for语句
gawk '{
total = 0
for (i=1; i<4; i++)
{
total+=$i
}
avg = total/3
print "Average:".avg
}' test

View File

@ -0,0 +1,12 @@
#!/bin/bash
#gawk 自定义函数
gawk '
function myprint()
{
printf "%-16s - %s\n", $1, $4
}
BEGIN{FS="\n"; RS=""}
{
myprint()
}' test

View File

@ -0,0 +1,5 @@
#!/bin/bash
#使用函数库和gawk脚本
gawk -f gawk函数库 -f gawk脚本 test

View File

@ -0,0 +1,66 @@
#!/bin/bash
#print用于产生简单输出
#多个表达式的字符串值之间用输出字段分隔符分开
gawk '{ print $1, $2 }'
#输出字段分割符与输出记录分隔符存储在内建变量OFS与ORS中
#初始情况下OFS与ORS被设置成一个空格符与一个换行符但它们的值可以在任何时候改变
#下面这个程序打印每一行的第1第2个字段字段之间用分号分开在每一行的第2个字段之后输出两个换行符
gawk 'BEGIN { OFS = ":"; ORS = "\n\n" }
{ print $1, $2 }'
#下面这个程序拼接第1个与第2个字段两个字段之间没有输出字段分隔符插入
gawk '{ print $1 $2 }'
#这两句话等价
gawk '{ print }'
gawk '{ print $0 }'
#输出空行
gawk '{ print "" }'
#printf用于产生格式化输出
#printf不会自动换行需要手动添加\n
#格式说明符以%开始,以转换字符结束
# - 表达式在它的域内左对齐,没有则右对齐
# width 为了达到规定的宽度,必要时填充空格
# .prec 字符串最大宽度, 或十进制数的小数部分的位数
gawk '{ printf ("Name:%-10sAge:%-5dWeight:%7.2f\n", $1, $2, $3) }'
#输出到文件
#重定向运算符>与>>用于将输出重定向到文件,文件名必须用双引号括起来
#下面这个程序将所有输入行的第1个与第3个字段输出到两个文件中如果第3个字段大于100则输出到bigpop否则输出到smallpop
gawk '{ print($1, $3) > ($3 > 100 ? "bigpop" : "smallpop") }'
#输出到管道
#print的输出将以管道的方式传递给command
# Canada 3852
# China 3705
# USA 3615
# Brazil 3286
gawk '{ pop[$1]+=$2 }
END{ for(c in pop) printf("%15-s%6d\n", c, pop[c]) | "sort -nk 2"; close("sort -nk 2") }'
#关闭文件与管道
#语句close(expression)关闭一个文件或管道文件或管道由expression指定。
#expression的字符串值必须与最初用于创建文件或管道的字符串值相同。
#在同一个程序中如果你写了一个文件而待会儿想要读取它那么就需要调用close。
#某一时刻,同时处于打开状态的文件或管道数量最大值由实现定义。
close("sort -nk 2")

View File

@ -0,0 +1,18 @@
#!/bin/bash
# count number of files in your PATH
mypath=`echo $PATH | sed 's/:/ /g'`
count=0
for directory in $mypath
do
check=`ls $directory`
echo $check
for item in $check
do
count=$[ $count + 1 ]
done
echo "$directory - $count"
count=0
done

View File

@ -0,0 +1,7 @@
#!/bin/bash
#验证邮件
gawk --re-interval '/^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\+]+)\.([a-zA-Z]{2,5})/{print $0}'

View File

@ -0,0 +1,24 @@
#!/bin/bash
#向文件写入
sed '1,2w test1' test1
echo -e "next\n"
#从文件读取
sed '3r ./test' ./test
echo -e "next\n"
#从文件读取,并插入字符流
sed '/lazy/r test' test
#向数据流末尾添加数据
sed '$r test' test
echo -e "next1\n"
sed '/lazy/ {
r test
d
}' test

View File

@ -0,0 +1,104 @@
#!/bin/bash
#sed编辑器基础
#替换标记
sed 's/lazy/ht/' ./test
echo -e "next\n"
#可用的替换标记
#1.数字 表明新闻本将替换第几处模式匹配的地方
sed 's/lazy/ht/2' ./test
#2.g 表明新文件将会替换所有已有文本出现的地方
sed 's/lazy/ht/g' ./test
#3.p 表明原来行的内容要打印出来,替换后的
sed 's/lazy/ht/p' ./test
#4.w file 将替换的结果写到文件中
sed 's/lazy/ht/w test1' ./test
echo -e "next\n"
#替换字符
sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
#或者
sed 's!/bin/bash!/bin/csh!' /etc/passwd
echo -e "next\n"
#使用地址
#1.数字方式的行寻址
sed '2s/lazy/cat/' ./test
sed '2,3s/lazy/cat/' ./test
sed '2,$s/lazy/cat/' ./test
#2.使用文本模式过滤器
sed '/tiandi/s/bash/csh/' /etc/passwd
echo -e "next\n"
#组合命令
sed '2{
s/fox/elephant/
s/dog/cat/
}' test
sed '2,${
s/fox/elephant/
s/dog/cat/
}' test
echo -e "next\n"
#删除行
sed '3d' ./test
sed '2,$d' ./test
sed '/number 1/d' ./test
#删除两个文本模式来删除某个范围的行,第一个开启删除功能,第二个关闭删除功能
sed '/1/,/3/d' ./test
echo -e "next\n"
#插入和附加文本
sed '3i\
This is an appended line.' ./test
sed '$a\
This is a new line of text.' ./test
#修改行
sed '3c\
This a changed line of text.' ./test
sed '/number 1/c\
This a changed line of text.' ./test
#替换两行文本
#sed '2,3c\
#This a changed line of text.' ./test
#转换命令,处理单个字符
#sed 'y/123/789/' ./test
echo -e "next\n"
#回顾打印
# p 打印文本行
# -n 禁止其他行,只打印包含匹配文本模式的行
sed -n '/number 3/p' ./test
#查看修改之前的行和修改之后的行
#sed -n '/3/{
#p
#s/line/test/p
#}' ./test
echo -e "next\n"
# 打印行号
sed '=' ./test
#打印指定的行和行号
#sed -n '/lazy/{
#=
#p
#}' ./test
#列出行 打印数据流中的文本和不可打印的ASCII字符任何不可打印的字符都用它们的八进制值前加一个反斜线或标准C风格的命名法比如用\t来代表制表符
sed -n 'l' ./test

View File

@ -0,0 +1,5 @@
This is the header line
This is the first data line
This is the data line
This is the second data line
line This is the last line

View File

@ -0,0 +1,16 @@
#!/bin/bash
#h将模式空间保存到保持空间
#H将模式空间附加到保持空间
#g将保持空间复制到模式空间
#G将保持空间保存到模式空间
#x交换模式空间和保持空间的内容
sed -n '/first/{
h
p
n
p
g
p
}' test

View File

@ -0,0 +1,21 @@
#!/bin/bash
#多个空格只保留一个
#sed '/./,/^$/!d' test
#删除开头的空白行
#sed '/./,$!d' test
#删除结尾的空白行
sed '{
:start
/^\n*$/{$d; N; b start}
}' test
#删除html标签
#有问题
#s/<.*>//g
#sed 's/<[^>]*>//g' test1
#sed 's/<[^>]*>//g;/^$/d' test1

View File

@ -0,0 +1,8 @@
#!/bin/bash
# shell wrapper for sed editor script to reverse lines
sed -n '{
1!G
h
$p
}' $1

View File

@ -0,0 +1,8 @@
!/bin/bash
#排除命令,使本来起作用的命令不起作用
sed -n '/heade/!p' test
#反转文本文件
sed -n '{1!G ; h; $p}' test

View File

@ -0,0 +1,11 @@
#!/bin/bash
#and符号代表替换命令中的匹配模式不管预定义模式是什么文本都可以用and符号替换and符号会提取匹配替换命令中指定替换模式中的所有字符串
echo "The cat sleeps in his hat" | sed 's/.at/"&"/g'
#替换单独的单词
echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 user/'
#在长数字中插入逗号
echo "1234567" | sed '{:start; s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/; t start}'

View File

@ -0,0 +1,4 @@
#!/bin/bash
#测试如果测试成功如果没有标签sed会跳转到结尾如果有标签就跳转到标签如果测试失败则不会跳转
sed -n '{s/first/matched/; t; s/This is the/No match on/}' test

View File

@ -0,0 +1,3 @@
#!/bin/bash
sed '=' test | sed 'N; s/\n/ /'

View File

@ -0,0 +1,7 @@
#!/bin/bash
#跳转到指定脚本
sed '{/first/b jump1; s/This is the/No jump on/; :jump1; s/This is the/Jump here on/}' test
#跳转到开头,删除每一个逗号,并保证删除最后一个逗号之后,跳出循环
sed -n '{:start; s/,//1p; /,/b start}' test

View File

@ -0,0 +1,10 @@
#!/bin/bash
#输出末尾10行数据
sed '{
:start
$q
N
11,$D
b start
}' /etc/passwd

View File

@ -0,0 +1,21 @@
#!/bin/bash
# add commas to numbers in factorial answer
factorial=1
counter=1
number=$1
while [ $counter -le $number ]
do
factorial=$[ $factorial * $counter ]
counter=$[ $counter + 1 ]
done
result=`echo $factorial | sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t start
}'`
echo "The result is $result"

View File

@ -0,0 +1,25 @@
#!/bin/bash
# returning an array value
function arraydblr {
local origarry
local newarray
local elements
local i
origarry=(`echo "$@"`)
newarray=(`echo "$@"`)
elements=$[ $# - 1 ]
for (( i=0; i<=$elements; i++ ))
{
newarray[$i]=$[ ${origarry[$i]} * 2 ]
}
echo ${newarray[*]}
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=(`arraydblr $arg1`)
echo "The new array is : ${result[*]}"

View File

@ -0,0 +1,12 @@
#!/bin/bash
# using the return command in a function
function db1 {
read -p "Enter a value:" value
echo "doubling the value"
return $[ $value * 2 ]
}
db1
echo "The new value is $?"

View File

@ -0,0 +1,21 @@
#!/bin/bash
# demonstrating a bad use of variables
function func1 {
temp=$[ $value + 5 ]
result=$[ $temp * 2 ]
}
temlp=4
value=6
func1
echo "The result is $result"
if [ $temp -gt $value ]
then
echo "Temp is larger"
else
echo "temp is smaller"
fi

View File

@ -0,0 +1,11 @@
#!/bin/bash
# using the echo to return a value
function db1 {
read -p "Enter a value:" value
echo $[ $value*2 ]
}
result=`db1`
echo "The new value is $result"

View File

@ -0,0 +1,16 @@
#!/bin/bash
# using a global variable to pass a value
function db1 {
# $1和$2 不能从命令行中传递,只能调用函数时,手动传递
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ]
then
value=`db1 $1 $2`
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi

View File

@ -0,0 +1,21 @@
#!/bin/bash
# demonstrating the local keyword
function func1 {
local temp=$[ $value +5 ]
result=$[ $temp * 2 ]
}
temp=4
value=6
func1
echo "The result is $result"
if [ $temp -gt $value ]
then
echo "temp is larger"
else
echo "temp is smaller"
fi

View File

@ -0,0 +1,8 @@
#!/bin/bash
#using a library file the wrong way
. ./脚本库.sh
result=`addem 10 15`
echo "The result is $result"

View File

@ -0,0 +1,12 @@
#!/bin/bash
# using a global variable to pass a value
function db1 {
value=$[ $value * 2 ]
}
read -p "Enter a value: " value
db1
echo "The new value is : $value"

View File

@ -0,0 +1,16 @@
#!/bin/bash
function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 -1 ]
local result=`factorial $temp`
echo $[ $result * $1 ]
fi
}
read -p "Please input a value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"

View File

@ -0,0 +1,32 @@
#!/bin/bash
# passing parameters to a function
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15:"
value=`addem 10 15`
echo $value
echo -n "Let's try adding just one number: "
value=`addem 10`
echo $value
echo -n "Now trying adding no number: "
value=`addem`
echo $value
echo -n "Finally, try adding three or more numbers: "
value=`addem 10 15 20`
echo $value

View File

@ -0,0 +1,17 @@
#!/bin/bash
# using a function in script
function func1 {
echo "This is an example of a function"
}
count=1
while [ $count -le 5 ]
do
func1
count=$[ $count+1 ]
done
echo "This is the end of the loop"
func1
echo "Now this is the end of the script"

View File

@ -0,0 +1,23 @@
#!/bin/bash
# trying to pass an array variable
function testit {
echo "The parameters are : $@"
#函数只会读取数组变量的第一个值
thisarray=$1
echo "The received array is ${thisarray[*]}"
local newarray
newarray=(`echo "$@"`)
echo "The new array value is : ${newarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
#将数组变量当成一个函数参数,函数只会去函数变量第一个值
#testit $myarray
testit ${myarray[*]}

View File

@ -0,0 +1,20 @@
#!/bin/bash
#adding values in the array
function addarray {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=`addarray $arg1`
echo "The result is $result"

View File

@ -0,0 +1,20 @@
#!/bin/bash
# myscript functions
function addem {
echo $[ $1 + $2 ]
}
function multem {
echo $[ $1 * $2 ]
}
function divem {
if [ $2 -ne 0]
then
echo $[ $1/$2 ]
else
echo -1
fi
}

View File

@ -0,0 +1,52 @@
#!/bin/bash
# testing the exit status of a function
func1() {
echo "Trying to display a non-existent file"
ls -l badfile
}
#由于最后一条命令未执行成功返回的状态码非0
echo "testing the function"
func1
echo "The exit status is : $?"
func2() {
ls -l badfile
echo "Another test to display a non-existent file"
}
#由于最后一条命令echo执行成功返回的状态码为0
echo "Another test"
func2
echo "The exit status is : $?"

View File

@ -0,0 +1,3 @@
#!/bin/bash
dialog --title text --msgbox "This is a test" 10 20

View File

@ -0,0 +1,36 @@
#!/bin/bash
# using select in the menu
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
PS3="Enter option:"
select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program"
do
case $option in
"Exit program")
break;;
"Display disk space")
diskspace;;
"Display logged on users")
whoseon;;
"Display memory usage")
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
done
clear

View File

@ -0,0 +1,49 @@
#!/bin/bash
function menu {
clear
echo
echo -e "\t\tSys Admin Menu\n"
echo -e "\t1. Display disk space"
echo -e "\t2. Display logged on users"
echo -e "\t3. Display memory usage"
echo -e "\t0. Exit program\n\n"
echo -en "\t\tEnter option:"
read -n 1 option
}
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
while [ 1 ]
do
menu
case $option in
0)
break;;
1)
diskspace;;
2)
whoseon;;
3)
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
echo -en "\n\n\t\tHit any key to continue"
read -n 1 line
done
clear

View File

@ -0,0 +1,47 @@
#!/bin/bash
# using dialog to create a menu
temp=`mktemp -t test.XXXXXX`
temp2=`mktemp -t test2.XXXXXX`
function diskspace {
df -k > $temp
dialog --textbox $temp 20 60
}
function whoseon {
who > $temp
dialog --textbox $temp 20 50
}
function menusage {
cat /proc/meminfo > $temp
dialog --textbox $temp 20 50
}
while [ 1 ]
do
dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2> $temp2
if [ $? -eq 1 ]
then
break
fi
selection=`cat $temp2`
case $selection in
1)
diskspace;;
2)
whoseon;;
3)
menusage;;
0)
break;;
*)
dialog --msgbox "Sorry,invalid selection" 10 30
esac
done
rm -f $temp 2> /dev/null
rm -f $temp2 2> /dev/null

View File

@ -0,0 +1,2 @@
ls: cannot access badtest: No such file or directory
but this should go to the testerror file

View File

@ -0,0 +1 @@
This is the end of the script

View File

@ -0,0 +1,6 @@
#!/bin/bash
# testing STDERR messages
echo "This is an error " >&2
echo "This is another error"
echo "This is also an error" >&2

View File

@ -0,0 +1,11 @@
#!/bin/bash
# reading data from a file
count=1
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"

View File

@ -0,0 +1,12 @@
#!/bin/bash
# simple demonstration of the getopts command
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option";;
*) echo "Unknown option:$opt";;
esac
done

View File

@ -0,0 +1,20 @@
#!/bin/bash
# processing options and parameters with getopts
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option,with value $OPTARG";;
c) echo "Found the -c option";;
d) echo "Found the -d option";;
*) echo "Unknown option: $opt";;
esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,25 @@
#!/bin/bash
#extracting command line options and values with getopt
# getopt command is not goot at dealing with space,we can use getopts
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option,with parameter value $param"
shift;;
-c) echo "Found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count+1 ]
done

View File

@ -0,0 +1,17 @@
#!/bin/bash
# shift n 移动变量
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count+1 ]
shift
done
echo -e "\n"
# demonstrating a multi-position shift
echo "The original parameters : $*"
shift 2
echo "Here's the new first parameter: $1"

View File

@ -0,0 +1,23 @@
#!/bin/bash
#extracting options and parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b option";;
-c) echo "Found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,14 @@
#!/bin/bash
# testing lsof with file descriptors
exec 3>test
exec 6>test
exec 7<test
lsof -a -p $$ -d0,1,2,3,6,7

View File

@ -0,0 +1,16 @@
#!/bin/bash
# using a temporary directory
tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXX`
tempfile2=`mktemp temp.XXXXXX`
exec 7> $tempfile1
exec 8> $tempfile2
echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" >&7
echo "This is a test line of data for $tempfile2" >&8

View File

@ -0,0 +1,22 @@
#!/bin/bash
# creating and using a temp file
tempfile=`mktemp test.XXXXXX`
exec 3>$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line" >&3
echo "This is the last line" >&3
exec 3>&-
echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null

View File

@ -0,0 +1,13 @@
#!/bin/bash
#storing STDOUT, then coming back to it
exec 3>&1
exec 1>test
echo "This should store in output file"
echo "along with this line"
exec 1>&3
echo "Now things should be back to normal"

View File

@ -0,0 +1,16 @@
#!/bin/bash
# testing closing file descriptors
exec 3>test
echo "This is a test line of data" >&3
# closing file descriptor
exec 3>&-
echo "This won't work" >&3
cat test
#覆盖前一个test文件
exec 3>test
echo "This'll be bad" >&3

View File

@ -0,0 +1,7 @@
#!/bin/bash
# testing inpiut/output file descriptor
exec 3<> test
read line <&3
echo "Read: $line"
echo "This is the test line" >&3

View File

@ -0,0 +1,22 @@
#!/bin/bash
# redirecting input file descriptors
exec 3>&1
echo "This is the 3 file descriptor" >&3
exec 6>&0
exec 0<test
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count+1 ]
done
exec 0<&6
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Sorry, this is the end";;
esac

View File

@ -0,0 +1,9 @@
#!/bin/bash
# using an alternative file descriptor
exec 3>test
echo "This should display on the monitor"
echo "and this should be stored in the file" >&3
echo "Then this should be back on the monitor"

View File

@ -0,0 +1,10 @@
#!/bin/bash
# getting the number of parameters
echo There were $# parameters supplied
#花括号里不能使用美元符号
params=$#
echo The last parameter is $params
echo The last parameter is ${!#}

View File

@ -0,0 +1,12 @@
#!/bin/bash
# creating a temp file in /tmp
tempfile=`mktemp -t tmp.XXXXXX`
echo "This is a test file" > $tempfile
echo "This is the second line of the test" >> $tempfile
echo ”The temp is locate at : $tempfile
cat $tempfile
rm -f $tempfile

Some files were not shown because too many files have changed in this diff Show More