Linux shell编程常用语法

Linux shell编程常用语法Linuxshell编程常用语法

大家好,欢迎来到IT知识分享网。Linux shell编程常用语法

1 常用语法
1.1 shell编程空格注意事项
shell 编程空格注意事项
https://blog.csdn.net/codeheng/article/details/51177344?from=timeline&isappinstalled=0

1.2 shell中单中括号与双中括号的区别
1)“[”是一个可执行程序,路径是/usr/bin/[;用来做条件测试
2)“[[”是shell中的关键字,支持正则表达式的条件测试,进行正则匹配时不再是=,而是=~

1.3 把一个语句执行的结果赋值给变量
使用反引号“或者$()

str=`ls`
or
str=$(ls)

注意的是$(())是用来做整数运算的。

1.4 判断返回的字符串是否为空
-n : number,判定字符串非空
if [ -n “$str” ] 当串的长度大于0时为真(串非空)

1.5 shell数组
Shell 数组
https://www.cnblogs.com/cisum/p/8010677.html?from=timeline&isappinstalled=0

1.6 字符串分割为列表
# IFS:Internal Field Seprator,Linux shell内部域分隔符,为系统变量,默认为空格

list=(${line}) # switch str to list
len=${#list[@]} # length of list
len=$((len – 1))
uid_str=${list[0]}

1.7 grep
grep -v abc:反向查找,查找不包含abc字符串的其它内容,排除多个字符串grep -vE “abc|cde”

1.8 shell打印16进制数
[10th-May-2022]
printf ‘%x\n’ val

1.9 Linux schedule policy
# For priority, refer to 5.1 of
# RTLinux和RTOS基本知识
# policy
# 0: SCHED_OTHER
# 1: SCHED_FIFO
# 2: SCHED_RR
pid=1
while [ $pid -le 10000 ]; do
    if [ -f /proc/$pid/comm ]; then
        name=$(cat /proc/$pid/comm)
        policy=$(cat \
            /proc/$pid/task/$pid/sched |\
            grep policy)
        prio=$(cat \
            /proc/$pid/task/$pid/sched |\
            grep prio)
        # switch str to list
        list=(${policy})
        # get the policy
        po_str=${list[2]}
        # switch str to list
        list=(${prio})
        # get the priority
        prio_str=${list[2]}
        if [ “$po_str” = “1” ]; then
            echo “$pid $po_str $prio_str $name”
        elif [ “$po_str” = “2” ]; then
            echo “$pid $po_str $prio_str $name”
        fi
    fi
    pid=$((pid + 1))
done

​​​​​​​2 Linux自动输入密码脚本
#!/usr/bin/expect

set timeout 20
set ID  “xxxxx”
set password “xxxxx”
spawn /usr/bin/ftp 1.1.1.1
expect “Name*: “
send “$ID\n”
expect “Password:”
send “$password\n”
interact

3 modem image
3.1 contents.xml
该文件在私有代码的根目录下,主要定义了升级包中镜像的名字、路径以及相关的打包工具。在common/config目录下,有不同后缀的contents.xml,根据项目需要把对应的xml复制到根目录下,并改名为contents.xml。

QFIL – Tools – Flat Meta Build
读取contents.xml文件,提取所有img并将之打包成Flat或者Meta格式的文件夹。所有img在同一个文件夹下的格式称为Flat,img在独立文件夹中的格式称为Meta。

3.2 sparse image
file system.img
sparse ext2 image,sparse的另外一个意思是稀疏矩阵,for fastboot

file system_1.img
raw ext2 image, for QFIL

QFIL rawprogramX.xml中的physical_partition_number表示UFS的LUN。

3.3 python
Windows Notepad – Save as – Encoding (ANSI)
Windows Notepad – Edit – Replace… – replaces ? with blank

# python pyimg.py 6155_la
import sys,os
from xml.dom.minidom import parse

def readXML(flavor, dnl_file):
    domTree = parse(“./contents.xml”)
    rootNode = domTree.documentElement

    builds_flat = \
        rootNode.getElementsByTagName(
        “builds_flat”)
    builds = \
        builds_flat[0].getElementsByTagName(
        “build”)

    for build in builds:
        name = \
            build.getElementsByTagName(
           “name”)
        download_files = \
            build.getElementsByTagName(
            dnl_file)
        for download_file in download_files:
            file_name = \
            download_file.getElementsByTagName(
            “file_name”)
            file_paths = \
            download_file.getElementsByTagName(
            “file_path”)

            show = 0
            if download_file.hasAttribute(“flavor”) \
                and \
                (download_file.getAttribute(“flavor”)
                == flavor) :
                show = 1

            for file_path in file_paths:
                if file_path.hasAttribute(“flavor”) \
                    and \
                    (file_path.getAttribute(“flavor”)
                         == flavor) and \
                    (file_path.firstChild.data.find(
                        “emmc”) < 0):
                    show = 1
                    break

            if show == 1:
                print(name[0].firstChild.data +
                    “/” +
                    file_path.firstChild.data.rstrip(“/”) +
                    “/” +
                    file_name[0].firstChild.data)

if __name__ == ‘__main__’:
    arg0_proc_name = sys.argv[0]
    if sys.argv[0].rfind(os.path.sep) > 0:
        index = sys.argv[0].rfind(os.path.sep)
        arg0_proc_name = sys.argv[0][index+1:]
    if len(sys.argv) < 2:
        print(“\nUsage: python ” +
            arg0_proc_name +
            ” <flavor>\n”)
        sys.exit(0)

    readXML(sys.argv[1], “download_file”)
    readXML(sys.argv[1], “partition_file”)
    readXML(sys.argv[1], “partition_patch_file”)
    readXML(sys.argv[1], “device_programmer”)

3.4 image
#!/bin/sh

amss_flavors=”660_la”
PRODUCT=”sdm660″
ANDROID_PATH=”/path/to/android”
AMSS_PATH=”/path/to”
AMSS_ANDROID=”${AMSS_PATH}/LINUX”

cd ${AMSS_PATH}
rm -rf ${AMSS_ANDROID}
mkdir -p ${AMSS_ANDROID}
ln -s ${ANDROID_PATH}/ ${AMSS_ANDROID}

# Generate NON-HLOS.bin and spare image
# cd common/build && python update_common_info.py
cd common/build && python build.py –flavors=${amss_flavors} –variant=xxx
cd –

# Check if bin/ is generated
if [ -d “common/build/ufs/${amss_flavors}/bin” ]; then
    ls “common/build/ufs/${amss_flavors}/bin”
else
    echo “Error, no bin generated!”
    exit 0
fi

# Copy files, search keyword LINUX in contents.xml, need to be copied,
# QFIL – Tools – Flat Meta Build
cp_files=”${AMSS_PATH}/common/build/ufs/${amss_flavors}/bin/NON-HLOS.bin
${AMSS_PATH}/common/build/ufs/${amss_flavors}/gpt_main0.bin
${AMSS_PATH}/common/build/ufs/${amss_flavors}/gpt_backup0.bin
${AMSS_PATH}/common/build/ufs/${amss_flavors}/patch0.xml
${AMSS_PATH}/common/build/ufs/${amss_flavors}/rawprogram0.xml
${AMSS_PATH}/xxx/boot_images/QcomPkg/SDMPkg/6150/Bin/AU/RELEASE/prog_firehose_ddr.elf
${AMSS_PATH}/xxx/trustzone_images/build/ms/bin/PAZAANAA/tz.mbn
${ANDROID_PATH}/out/target/product/${PRODUCT}/*.img”

for file in ${cp_files}; do
    echo ${file}
    cp ${file} ${AMSS_PATH}/common/build/ufs/${amss_flavors}/bin/sparse_images
done

# zip
GEN_TIME=$(date +%Y-%m-%d-%H%M%S)
GEN_FILE=FlashPackage_${PRODUCT}_${GEN_TIME}

cd common/build/ufs/${amss_flavors}/bin
zip -r ${GEN_FILE}.zip ./sparse_images
cd –
mv common/build/ufs/${amss_flavors}/bin/${GEN_FILE}.zip ./

echo “${AMSS_PATH}/${GEN_FILE}.zip has been created!”

4 Abbreviations
aop.mbn: always on processor, qcom RPM partition,使用CM4或者RISC-V
cmnlib.mbn: qcom trustzone commonlib, qseecom_sample_client
devcfg.mbn: qcom QUP访问权限控制,SPI片选和时钟线分别对应I2C的SDA和SCL
dmesg:display message
grep:Globally search a Regular Expression(RE)and Print
km41.mbn: qcom keymaster v4.1
lagvm: qcom Linux Android Guest Virtual Machine
MySQL:“My”是因为MySQL发明人Monty的女儿叫做My,而SQL表示Structured Query Language
IFS:Internal Field Seprator,Linux shell内部域分隔符,为系统变量,默认为空格
PS1:Prompt Sign,shell命令提示符
VT100:Video Terminal

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/14098.html

(0)
上一篇 2024-03-02 19:33
下一篇 2024-03-03 22:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信