Linux 的基本语法
1. cp 命令
功能: 将给出的文件或目录拷贝到另一文件或目录中
语法: cp[选项] 源文件或目录 目标文件或目录
2. mv 命令
功能: 为文件或目录改名或将文件由一个目录移入到另一个目录
语法: mv[选项] 源文件或目录 目标文件或目录
3. ls 命令
功能: 列出目录的内容
语法: ls[选项] 目录或文件
4. rm 命令
功能: 删除不需要的文件或目录
语法: rm[选项] 文件
5. su 命令
功能: 它可以让一个普通用户拥有超级用户或其他用户的权限, 也可以让超级用户以普通用户的身份做一些事情. 普通用户使用这个命令时必须有超级用户或其他用户的口令.
语法: su[选项][使用者账号]
6. cd 命令
功能: 改变工作目录
语法: cd[directory]
7. ps 命令
功能: 显示系统中执行的程序
语法: ps[选项]
8. echo 命令
功能: 在显示器上显示一段文字, 一般起一个提示作用
语法: echo [-n] 字符串
9. clear 命令
功能: 清除屏幕上的信息
以下为一些基本语法的例子:
1. for 循环
- cat>mv.file
- echo please type in the directory path
- read path
- for file
- in fil1 fil2 fil3
- do
- mv $file $path/$file
- done
2. if 结构
- cat>search
- echo type in the Word and the file name
- read Word file
- if grep $Word $file
- then
- echo $Word is in $file
- fi
3. until 循环
- cat>parg
- number=0
- until [ $# = 0 ]
- do
- number=`expr $number + 1`
- echo argument $number is $1
- shift
- done
4. while 循环
- cat>enter.name
- echo "please type in each person's name and then a <CR>"echo"please end the list of name with <Ctrl-d>"
- while
- read x
- do
- echo $x>>xfile
- done
- echo xfile contains the following name
- cat xfile
5. 位置参数
- cat>pp
- echo 1 is:$1
- echo 2 is:$2
- echo 3 is:$3
- echo 4 is:$4
- echo 5 is:$5
- echo 6 is:$6
- echo 7 is:$7
- echo 8 is:$8
- echo 9 is:$9
- 6. psubs
- cat>psubs
- dir=${
- 1:-$HOME
- }
- echo dir=$dir
- echo 2=${
- 2:? "positional parameter must be set"
- }
- tmp=/tmp
- t=${
- tmp:+"a legal value"
- }
- echo t=$t
- f=${
- fred:+ nothing
- }
- echo f=$f
- dir1=${
- dir:=same
- }
- dir2=${
- dir3:=$PATH
- }
- echo dir1 is $dir1
- echo dir2 is $dir2
- echo dir3 is $dir3
- 7. svtset
- cat>svtest
- echo the name of this program is: $0
- echo the arguments are $*
- echo the number of arguments is: $#
- date&
- echo process id of the date command was: $!
- ls
- echo the process id of this shell is: $$
- grep root /etc/passwd
- echo return code from grep was: $?
8. test 命令
类 测试符 功能
数 -eq 等于
值 -ne 不等于
测 -gt 大于
试 -ge 大于或等于
-lt 小于
-le 小于或等于
字 = 相等
符 != 不相等
串 -z 字符串 长度为零
-n 字符串 长度不为零
文 -r 文件名 如果文件存在并且可读
件 -w 如果文件存在并且可写
测 -x 如果文件存在并且可执行
试 -s 如果文件存在并且至少有一个字符
-d 如果文件存在并且为目录
-f 如果文件存在并且不为目录
-c 如果文件存在并且为字符型文件
-b 如果文件存在并且为块型文件
- cat>check.dir
- test -d $1 && echo "$1 is dictory" && exit 0
- echo "$1 is not dictory"
- exit 1
- Linxu
来源: http://www.bubuko.com/infodetail-2866730.html