---
title: "ubuntu-base-commands"
author: "leibnitz"
date: "2011-04-03T02:51:40Z"
tags: ["Bash", "Linux", "Ubuntu", "正则表达式", "脚本"]
categories: ["非技术"]
slug: "ubuntu-base-commands"
draft: false
lastmod: "2011-04-03T02:51:40Z"
id: 985559
url: "https://leibnitz.iteye.com/blog/985559"
---

references:  
http://home.lupaworld.com/home-space-uid-94908-do-blog-id-32301.html  
http://sucre.iteye.com/blog/587673

Linux中有许多种不同的shell，通常我们使用bash (bourne again shell) 进行shell编程，因为bash不仅免费（自由）且易于使用  
#!/bin/sh 符号#!用来告诉系统执行该脚本的程序,（必须放在文件的第一行）  
所有变量都由字符串组成，并且不需要声明。  
hmod +x filename # 使其可执  
防止混乱  
echo "this is the ${num}nd"

在shell脚本中可以使用三类命令：  
1\) Unix 命令:  
#vvvvvv 以下命令可以--help查看具体细节 vvvvv  
wc –l file wc -w file wc -c file: 分别计算文件的行数（line）、单词数（word,以空格为分格）和字符数（character）  
grep 'pattern' file: 在文件内搜索字符串或和正则表达式匹配的字符串,pattern前后引号可用双引号,或去掉;  
cut -c column file: 将指定范围内的文件内容输出到标准输出设备（屏幕）上。比如：输出每行第5至9个字符 cut -b5-9 file.txt  
cat file.txt: 输出文件内容到标准输出设备（屏幕）上

file somefile: 取得文件somefile的文件类型

read var: 提示用户输入，并将输入内容赋值给变量var;可以通过$var取得,如echo $var

sort file.txt: 对file.txt文件所有行进行排序,对于非字符将去掉再排序;  
uniq: 只输出文件中内容不一致/重复的行，如： sort file.txt | uniq;uniq file  
expr: 进行数学运算，如要进行2+3的运算，命令为: expr 2 '+' '3'  
find: 搜索文件，如根据文件名搜索：find . -name &lt;filename&gt; -print  
tee: 将数据输出到标准输出设备(屏幕) 和文件，相当生成新的文件,比如：somecommand | tee outfile,如sort a.txt | tee b.txt  
basename file: 返回不包含路径的文件名，如： basename /bin/tux 会返回 tux  
dirname file: 返回文件所在路径，如：dirname /bin/tux 会返回 /bin  
head \[-n N] file: 打印文本文件开头几行,如果提供-n则打印头几行而不是默认10行;如果-N则不打印最后N行;  
tail file : 打印文本文件末尾几行;同上

awk: awk 用来提取文本文件中的字段(不同于cut)。缺省的字段分割符是空格，可以使用 -F 指定其它分割符。cat file.txt | awk -F, '{print $1 "," $3 }'，  
这里我们使用 , 作为字段分割符，同时打印第一和第三个字段。如果该文件内容为 Adam Bor, 34, IndiaKerry Miller, 22, USA，  
则上述命令的输出为：Adam Bor, IndiaKerry Miller  
sed(StreamEDitor):为UNIX系统上提供将编辑工作自动化的编辑器(command),使用者无需直接编辑资料。

2\) 概念  
管道 (|) 将一个命令的输出作为另外一个命令的输入。  
 grep "hello" file.txt | wc -l    #可以连续书写多个类似命令  
重定向：将命令的结果输出到文件，而不是标准输出（屏幕）。  
代码:  
　　&gt; 写入文件并覆盖旧文件  
　　&gt;&gt; 加到文件的尾部，保留旧文件内容。  
反引号(backtick,即"\`")  
　　使用反短斜线可以将一个命令的输出作为另外一个命令的一个命令行参数。  
 tar -zcvf lastmod.tar.gz \`find . -mtime -1 -type f -print\`    #1小时内修改的文件打包

3\) 流程控制  
查看更多测试表达式:man test  
if ....; then  
　 ....  
elif ....; then  
　 ....  
else  
　 ....  
fi

case表达式可以用来匹配一个给定的字符串，而不是数字。  
case ... in  
...) do something here ;; #must use double ';'  
esac  
e.g.:  
#!/bin/sh  
ftype=\`file "$1"\`    #$1代表命令行输入的第一个参数   
echo $ftype  
case "$ftype" in  
"$1: Zip archive"\*)    #匹配模式,代表以$1: Zip archive开头的字符串  
unzip "$1" ;;  
"$1: gzip compressed"\*)  
  gunzip "$1";;  
"$1: bzip2 compressed"\*)  
bunzip2 "$1" ;;  
\*) echo "File $1 can not be uncompressed with smartzip";;  
esac

(no this command in ubuntu10)  
select var in ... ; do  
　break;  
done  
.... now $var can be used ....

while ...; do  
   ....(or continue,break)  
done

for var in ....; do  
   $var can be used....  
done

引号  
在向程序传递任何参数之前，程序会扩展通配符和变量。这里所谓的扩展是指程序会把通配符（比如\*）替换成适当的文件名，把变量替换成变量值。我们可以使用引号来防止这种扩展  
a.引号（单引号和双引号）可以防止通配符\*的扩展,通配作普通字符看待  
b.其中单引号更严格一些，它可以防止任何变量扩展；而双引号可以防止通配符扩展但允许变量扩展(变量优先,没有则以普通字符看待)

当要将几行文字传递给一个命令时，用here documents是一种不错的方法  
比如对每个脚本写一段帮助性的文字是很有用的，此时如果使用here documents就不必用echo函数一行行输出。  
here document(use for comment)  
cat &lt;&lt; flag comment-to-write.....  
flag

\[编辑] Shell里的函数  
functionname()  
{  
\# inside the body $1 is the first argument given to the function  
\# $2 the second ...  
body  
}  
#later usage:functionname (note:no '()' ended with functionname)

sh -x strangescript  
上述命令会执行该脚本，同时显示所有变量的值。

sh -n your\_script  
这个命令会返回所有语法错误。