【大数据】Spark优化经验&案例--数据倾斜 《Mysql必知必会》读书笔记 jar包名中自动添加git commit id PyCharm教学视频学习笔记 《SQL基础教程》简要总结 《设计师要懂心理学》读书笔记 MySQL与MariaDB学习笔记 WDT (Folly) 安装指南 -- CentOS 7 [solved]Page build failed(Jekyll) 数据包过滤及分析实例 tshark tcpdump Scala Tour 学习总结 “Docker容器和容器云”读书笔记(1) “Docker Practice”读书笔记 “图解基础设施设计模式”小结 “图解服务器端网络架构”小结 Python网络安全编程 数据包解析笔记 华为挑战赛(1) DDoS攻击防御与云服务 基于网络回溯分析技术的异常行为分析 “Linux程序设计”小结(进程间通信) C语言编程规范(华为软件精英挑战赛) 2017阿里在线编程题--单源最短路径问题 2017年阿里在线编程题-- 数串分组 Uinx/Linux上的帮助查询命令 你懂C,所以C++不在话下 一篇特别长的总结(C专家编程) 程序员面试金典--笔记(精华篇) C陷阱与缺陷--笔记 半小时搭建电子商务网站--opencart linux网络知识和工具(持续更新) 网卡参数查询及设置工具ethtool 高性能流量生成工具trafgen(DDoS模拟) Linux流量控制工具TC 流量控制工具TC详细说明 tcpdump过滤数据包,结果不对? Lecture 网络攻击与防御技术笔记 gotgit-git权威指南 高效使用MacOS所要知道的 shell内置字符串处理 配置ntp(知其所以然) 360黑客攻防技术分享会--记录 中毒U盘恢复--快捷键病毒 Tor--anonymity network介绍(PPT) IBM bluemix 再读《Linux Shell脚本攻略》 linux shell 学习摘记(9) linux shell 学习摘记(8) linux shell 学习摘记(7) linux shell 学习摘记(6) linux shell 学习摘记(5) linux shell 学习摘记(4) linux shell 学习摘记(3) linux shell 学习摘记(2) linux shell 学习摘记(1) firefox vim 插件 vimperator A Byte of Vim 笔记 windows注册表小知识 安全测试工具篇(开源&商业) 安全及性能测试工具(网站收集) 性能测试工具 屡试不爽的“3个”iPad使用技巧 Shell Shortcuts(和Tab键一样实用) vim--自动添加jekyll post信息头 vim 自动给文件添加头部信息 GitHub Tips (很实用,值得收藏) Linux路由、防火墙、NAT命令

linux shell 学习摘记(1)

2016年09月23日

《linux shell 脚本编程攻略 (第2版)》 第一章 “小试牛刀” 笔记

主要命令:let, cat, 变量字符串处理, 数学计算, 管道|, 重定向, $, expr, bc, echo, $#, $*, $0, eval, tr, printf, read, tput, 循环语句, 条件语句, 分隔符IFS

  • 文本行#!/bin/bash中的#!读作 shebang (也可读做 hashbangpound-bang参考维基

  • 终端打印除了echo命令还可以用printf(语法规则同C语言类似),如printf "%s %f\n" swf 1234输出 swf 1234.000000

  • 变量替换在单引号中无效

  • 打印彩色字体echo -e "\e[1;31m This is red text \e[0m"\e[1;31m将颜色设为红色(;前的数字表示背景颜色,;后的表示字体颜色),\e[0m将颜色重新置回

  • 查询某程序运行时环境变量cat /proc/进程ID/environca (pgrep gedit查看程序gedit的PID,或使用ps aux | grep gedit查看)

  • tr将输出重新格式化 cat /proc/84354/environ | tr '\0' '\n'

  • 获取字符串长度var=wenfeng; echo ${#var}(助记:#是number sign),获取当前使用的SHELL echo $0echo $SHELL

  • ${parameter:+expression} 如果parameter有值且不为空,则使用expression的值。prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1; } (eval`可将多个参数整合成一个参数)

  • 显示/修改Bash提示字符串echo $PS1/PS1="wefeng"

  • shell数学运算

    1. let 变量名前不需要用$,如let result=no1+no2; echo $result
    2. (())result=$(( no1 + 50 ))result=$ (( $no1 + 50 ))
    3. []result=$[ no1 + no2]result=$[ $no1 + 5 ]
    4. exprresult=反引号expr 3 + 4反引号result=$(expr $no1 + 5)
    5. bc命令(支持浮点数echo "scale=2;3/8" | bc 设置小数位数为2; echo "obase=2;$no" | bc将数字转化为2进制; echo "obase=10;ibase=2;$no" | bc 将二进制转化为十进制
  • 重定向
    1. stderr转换成stdout重定向 cmd 2>&1 stdout.txt 或者 cmd &> output.txt
    2. command | tee FILE1 FILE2 接收来自stdin的数据,将副本写入FILE1和FILE2,同时也将副本左后后续命令的stdin
    3. cmd - 将stdin作为命令参数
  • 脚本内部文件块重定向
#!/bin/bash
cat > log.txt <<EOF
This is a test log file.
Function: System statistics
EOF

或者

#!/bin/bash
cat<<EOF>log.txt
This is a test log file.
Function: System statistics
EOF
  • 自定义文件描述符 exec 3<input.txt exec 5>>output.txt
[root@share codes]# cat log.txt 
This is a test log file.
Function: System statistics
[root@share codes]# exec 3<log.txt 
[root@share codes]# cat<&3
This is a test log file.
Function: System statistics
[root@share codes]# exec 5>>output.txt
[root@share codes]# echo newline >&5
[root@share codes]# cat output.txt 
newline
  • 数组array_var=(s w f), echo ${array_var[0]}打印一个元素, echo ${#array_var[*]}打印数组长度,echo ${!array_var[*]}打印出数组索引列表,echo ${array_var[*]} 打印出所有元素

  • 关联数组(相当于字典)先声明declare -A ass_array,再赋值ass_array=([index1]=val1 [index2]=val2) 或者 ass_array[index3]=val3 其他操作同上,只是索引不再是数字了

  • 当用alias设置了别名命令后,当不想用这个别名是,可对其进行转义即 \command

  • 获取当前终端行数/列数/终端名 tput lines/tput cols/tput longname; tput sc存储光标位置, tput rc恢复光标位置 tput ed清除从当前光标位置到行尾之间的所有内容 (下面实现一个计时器)

#! /bin/bash

echo -n Count:
tput sc

count=0;
while true;
do
  if [ $count -lt 40 ];
  then
    let count++;
    sleep 1;
    tput rc
    tput ed
    echo -n $count;
  else exit 0;
  fi
done
  • 通过stty实现 输入密码时不应显示输入内容(也可直接read -s启动slient模式,但stty更通用)
#!/bin/bash
echo -e "Enter password:"
stty -echo
read password
stty echo
echo
echo Password is $password
  • date +%s 打印timestamp (从1970年1月1日0点开始的秒数)即POSIX时间; date "+%d %B %Y" 按格式输出

  • 调试脚本

    1. bash -x script.shsh -x script.sh
    2. set -xset +x(执行命令后会显示该指令及其参数);set -v当命令进行读取时显示输入,set +v进制打印输入 详细介绍
    3. 将shebang从#!/bin/bash 改成 #!/bin/bash -xv 也可(./script.sh运行)
    4. 自定义格式显示调试信息,新建如下脚本,然后命令行键入 [root@share codes]# _DEBUG=on sh debug.sh (:告诉shell不进行任何操作)
#!/bin/bash

function DEBUG()
{
  [ "$_DEBUG" == "on" ] && $@ || :
}

for i in {1..10};
do
  DEBUG echo $i
done
  • 函数和参数
    1. $@ 被扩展成 $1 $2 $3
    2. $* 被扩展成$1c$2c$3(c是IFS的第一个字符, IFS即internal field seprator, env | grep IFSset | grep IFS查看)
    3. $? 获取命令或函数的返回值
    4. ”$@” 比 “$*” 更常用,后者会把所有参数当作单个字符串处理
    5. 递归 F() { echo $1; F hello; sleep 1; } ,这是 fork bomb :(){ :|:& };: (可通过修改/etc/security/limits.conf或命令ulimit来限制生成的最大进程数)维基扩展
    6. 导出函数 export -f fname 可将函数的作用域扩展到子进程中
    7. 向命令传递参数的方式command -p -v -k 1 file或者command -pv -k1 file或者command -pvk 1 file或者command file -pvk 1
  • 存储命令的输出
    1. $( ) 通过子shell方式, 如 output=$(ls | cat -n) 子shell里面的变化如cd XX等不会反应到主shell中
    2. 反引用方式(通过反引号)
    3. 可将子shell或反引用放入以个双引号中,以保留空格和换行符(\n)
cmd_output=`ls | cat -n`
echo $cmd_output
  • read -n 5 -s -t 2 -d ":" -p "Enter your password:" var 从输入中以无回显的方式读取5个字符并存入变量var中,且需要在2秒内输入,以:作为输入行的结束符,终端会有”Enter your password:”的提示

  • true作为/bin中的一个二进制文件来实现的,即表示每执行一次便会生成一个进程,若不想则可以通过内建的:命令(其总会返回0)

[root@share codes]# repeat() { while :; do $@ && return; sleep 30; done }
[root@share codes]# repeat wget -c http://www.wenfengbaidu.com/
  • 字段分隔符 IFS
#!/bin/bash -xv

line="shares:x:503:lu,han,lei,wenfeng,yuxuan,ming"
oldIFS=$IFS
IFS=":"
count=0
for item in $line; do
    [ $count -eq 0 ] && user=$item;
    [ $count -eq 3 ] && members=$item;
    if [[ "$members" != "" ]]; then
        IFS=",";
        no=1;
        for name in $members; do
            echo "NO. $no: $name"
        let no++
        done
    fi
    let count++
done
IFS=$oldFIS
echo "This is $user group"
  • 循环语句 for in do done, while do done, until do done 连写成一行时,do前要用;

  • 比较语句 if then fi, if then else if then else fi 连写成一行时,then前用;

    1. [ condition ] && action[ condition ] || action
    2. 算数比较 -eq, -ne, -lt, -ge, -le; [ $var1 -ne 0 -a $var2 -gt 2 ](AND条件用-a,OR条件用-o),也可分开用类似[ ] && [ ]
    3. 文件系统相关测试 -f, -d, -e, -b, -w, -r, -x, -L (用[ ])
    4. 字符串比较 [[ ]] (-z, -n , ==, !=)

知识共享许可协议
SWF's Hacking Dreamonephone 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议进行许可。
© 2011-2024. All rights reserved by onephone. Powerd by Jekyll.