Linux命令

mark

基本命令

  • tail命令添加-f参数允许在其他进程使用该文件时查看文件的内容。tail命令会保持活动状态。

  • 变量名、等号和值之间没有空格,如果要赋值一个含有空格的字符串组,必须用单引号来界定字符串的首尾。

  • 删除已经存在的环境变量,可以用unset命令完成。在涉及环境变量名时,需要记住的一点是:如果要用到环境变量,使用$,如果要操作变量,不使用$。一个例外就是使用printenv显示某个变量的值。

  • /etc/profile文件是bash shell默认的主启动文件,只要登录了Linux系统,bash就会执行/etc/profile启动文件中的内容。

  • 用来向Linux系统添加新用户的主要工具是useradd,删除用户使用userdel

  • Vim编辑器中G表示移到最后一行,num G表示移到第num行,gg表示移到第一行。

  • echo命令如果想将文本字符串和命令输出显示在同一行中,可以用echo语句的-n参数。需要在字符串的两侧使用引号,保证要显示的字符串尾部有一个空格。

  • 使用等号将值赋给用户变量,在变量、等号和值之间不能出现空格。

  • 有两种方法可以将命令输出赋给变量

    • 反引号字符(`)
    • $()格式
  • 重定向输出时,>会覆盖已有文件,>>追加数据

  • 输入重定向和输出重定向正好相反,输入重定向将文件的内容重定到命令,而非将命令的输出重定向到文件。输入重定向的符号是<command < inputfile。一个记忆方法是:在命令行上,命令总在左侧,而重定向符号指向数据流动的方向。

  • 使用方括号进行数学运算,在bash中将一个数学运算结果赋值给某个变量时,可以用美元符和方括号($[ operation ])将数学表达式围起来

结构化命令

使用if-then语句

if-then语句是最基本的结构化语句,其格式如下:

if command
then 
    command
fi

bash shell的语句会运行if后面的命令,如果该命令的退出码是0(表示该命令运行成功),位于then部分的命令就会被执行,否则then部分的命令不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束。

使用if-then-else语句

if-then语句中,不管命令是否成功执行,都只有一种选择。如果命令返回一个非零退出状态码,bash shell会继续执行脚本中的下一条命令。在这种情况下,如果能够执行另一组命令就好了,这正是if-then-else语句的作用。

if command
then  
     command
else
     command
fi

if-then相似,区别就是当if语句中的命令返回非零退出状态码时,bash shell会执行else部分中的命令。

嵌套if

有时需要检查脚本代码中的多种条件,可以使用嵌套的if-then语句。嵌套的if-then语句位于主if-then-else语句的else代码块中。

$ ls -d /home/NoSuchUser/
/home/NoSuchUser/
$
$ cat test5.sh
#!/bin/bash
# Testing nested ifs
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
   echo "The user $testuser exists on this system."
else
   echo "The user $testuser does not exist on this system."
   if ls -d /home/$testuser/
   then
      echo "However, $testuser has a directory."
   fi
fi
$
$ ./test5.sh
The user NoSuchUser does not exist on this system.
/home/NoSuchUser/
However, NoSuchUser has a directory.

可以使用else部分的另一种形式:elif。这样就不用书写多个if-then语句了,elif使用另一个if-then语句延续else部分。

if command
then
    command
elif command
then
    more command
fi

elif语句提供了另一个要测试的命令,这类似于原始的if语句行。如果elif后命令的退出状态码是0,则bash会执行第二个then语句部分的命令。

$ cat test5.sh
#!/bin/bash
# Testing nested ifs - use elif & else
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The user $testuser exists on this system."
#
elif ls -d /home/$testuser
then
echo "The user $testuser does not exist on this system."
echo "However, $testuser has a directory."
#
else
echo "The user $testuser does not exist on this system."
echo "And, $testuser does not have a directory."
fi
$
$ ./test5.sh
/home/NoSuchUser
The user NoSuchUser does not exist on this system.
However, NoSuchUser has a directory.
$
$ sudo rmdir /home/NoSuchUser
[sudo] password for Christine:
$
$ ./test5.sh
ls: cannot access /home/NoSuchUser: No such file or directory
The user NoSuchUser does not exist on this system.
And, NoSuchUser does not have a directory.

需要注意的是,在elif语句中,紧跟其后的else语句属于elif代码块。它们并不属于之前的if-then代码块

可以继续将多个elif语句串起来,形成一个大的if-then-elif嵌套组合。

if command1
then
    command set 1
elif command2
then
    command set 2
elif command3
then
    command set 3
elif command4
then
    command set 4
fi

每块命令都会根据命令是否会返回退出状态码0来执行。记住,bash shell会依次执行if语句,只有第一个返回退出状态码0的语句中的then部分会被执行。

Researcher

I am a PhD student of Crop Genetics and Breeding at the Zhejiang University Crop Science Lab. My research interests covers a range of issues:Population Genetics Evolution and Ecotype Divergence Analysis of Oilseed Rape, Genome-wide Association Study (GWAS) of Agronomic Traits.

comments powered by Disqus