# 判断两个变量是否相等if["$var1"="$var2"];thenecho"var1 and var2 are equal"fi# 判断两个变量是否不相等if["$var1"!="$var2"];thenecho"var1 and var2 are not equal"fi# 判断 var1 是否大于 var2if["$var1"-gt"$var2"];thenecho"var1 is greater than var2"fi# 判断 var1 是否小于 var2if["$var1"-lt"$var2"];thenecho"var1 is less than var2"fi# 判断 var1 是否大于等于 var2if["$var1"-ge"$var2"];thenecho"var1 is greater than or equal to var2"fi# 判断 var1 是否小于等于 var2if["$var1"-le"$var2"];thenecho"var1 is less than or equal to var2"fiif[$PF-eq1];thenPF_CMD="--prof"fiif["$RUN"-ne0];thenreturnfi
5. mkdir -p 连接字符串
mkdir-p"${REPO_TOP}"/scripts
6. 是否是文件,是否是文件夹
# 检查是否是文件if[-f"$path"];thenecho"$path is a file"elseecho"$path is not a file"fi# 检查是否是目录if[-d"$path"];thenecho"$path is a directory"elseecho"$path is not a directory"fi
LOCAL_UID=$(grep"$USER" /etc/passwd |awk-F":"'{print $3}')grep"$USER" /etc/passwd: This command searches for the current user's username in the /etc/passwd file. This file contains one line for each user account, with seven fields delimited by colons (:). The first field is the username, and the third field is the user ID.
awk -F ":" '{print $3}': This command processes the output of the grep command. It uses : as the field delimiter (-F ":"), and prints the third field ('{print $3}'), which is the user ID.
LOCAL_UID=$(...): This sets the variable LOCAL_UID to the result of the command inside the $(...).
cd"$FOLDER"||exit1;timeout --preserve-status 40s wget-S\
https://arm.sero.gic.ericsson.se/artifactory/proj-codechecker-release-generic-local/"$CODECHECKER_RELEASE"\-o /dev/stdout 2>&1|grep-E'HTTP/.* 200'> /dev/null 2>&1RES=$?
- `cd"$FOLDER"||exit1;`: This line changes the current directory to the directory specified by the `FOLDER` variable. If the `cd`command fails (for example, if the directory doesn't exist), the script will exit with a status of 1.
- `timeout --preserve-status 40s wget -S ...`: This line runs the `wget` command to download a file from a URL. The `timeout --preserve-status 40s` part means that if the `wget` command takes more than 40 seconds to complete, it will be stopped and the script will continue to the next command. The `-S` option tells `wget` to print the server response.
- `https://arm.sero.gic.ericsson.se/artifactory/proj-codechecker-release-generic-local/"$CODECHECKER_RELEASE"`: This is the URL that `wget` is downloading from. The `CODECHECKER_RELEASE` variable is appended to the end of the URL, so it's likely the name or version of the CodeChecker release that the script is trying to download.
- `-o /dev/stdout 2>&1|grep-E'HTTP/.* 200'> /dev/null 2>&1`: This part redirects the output of the `wget`command to `stdout` and then pipes it to the `grep` command. The `grep`command is looking for a line that matches the regular expression 'HTTP/.* 200', which would indicate a successful HTTP request. The output of the `grep`command is then redirected to `/dev/null`, effectively discarding it.
- `RES=$?`: This line sets the variable `RES` to the exit status of the last command. If the `wget`command was successful, `RES` will be 0. If the `wget`command failed or was stopped by the `timeout` command, `RES` will be a non-zero value.
find.-type f -name"*meson*"-execgrep-l"make"{}\;find.-type f -name"*meson*": 在当前目录(.)及其子目录中查找所有名字包含 "meson" 的文件。-type f 表示只查找文件,不包括目录。
-execgrep-l"make"{}\;: 对每个找到的文件执行 grep-l"make" 命令。grep -l"make" 命令会在文件中查找 "make" 字符串,如果找到,就打印文件名。{} 是 find 命令找到的文件名,\; 表示 -exec 选项的结束。
14. 模糊查找文件名,并查找文件内容
#!/bin/bashmeson_files=$(find.-type f -name"meson.build")forfilein$meson_filesdogrep-q$1$fileif[$?-eq0];thenecho"find $1 in $file"fidone
15. 路径查找并返回
#Move to folder where script is located, to extract path, then move backSCRIPT_DIR="$(dirname "$(readlink -f"$0")")"
pushd "$SCRIPT_DIR" 1>/dev/null || exit
REPO_ROOT="$(realpath ../../)"
popd1>/dev/null ||exit
16. mapfile
# Get list of modified files one commit ahead of master, exclude deleted filesMODIFIED_FILES_STRING=$(gitdiff --name-only --diff-filter=d HEAD~1)# Convert newline separated strings to arraymapfile-t MODIFIED_FILES <<<"$MODIFIED_FILES_STRING"mapfile-t MODIFIED_FILES: The mapfile command, also known as readarray, reads lines from the standard input into an array variable. The -t option tells mapfile to strip a trailing newline from each line read. MODIFIED_FILES is the name of the array variable.
<<<"$MODIFIED_FILES_STRING": This is a here string in bash. It redirects a string into the standard input of a command. "$MODIFIED_FILES_STRING" is a variable that holds the string to be redirected.
17. 查找目录中文件,并复制到其它目录
1 #!/bin/bash
2
3
4 # search from . find $1 file and copy to path $2
5
6 files_=$(find .-type f -name "*$1*")
7
8 for file in $files_
9 do
10 new_name=$(echo$file| sed 's/\//_/g')
11 new_name=$(echo$new_name| sed 's/\.//g')
12 echo$new_name
13 cp-r $file$2"/"$new_name
14 done