Bo's Oracle Station

查看: 2289|回复: 0

Bash Shell编程

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2019-6-30 21:26:05 | 显示全部楼层 |阅读模式
  1. #!/bin/sh
  2. read -p "Please input number1: " V_NUM1
  3. read -p "Please input number2: " V_NUM2
  4. bc <<EOF
  5. scale=10
  6. $V_NUM1/$V_NUM2
  7. quit
  8. EOF
复制代码

  1. SUB_BC() {
  2. read -p "Please input number1: " V_NUM1
  3. read -p "Please input number2: " V_NUM2
  4. bc <<-EOF
  5.   scale=10
  6.   $V_NUM1/$V_NUM2
  7.   quit
  8. EOF
  9. }

复制代码

  1. #!/bin/sh
  2. . ./2.func

  3. SUB_BC
复制代码

了解变量,和存储位置参数的变量:
  1. SUB_A() {
  2.   echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
  3.   echo $*
  4.   echo $#
  5.   echo $?
  6. }
复制代码

  1. #!/bin/sh

  2. . ./5.func

  3. SUB_A 1 2 3 4 5 6 7 8 9 a
复制代码
[root@server3 labs]# ./5.sh
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 a
10
0

  1. SUB_A() {
  2.   echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
  3.   echo $*
  4.   echo $#
  5.   echo $?
  6. }
复制代码
[root@server3 labs]# ./5.sh
1 2 3 4 5 6 7 8 9 a
1 2 3 4 5 6 7 8 9 a
10
0

------------------------------------------------------

$() 会自动把输出中的回车,自动替换成空格:








[root@server3 labs]# seq 100  -2 0
100
98
96
94
92
90
88
86
84
82
80
78
76
74
72
70
68
66
64
62
60
58
56
54
52
50
48
46
44
42
40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
2
0
[root@server3 labs]# A=$(seq 100  -2 0)
[root@server3 labs]# echo $A
100 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0
[root@server3 labs]#

  1. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  2. 36
  3. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  4. 14
  5. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  6. 58
  7. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  8. 27
  9. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  10. 46
  11. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  12. 18
  13. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  14. 6
  15. [root@server3 labs]# echo $[ $RANDOM % 60 +1 ]
  16. 21
复制代码
  1. [root@server3 /]# for i in * ; do du -s  $i  ; done |sort -n
  2. du: cannot access ‘proc/68253/task/68253/fd/3’: No such file or directory
  3. du: cannot access ‘proc/68253/task/68253/fdinfo/3’: No such file or directory
  4. du: cannot access ‘proc/68253/fd/4’: No such file or directory
  5. du: cannot access ‘proc/68253/fdinfo/4’: No such file or directory
  6. 0       bin
  7. 0       dev
  8. 0       lib
  9. 0       lib64
  10. 0       media
  11. 0       nfsserver
  12. 0       opt
  13. 0       proc
  14. 0       sbin
  15. 0       srv
  16. 0       sys
  17. 4       mnt
  18. 20      home
  19. 564     tmp
  20. 2356    lost+found
  21. 11128   run
  22. 48624   etc
  23. 107620  root
  24. 148236  boot
  25. 1113676 var
  26. 3683376 usr
  27. [root@server3 /]#
复制代码
-------------------------------------------------------------------------
事件观察者:
  1. #!/bin/sh

  2. echo "Waiting for the file: "
  3. until [ -f ./6.txt  ]
  4. do
  5. sleep 1
  6. done

  7. echo "The file exists."
复制代码

  1. #!/bin/sh

  2. echo "Waiting for the file: "
  3. while [ ! -f ./7.txt  ]
  4. do
  5. sleep 1
  6. done

  7. echo "The file exists."

复制代码
1.sh :

  1. #!/bin/sh

  2. read -p "Please input color: " V_C

  3. case "$V_C" in
  4. r)
  5. echo "redhat"
  6. ;;
  7. g)
  8. echo "green"
  9. ;;
  10. *)
  11. echo "other color"
  12. ;;
  13. esac

复制代码
2.sh
  1. #!/bin/sh

  2. if [ $# -eq 0 ]
  3. then
  4. echo "No argument"
  5. exit 1
  6. fi

  7. if [ ! -f $1 ]
  8. then
  9. echo "No file"
  10. exit 2
  11. fi

  12. V_F=$(cat $1)

  13. for i in $V_F
  14. do
  15. echo "useradd -s /sbin/nologin $i"
  16. done

复制代码


























回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-12-4 01:19 , Processed in 0.040753 second(s), 25 queries .

快速回复 返回顶部 返回列表