1. 变量与替换

#!/bin/bash

# 变量替换
# 另外, 变量替换还有许多别的语法
# 例如, b=${a/23/bb} 将 23 替换成 bb 等等, 用到时再找
a=375
hello=$a # 注意空格
# 如果变量中有空格, 那么变量就需要被双引号括起来
hello="a b c d"
echo $hello # 输出 a b c d
echo ${hello} # 输出 a b c d
echo "$hello" # 输出 a b c d
# ------------------------------------------------- # 变量赋值
# 变量什么时候是"裸体"的即前边没有变量符号, 当它被赋值的时候,
#+而不是被引用的时候. a=123
# 使用 let 赋值
let a=16+5
# 最好将 let 形式写为如下格式, 因为这样的格式会显示赋值语句是一个整体
let "a=16+5"
echo $a # 显示结果是21
# 如果不使用let, 直接 a=16+5, 那么echo $a 就是 16+5
# 在 for 循环中的变量赋值
for a in 7 8 9 10
do
echo -n "$a"
done
echo
echo
# 特殊的变量赋值, 把命令的结果赋值给变量
a=`ls -l`
echo $a
echo "$a"
# 与上边的 `` 含义一样
a=$(uname -m)
echo $a exit 0

2. 个人推荐的测试结构 test

#!/bin/bash

# [] 单中括号
if [ $UID -ne 0 ]
then
echo "This is not root."
fi # [[]] 双中括号
if [[ $comparison = "integer" ]]
then
echo "test"
fi # 注意, 使用[[]], 而不是[], 能够阻止脚本中许多逻辑错误, 比如, 尽管在[]中将给出一个错误,
#+但是, &&,||,<>操作还是能够工作在一个[[]]中. # 算数测试使用 (())
((0)) # 返回 1
((1)) # 返回 0, 这才是真
(( 5 > 4 )) # 返回 0, 这才是真 # 比较这里有很多关键参数, 比如 -e, -eq 等, 用时再确认 # 与或非 下边的等价写法
[[ condition1 && condition2 ]]
if [ "$exp1" -a "$exp2" ]
fi exit 0

3. 个人推荐的算术表达式

#!/bin/bash

n=100

let "n++"  # 101
echo $n
((n++)) # 102
echo $n exit 0

4. 清除目录下的文件

#!/bin/bash

# clean up version 1
# ---------------------------------------------------------------------------------
cd /tmp/log
cat /dev/null > testfile echo "Log clean up." # clean up version 2
# ---------------------------------------------------------------------------------
LOG_DIR=/tmp/log
cd $LOG_DIR
cat /dev/null > testfile echo "Log clean up." exit #clean up version 3
# ---------------------------------------------------------------------------------
LOG_DIR=/tmp/log
ROOT_UID=0
LINES=10
E_XCD=66
E_NOTROOT=67 if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
else
echo "Yes, you are the god root."
fi if [ -n "$1" ]
then
lines=$1
else
lines=$LINES
fi # E_WRONGAGRS=65
# case "$1" in
# "") lines=50;;
# *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGAGRS;;
# *) lines=$1;; cd $LOG_DIR if [ `pwd` != "$LOG_DIR" ]
then:
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # 更有效的实现上边的内容.
# cd /tmp/log || {
# echo "Cannot change to necessary directory." >&2
# exit $E_XCD;
# } tail -$lines testfile > mesg.temp
mv mesg.temp message echo "Logs cleaned up." exit 0

5. 个人推荐的逻辑 与, 或

#!/bin/bash

# 逻辑与
if [ condition1 ] && [ condition2 ]
fi
if [[ condition1 && condition2 ]]
fi # 逻辑或
if [ condition1 ] || [ condition2 ]
fi
if [[ condition1 || condition2 ]]
fi

6. 最大公约数

#!/bin/bash

# 最大公约数

ARGS=2
E_BADARGS=65 # 参数检查
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` first-number second-number"
exit $E_BADARGS
fi gcd()
{
dividend=$1
divsor=$2 remainder=1 until [ "$remainder" -eq 0 ]
do
let "remainder=$dividend%$divsor"
dividend=$divsor
divsor=$remainder
done
} # 另外, 有关函数调用
#+上边定义了一个函数, 然后下边调用, 将参数传递进去的办法如下
gcd $1 $2
echo; echo "GCD of $1 and $2 = $dividend"; echo exit 0

一些比较好的shellscript脚本的更多相关文章

  1. ShellScript值传递参数

    Shell传递参数 ######################################摘自菜鸟教程:http://www.runoob.com/linux/linux-shell-passi ...

  2. Linux第02天

    Linux 第02天 1.Linux磁盘和文件系统 VFS————虚拟文件系统 df命令————查看已挂载的分区 df 分区名 du命令————查看文件夹大小 du 文件夹名 ln命令————符号链接 ...

  3. Linux 总结1

    ============================= 一般 ========================================= chown -R oracle:oinstall ...

  4. Linux 总结2

    cd                                             pwd                                            mkdir ...

  5. Ansible学习 ad-hoc命令

    Ansible提供两种方式去执行命令,一种是ad-hoc命令,一种是写入Ansible playbook.类似于前者在命令行敲shell,后者是写shell-script脚本,前者解决一些简单的任务, ...

  6. Linux&shell之高级Shell脚本编程-创建函数

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用函数 #!/bin/bash # testing the script function myfun { echo "This i ...

  7. shell 编程基础(1)---初识shellscript

    shellscript 是linux下强大的系统管理工具,可以通过bash命令和管道命令直接在linux系统上进行编程,所写的脚本不需要编译就可以执行,对于系统管理而言十分方便. #!/bin/bas ...

  8. Linux&shell之Shell脚本

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用多条命令shell脚本的关键是可以输入多条命令,甚至可以将一条命令的结果传递给另一条命令.date;who(两个命令shell脚本的写法) ...

  9. Linux&shell之如何控制脚本

    写在前面:案例.常用.归类.解释说明.(By Jim) Ctrl+C组合键可以生产SIGINT信号Ctrl+Z组合键生产SIGTSTP信号,停止进程后程序仍然留在内存中,能够从停止的地方继续运行. 捕 ...

随机推荐

  1. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

  2. set_include_path — 设置 include_path 配置选项为当前脚本设置 include_path 运行时的配置选项。

    说明 string set_include_path ( string $new_include_path ) 为当前脚本设置 include_path 运行时的配置选项. 参数 new_includ ...

  3. greenplum集群安装

    一.环境配置 1.地址分配 192.168.1.201 mdw master 192.168.1.202 sdw1 segment1 192.168.1.203 sdw2 segment2 2.创建用 ...

  4. ACM题目————Team Queue

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  5. sql截断日志

    --收缩数据库 DBCC SHRINKDATABASE(fas) --截断事务日志: BACKUP LOG fas WITH NO_LOG 1.清空日志 DUMP TRANSACTION 库名 WIT ...

  6. Socket状态变迁图

    在一些防火墙或端口管理工具中经常会看到连接状态为CLOSED CLOSE_WITE LAST_ACK等的进程, 虽然状态就那么很少的几个, 而且看字面意思也能猜个大概, 但没做过Socket编程的朋友 ...

  7. 2016年12月16日 星期五 --出埃及记 Exodus 21:11

    2016年12月16日 星期五 --出埃及记 Exodus 21:11 If he does not provide her with these three things, she is to go ...

  8. CentOS6 启动流程图文解剖

    我们在使用Linux操作系统的时候,我们只需按下电源键,等待,然后输入账户和密码就可以使用Linux操作系统了.那么在按下电源到输入账号和密码之前,操作系统都做了些什么?下面就来讲述在这段时间发生的动 ...

  9. IOS文字属性备注

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...

  10. 【MySQL】过滤后的结果集较大,用LIMIT查询分页记录,查询效率不理想

    > 参考的优秀文章 优化LIMIT分页--<高性能MySQL>(电子工业出版社) > 场景描述 遇到一个场景:查询排序后的结果集较大,我们采用分页显示,每页显示20条记录,但是 ...