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. String类方法

    1.charAt(int index)  返回指定索引处的 char 值. 2. length() 返回此字符串的长度. 3.String replace(char oldChar, char new ...

  2. javaWEB国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

    DateFormat:格式化日期的工具类,本身是一个抽象类: NumberFormat:格式化 数字 到 数字字符串,或货币字符串的字符类; MessageFormat: 可以格式化模式字符串,模式字 ...

  3. c3p0数据库连接池(作用不重复)

    /* * c3p0数据库连接池: * 只被初始化一次 * connection对象进行close时,不是正的关闭,而是将该数据连接归还给数据库连接池 * * */ 四个架包 mysql-connect ...

  4. Alice and Bob(贪心HDU 4268)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. MFC编程基础

    http://www.cnblogs.com/lzmfywz/archive/2012/03/15/2399403.html 一.MFC类库概述 MFC(Microsoft Foundation cl ...

  6. hdu 1999 不可摸数 水题。

    不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. LYK 快跑!(run)

    LYK 快跑!(run)Time Limit:5000ms Memory Limit:64MB[题目描述] LYK 陷进了一个迷宫! 这个迷宫是网格图形状的. LYK 一开始在(1,1)位置, 出口在 ...

  8. 自学QT笔记

    前言: Qt 是一个跨平台的 C++图形用户界面库,由挪威 TrollTech 公司于1995年底出品. Trolltech 公司在 1994 年成立,但是在 1992 年,成立 Trolltech ...

  9. CodeForces 670D2 Magic Powder 二分

    D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...

  10. 在JSP页面中输出字符" * "组成的金字塔

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...