I) Linux command
1.1 to give the value, no need space after equality operator
yes:
num2=“W3Cschool”
no:
num2 = “W3Cschool”
1.2 true and false in Linux, which is contrary with C language
In Linux: 0 true 1 false
At C: 0 false 1 true
1.3 while…do : how to use it
#!/bin/sh
int=1
while [ $int -le 5 ]; do
echo $int
let "int++"
done
1.4) how to move file from current directory to the designed/objective directory
[root@iZ2vc5lqzt23aweti4j777Z ~]# pwd
/root
[root@iZ2vc5lqzt23aweti4j777Z ~]# mv posargu.sh /root/April9th2024/
1.5) how to set/modify/create multiple positional auguments.
#!/bin/bash
echo Hello $1 $2
[root@iZ2vc5lqzt23aweti4j777Z April9th2024]# ./posargu.sh Jenny Maria Joe
Hello Jenny Maria
II) Linux Shell
2.1) with or without single quote mark or double quote mark, to define the array element will be agreeable
my_array=(A B 'C' "D弟~~")
echo "第一个元素为: ${my_array[0]}"
echo "第二个元素为: ${my_array[1]}"
echo "第三个元素为: ${my_array[2]}"
echo "第四个元素为: ${my_array[3]}"
echo "elements of array are: ${my_array[*]}"
echo "factors of array are: ${my_array[@]}"
```
## 2.2) 1) name with color is not file; 2) we can use cd folder in Shell
```bash
[root@iZ2vc5lqzt23aweti4j777Z /]# ls cd bin
basename nmcli
bash nm-online
bashbug (letter with color)
[root@iZ2vc5lqzt23aweti4j777Z /]# vim file.sh
cd /bin
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi
if test -e ./basename -a -e ./bash
then
echo '两个文件都存在!'
else
echo '最多存在一个文件!'
fi
```
##2.2) sequence for change modity on file, u/g/o/a+x
chmod +x 和 chmod u+x的区别
chmod a+x 其中加号前面的字母的含义
u 代表用户.
g 代表用户组.
o 代表其他.
a 代表所有.
```bash
[root@iZ2vc5lqzt23aweti4j777Z March31th2024]# chmod x+u add.sh
chmod: invalid mode: ‘x+u’
Try 'chmod --help' for more information.
```
## 2.2) how to comment on bash/linux
```bash
demoFun(){
multile line in a functions, not used by programmer
}
```
## 2.3) how to create and use array
check the manual on two chapters as the one variables and the other function as below:
https://www.w3cschool.cn/linux/linux-shell-variable.html
https://www.w3cschool.cn/linux/linux-shell-func.html
## 2.4) two ways of printing the array factors/elements
```bash
[root@iZ2vc5lqzt23aweti4j777Z ~]# for i in ${array_name[@]}; do echo $i; done
value00
value1
value2
value3
[root@iZ2vc5lqzt23aweti4j777Z ~]# echo "数组的元素为: ${array_name[*]}"
数组的元素为: value00 value1 value2 value3
```
## 2.5) use lowercase for if condition, example ( if [ ${1,,} = herbert ];)
```bash
#!/bin/bash
showname(){
echo hello $1
if [ ${1,,} = herbert ]; then
return 0
else
return 1
fi
}
showname $1
if [ $? = 1 ]; then
echo "Someone unknown called the function!"
fi
~
```
1.8) after and before [], there should be space
```bash
#! usr/bin/bash
echo "What is your name?"
read name
if [ $name ]; then
echo "$name sounds alright to me"
else
echo "Doesn't sound like anything to me"
fi
~
```
2.5) commands on : curl and find
```bash
[root@iZ2vc5lqzt23aweti4j777Z ~]# curl -I http://example.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 361170
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 21 Apr 2024 14:24:51 GMT
Etag: "3147526947"
Expires: Sun, 28 Apr 2024 14:24:51 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (sac/254E)
X-Cache: HIT
Content-Length: 1256
[root@iZ2vc5lqzt23aweti4j777Z ~]# find /home -name "*.txt"
[root@iZ2vc5lqzt23aweti4j777Z ~]# cd /home
[root@iZ2vc5lqzt23aweti4j777Z home]# ll
total 4
drwxr-xr-x 2 root root 4096 Apr 20 19:53 www
[root@iZ2vc5lqzt23aweti4j777Z home]# cd ..
[root@iZ2vc5lqzt23aweti4j777Z /]# find /home -name "www"
/home/www
[root@iZ2vc5lqzt23aweti4j777Z /]#
```