赞
踩
[root@server ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh #相当于/bin/sh的备份
/usr/bin/bash #相当于/bin/bash的备份
[root@server ~]# echo $SHELL
/bin/bash
#comment1
#comment2
#comment3
...
:<<'xxxx'
comment1
comment2
comment3
……
xxxx
xxxx可以是字符或数字,单引号可以不加,但以防出现莫名其妙的意外发生,最好加上
# Date:创建日期
# Author:作者
# Mail:联系方式
# Function:功能
# Version:版本
[root@server ~]# vim ~/.vimrc # 新建配置文件 autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()" func SetTitle() if expand("%:e") == 'sh' call setline(1,"#!/bin/bash") call setline(2,"##############################################################") call setline(3, "# File Name: ".expand("%")) call setline(4, "# Version: V1.0") call setline(5, "# Author: Andy_Sun") call setline(6, "# Email: Andy_Sun@163.com") call setline(7, "# Organization: http://www.cnblogs.com/Andy_Sun/") call setline(8, "# Created Time : ".strftime("%F %T")) call setline(9, "# Description:") call setline(10,"##############################################################") call setline(11, "") endif endfunc
创建一个标准的脚本,实现有注释信息,版权信息,内容任意,并执行
[root@server ~]# vim cmatrix.sh #写入以下内容
wget http://archive.ubuntu.com/ubuntu/pool/universe/c/cmatrix/cmatrix_1.2a.orig.tar.gz
tar xvf cmatrix_1.2a.orig.tar.gz
cd cmatrix-1.2a
yum install -y ncurses-devel
yum install -y gcc
./configure && make && make install
echo "Program installation complete !"
echo "Program installation complete !"
echo "Program installation complete !"
[root@server ~]# bash cmatrix.sh #执行脚本
[root@server ~]# cmatrix
[root@server ~]# cat /etc/passwd | grep root
[root@server ~]# grep root /etc/passwd
[root@server ~]# vim 99.sh #!/bin/bash for((i=1;i<10;i++)) do echo -ne "$i\t" done echo for((i=1;i<70;i++)) do echo -n "=" done echo for((i=1;i<10;i++)) do for((j=1;j<=i;j++)) do echo -en "$i*$j=$[i*j]\t" done echo done
[root@server ~]# bash 99.sh
1 2 3 4 5 6 7 8 9
=====================================================================
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
[root@server ~]# vim test.sh
#!/bin/bash
echo "china"
[root@server ~]# bash test.sh
china
[root@server ~]# sh test.sh
china
可以使用bash -n 脚本名 ,进行语法检测,且不执行脚本
可以使用bash -x 脚本名 ,进行脚本执行跟踪,逐条语句的跟踪执行
[root@server ~]# ll -d test.sh
-rw-r--r-- 1 root root 334 3月 14 00:00 test.sh
[root@server ~]# ./test.sh
-bash: ./test.sh: 权限不够
[root@server ~]# chmod +x test.sh
[root@server ~]# ll -d test.sh
-rwxr-xr-x 1 root root 334 3月 14 00:00 test.sh
[root@server ~]# ./test.sh
china
[root@server ~]# vim /t1.sh #!/bin/bash echo "china" [root@server ~]# /t1.sh -bash: /t1.sh: 权限不够 [root@server ~]# ll -d /t1.sh -rw-r--r-- 1 root root 333 3月 14 00:07 /t1.sh [root@server ~]# chmod +x /t1.sh [root@server ~]# ll -d /t1.sh -rwxr-xr-x 1 root root 333 3月 14 00:07 /t1.sh [root@server ~]# /t1.sh china
[root@server ~]# source /t1.sh
china
[root@server ~]# . test.sh
china
格式:echo -参数 内容
参数:
可以输出带颜色得字体:
格式:echo -e "\e[字体控制;字体颜色或背景色 字符串内容 \e[0m"
\e[表示控制开始,\e[0m表示控制结束
字体控制选项:1表示高亮,4表示下划线,5颜色闪烁
颜色如下:字颜色:30-37 , 背景色:40-47
[root@server ~]# vim color.sh echo -e "\e[30m 黑色字\e[0m" echo -e "\e[1;31m 紅色字\e[0m" echo -e "\e[32m 綠色字\e[0m" echo -e "\e[33m 黃色字\e[0m" echo -e "\e[34m 藍色字\e[0m" echo -e "\e[35m 紫色字\e[0m" echo -e "\e[36m 天藍字\e[0m" echo -e "\e[37m 白色字\e[0m" echo -e "\e[40;37m 黑底白字\e[0m" echo -e "\e[41;37m 紅底白字\e[0m" echo -e "\e[42;37m 綠底白字\e[0m" echo -e "\e[43;37m 黃底白字\e[0m" echo -e "\e[44;37m 藍底白字\e[0m" echo -e "\e[45;37m 紫底白字\e[0m" echo -e "\e[46;37m 天藍底白字\e[0m" echo -e "\e[47;30m 白底黑字\e[0m" [root@server ~]# bash color.sh #!/bin/bash echo -e "\e[30m 黑色字\e[0m" echo -e "\e[1;31m 紅色字\e[0m" echo -e "\e[32m 綠色字\e[0m" echo -e "\e[33m 黃色字\e[0m" echo -e "\e[34m 藍色字\e[0m" echo -e "\e[35m 紫色字\e[0m" echo -e "\e[36m 天藍字\e[0m" echo -e "\e[37m 白色字\e[0m" echo -e "\e[40;37m 黑底白字\e[0m" echo -e "\e[41;37m 紅底白字\e[0m" echo -e "\e[42;37m 綠底白字\e[0m" echo -e "\e[43;37m 黃底白字\e[0m" echo -e "\e[44;37m 藍底白字\e[0m" echo -e "\e[45;37m 紫底白字\e[0m" echo -e "\e[46;37m 天藍底白字\e[0m" echo -e "\e[47;30m 白底黑字\e[0m"
[root@server ~]# bash color.sh
printf 格式控制字符串 参数列表
[root@server ~]# echo "hello world" hello world [root@server ~]# printf "hello, shell\n" hello, shell [root@server ~]# printf "%d %s\n" 1 'abc' 1 abc [root@server ~]# printf "%-10s %-8s %-4s\n" '华子' '男' '130' 华子 男 130 # %s %c %d %f 都是格式替代符,%s 输出一个字符串,%d 整型输出,%c 输出一个字符,%f 输出实数,以小数形式输出。 # %-10s 指一个宽度为 10 个字符(- 表示左对齐,没有则表示右对齐),任何字符都会被显示在 10 个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。 [root@server ~]# printf "%-10s %-8s %-4.2f\n" '郭靖' '男' 66.1234 郭靖 男 66.12 # %-4.2f 指格式化为小数,其中 .2 指保留2位小数
history [参数] [历史命令保存文件]
参数
修改默认记录历史命令条数:
[root@server ~]# vim /etc/profile (重启生效)
HISTSIZE=1000 #储存历史命令记录条数
[root@server /]# history | tr -s " " | cut -d " " -f3 | sort | uniq -c | sort -nr | head -10
# 浏览历史命令 | 压缩为1个空格 | 截取以空格为间隔的第3列 | 排序 | 统计并去重 | 以次数为依据进行降序排序 | 取前10个
[root@server /]# vim ~/.bashrc # 编辑bash的配置文件,最后一行后添加:
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S: "
[root@server /]# source ~/.bashrc # 刷新配置
[root@server /]# history # 查看结果
alias 别名=原命令
[root@server ~]# alias hi=history #重启无效
[root@server ~]# hi
第一顺位:执行用绝对路径或相对路径执行的命令。
第二顺位:执行别名。
第三顺位:执行 Bash 的内部命令。
第四顺位:执行按照 $PATH 环境变量定义的目录查找顺序找到的第一个命令。
[root@server ~]# vim ~/.bashrc # 在最下面增加
alias hi=history
[root@server ~]# date ; ls -l /etc/passwd
[root@server ~]# mkdir /mnt/iso && mount /dev/sr0 /mnt/iso
[root@server ~]# mkdir tt || ls /
[root@server ~]# mkdir tt || ls / # 可以再次执行
[root@server ~]# ip a | grep ens160 | grep inet | tr -s " " | cut -d " " -f3
192.168.48.130/24
# 查看IP信息 | 过滤包含esn160行 | 过滤包含inet行 | 压缩空格为1个 | 列向截取列
[root@server ~]# ip a | grep ens160 | grep inet | cut -d "/" -f1 | cut -d " " -f6
192.168.48.130
[root@server ~]# free -h | grep Mem | tr -s " " | cut -d " " -f4
0----------------命令运行成功
1----------------通知未知错误
2----------------误用shell命令
126-------------命令不可执行
127-------------没有找到命令
128-------------无效退出参数
128+x-----------linux信号x的严重错误
130--------------命令通过Ctrl+C终止
255--------------退出状态码越界
[root@server ~]# echo 'china'
china
[root@server ~]# echo $?
0
[root@server ~]# ehco 'china'
bash: ehco: command not found...
Similar command is: 'echo'
[root@server ~]# echo $?
127
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。