向bash脚本添加参数#
basic#
─ ~/shellTest ly@vmmin 10:37:24
╰─❯ cat ./16myscript_cls.sh
#!/bin/bash
echo "You entered the argument: $1,$2,$3, and $4."结果
╭─ ~/shellTest 16s ly@vmmin 10:37:18
╰─❯ ./16myscript_cls.sh Linux1 Linux2
You entered the argument: Linux1,Linux2,, and .示例1#
╭─ ~/shellTest ly@vmmin 10:41:45
╰─❯ cat ./16myscript_cls.sh
#!/bin/bash
ls -lh $1
#echo "You entered the argument: $1,$2,$3, and $4."
╭─ ~/shellTest ly@vmmin 10:41:28
╰─❯ ./16myscript_cls.sh /etc
total 792K
-rw-r--r-- 1 root root 3.0K May 25 2023 adduser.conf
-rw-r--r-- 1 root root 44 Dec 17 15:26 adjtime
-rw-r--r-- 1 root root 194 Dec 23 22:38 aliases
drwxr-xr-x 2 root root 4.0K Dec 23 22:38 alternatives
drwxr-xr-x 2 root root 4.0K Dec 17 15:24 apparmor
drwxr-xr-x 8 root root 4.0K Dec 17 15:25 apparmor.d
drwxr-xr-x 9 root root 4.0K Dec 17 15:30 apt
-rw-r----- 1 root daemon 144 Oct 16 2022 at.deny
-rw-r--r-- 1 root root 2.0K Mar 30 2024 bash.bashrc示例2#
#!/bin/bash
lines=$(ls -lh $1 | wc -l) #行计数
echo "You hava $(($lines-1)) objects in the $1 directory."
#$(($lines-1))这里用到了子shell
#echo "You entered the argument: $1,$2,$3, and $4."╭─ ~/shellTest ly@vmmin 10:48:06
╰─❯ ls -lh logfiles
total 12K
-rw-r--r-- 1 ly ly 0 Dec 22 23:07 a.log
-rw-r--r-- 1 ly ly 120 Dec 22 23:17 a.log.tar.gz
-rw-r--r-- 1 ly ly 0 Dec 22 23:07 b.log
-rw-r--r-- 1 ly ly 121 Dec 22 23:17 b.log.tar.gz
-rw-r--r-- 1 ly ly 0 Dec 22 23:07 c.log
-rw-r--r-- 1 ly ly 121 Dec 22 23:17 c.log.tar.gz
-rw-r--r-- 1 ly ly 0 Dec 22 23:15 xx.txt
-rw-r--r-- 1 ly ly 0 Dec 22 23:15 y.txt
╭─ ~/shellTest ly@vmmin 10:48:10
╰─❯ ./16myscript_cls.sh logfiles
You hava 8 objects in the logfiles directory.head,表示前十行,可以看出total这些被算作一行了,所以上面的shell中-1








