Shell

1、以>改变标准输出
2、以>>附加到文件
3、以| 建立管道

查找和替换

1、正则表达式

image.png

shell 举例

1、test1

  1 #! /bin/bash 
  2 testing=$(date)
  3 echo "The date and time are: " $testing$
root@Syy:~/syy/shellFiles# ./test5
The date and time are:  Mon Aug  3 21:36:04 CST 2020$

2、test2生成日志文件常用的一种技术

  1 #! /bin/bash 
  2 today=$(date +%y%m%d)
  3 ls /usr/bin -al > log.$today

3、通过将输出管道连接到more命令,可以强制输出在一屏数据后停下来

ls -l /usr/bin | more

4、test4

  1 #! /bin/bash 
  2 var1=100
  3 var2=50
  4 var3=45
  5 var4=$[$var1 * (var2 - var3)]
  6 echo $var4 #结果为500

5、test5

  1 #! /bin/bash 
  2 if pwd
  3 then    
  4         echo "it work"
  5 fi   

输出结果为:

root@Syy:~/syy/shellFiles# ./test8
/home/syy/syy/shellFiles
it work

shell执行了if行中的pwd 命令。由于退出状态码是0,它就又执行了then部分的echo语句。
6、test6

#!/bin/bash
# testing multiple commands in the then section
#
testuser=Christine
#
if grep $testuser /etc/passwd
then
echo "This is my first command"
echo "This is my second command"
echo "I can even put in other commands besides echo:"
ls -a /home/$testuser/.b*
fi
$

系统上存在该用户输出结果:

 ./test3.sh
Christine:x:501:501:Christine B:/home/Christine:/bin/bash
This is my first command
This is my second command
I can even put in other commands besides echo:
/home/Christine/.bash_history /home/Christine/.bash_profile
/home/Christine/.bash_logout /home/Christine/.bashrc
$

系统上不存在该用户输出结果:什么都不显示.

if-then-else语句

#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The bash files for user $testuser are:"
ls -a /home/$testuser/.b*
echo
else
echo "The user $testuser does not exist on this system."
echo
fi
#输出结果
$
$  ./test4.sh
The user NoSuchUser does not exist on this system.
$

嵌套if

#!/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.
$
#!/bin/bash
# Testing nested ifs - use elif
#
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."
#
fi
$
#输出结果
$  ./test5.sh
/home/NoSuchUser
The user NoSuchUser does not exist on this system.
However, NoSuchUser has a directory.
$

7、test7


image.png
image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容