Open AndreGeng opened 5 years ago
echo -n 'hi there' // 输出信息但不换行
myvar=123
today=$(date +%y%m%d)
ls /usr/bin -al > log.$today
wc << EOF
test string 1
test string 2
EOF
以上命令输出结果为:
2 6 28
ps: wc对数据中的文本进行计数,默认情况下它输出三个值
- 文本的行数
- 文本的词数
- 文本的字节数
if command; then
commands
fi
或者
if command then
commands
fi
if command then
commands
else
commands
fi
if command1; then
commands
elif command2; then
more commands
fi
if test $my_variable; then
echo "The $my_variable expression returns a True"
else
echo "The $my_variable expression returns a False"
fi
或者
if [$my_variable]; then
echo "The $my_variable expression returns a True"
else
echo "The $my_variable expression returns a False"
fi
if (( a > b )); then
...
fi
或者(不如上面的功能强大)
if [ "$a" -gt "$b" ]; then
...
fi
ps: 用中括号时,注意两边要有空格的.
str = str1 // 相等
str != str2 // 不相等
-n str // str长度是否为0
-z str // str长度是否为非0
if [ $val1 \> $val2 ]; then
echo "$val1 is greater than $val2"
fi
// 双括号提供了test不支持的特性:正则表达式 if [[ $USER == r* ]]; then echo "Hello $USER" else echo "Sorry, I do not know you" fi
- 文件比较
-d file // 文件是否存在并且为目录 -e file // 文件是否存在 -f file // 文件是否存在且为文件 -r file // 文件是否存在且可读 -s file // 检查file是否存在并非空 -w file // 检查file是否存在并可写 -x file // 检查file是否存在并可执行 -O file // 检查file是否存在并属当前用户所有 -G file // 检查file是否存在并且默认组与当前用户相同 file1 -nt file2 // 检查file1是否比file2新 file1 -ot file2 // 检查file1是否比file2旧
- 复合条件测试
if [ -d $HOME ] && [ -w $HOME/testing ]; then echo "The file exists and you can write to it" else echo "I cannot write to the file" fi
- case命令
// 语法 case variable in pattern1 | pattern2) commands1;; pattern3) commands2;; *) default commands;; esac
// e.g. case $USER in rich | barbara) echo "Welcome, $USER" echo "Please enjoy your visit";; testing) echo "Special testing account";; jessica) echo "Do not forget to log off when you're done";; *) echo "Sorry, you are not allowed here";; esac
## 循环
- for
for test in Nevada "New Hampshire" "New Mexico" "New York" do echo "Now going to $test" done
// 在Linux中,目录名和文件名中包含空格当然是合法的。要适应这种情况,应该将$file变 量用双引号圈起来。如果不这么做,遇到含有空格的目录名或文件名时就会有错误产生:./test6: line 6: [: too many arguments。 for file in /home/rich/test/* do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fi done
// 更改字段分隔符, 默认字段分隔符为: 空格,制表符,换行符 file="states" for state in $(cat $file) do echo "Visit beautiful $state" done // 指定多个字段分隔符 IFS=$'\n':;"
// c语言风格的for for (( a=1, b=10; a <= 10; a++, b-- )) do echo "$a - $b" done
- while
while test command; do other commands done
- until
until [ $var1 -eq 0 ]; do echo $var1 var1=$[ $var1 - 25 ] done
- 循环处理文件数据
IFS.OLD=$IFS IFS=$'\n' for entry in $(cat /etc/passwd); do echo "Values in $entry –" IFS=: for value in $entry; do echo " $value" done done
- break && continue
// break/continue 后面也可以跟要跳出的循环层级,e.g. break 2 //表示跳出两层循环,默认情况下是break 1 for (( var1 = 1; var1 < 15; var1++ )); do if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]; then continue fi echo "Iteration number: $var1" done
- 处理循环的输出
for (( a = 1; a < 10; a++ )) do echo "The number is $a" done > test23.txt
- 实例
input="users.csv" while IFS=',' read -r userid name do echo "adding $userid" useradd -c "$name" -m $userid done < "$input"
# 处理用户输入
Shell基础
子shell
shell内部命令与外部命令
Linux 环境变量
Linux账户
usermod -G GROUPNAME USERNAME // 把USERNAME添加天GROUPNAME的组中
umask // 0022, umask里面存储的为掩码, 对文件来说,全权限的值是666(所有用户都有读 和写的权限);而对目录来说,则是777.
举例来说,对umask值为0022的系统, 创建文件时,默认权限为644。创建目录时,默认权限为755
ps: umask第一位的值为粘着位(sticky bit)
chmod 777 FILE chmod u+x FILE // u: 用户,g: 组,o: 其它用户, a: 以上所有
chown USERNAME FILENAME
mkdir testdir chgrp shared testdir // 把testdir的组设为shared chmod g+s testdir // 给组设置sgid, 这样可以强制文件夹和子文件都属于shared组
aptitude search package_name // 软件包前显示i, 表示已安装 aptitude install package_name aptitude search package_name // 确认下是否安装成功 aptitude safe-upgrade // 更新软件包 aptitude purge package_name // 删除软件包
添加aptitude仓库, 编辑/etc/apt/sources.list文件来添加仓库
yum list installed // 列出已安装包 yum list installed xterm // xterm是否安装 yum provides file_name // 查看系统上特定文件属于哪个软件包 yum install package_name // 安装软件 yum localinstall package_name.rpm // 安装手动下载的包 yum list updates // 列出可更新的包 yum update package_name // 更新包 yum remove package_name // 删除软件包,但保留相关配置 yum erase package_name // 删除软件和它的所有文件
// 处理损坏的包依赖关系 yum clean all yum deplist package_name yum update --skip-broken
yum repolist // 列出yum的软件仓库地址 yum的仓库定义文件位于 /etc/yum.repos.d
tar -zxvf sysstat-11.1.1.tar.gz cd sysstat-11.1.1 ./configure make make install