0
Follow
0
View

linux shell script

dayin781m 注册会员
2023-02-27 19:39

This answer quotes ChatGPT

In your script, you call a function CH() to check if the filename exists or is empty. This function returns 1 if the filename exists or is empty, and 0 otherwise. The return value of the function is then checked after the function call to determine what to do next.

However, in your script, you check the return value of the function twice. The first is in the if statement, which checks whether the function returns a value of 1. If the function returns a value of 1, print "The filename you entered exists or the input is empty, please retype!" "Error message. This part of the code works fine.

However, in the second if statement, you double check the return value of the function. This if statement has nothing to do with the if statement above; it simply checks whether the function returns a value of 0. If the function returns 0, it prints "The FILENAME you entered, ${FILENAME}, has been created!" And create a new file. However, since you check the return value of the function twice after the call, the condition in the second if statement is never satisfied. Therefore, if you use an elif statement, it will never execute the block of code in it.

You can solve this problem by putting the if statement on the same line as the code that created the file, so you don't need to check the return value of the function twice. For example, you could write:

if CH; then echo -e "\e[33m您输入的文件名${FILENAME}已经创建!\e[0m" && touch /mnt/${FILENAME}; else echo -e "\e[33m您输入的文件名存在或输入为空,请重新输入!\e[0m"; fi

The

line first calls the CH() function and checks its return value. If the return value is 0, print "The file name you entered ${FILENAME} has been created!" And create a new file. Otherwise, it will print "Filename you entered exists or input is empty, please retype!" The news of

Modified code

#!/bin/bash

CH() {
    read -p "请输入文件名:" FILENAME
    if [ -z "${FILENAME}" ]; then
        echo -e "\e[33m输入内容为空或文件名不存在!\e[0m"
        return 1
    elif [ -e "${FILENAME}" ]; then
        echo -e "\e[33m输入内容为空或文件名已存在!\e[0m"
        return 1
    else
        echo -e "\e[33m输入的文件名${FILENAME}已经创建!\e[0m"
        touch /mnt/"${FILENAME}"
        return 0
    fi
}

if CH; then
    echo -e "\e[33m文件创建成功!\e[0m"
else
    echo -e "\e[33m文件创建失败!\e[0m"
fi

dian19920226 注册会员
2023-02-27 19:39

djttokyo 注册会员
2023-02-27 19:39

$? It's just the result of the previous command, and if it's correct, it returns 0, if it's wrong, it returns 2, and your if should overwrite the previous value as soon as it's executed. So elif was never satisfied. Modified code:

#!/bin/bash
CH(){
read -p "请输入文件名:" FILENAME
if [ -e "${FILENAME}" ];then
        return 1
elif [ -z "${FILENAME}" ];then
        return 1
else
        return 0
fi
}
CH

result=$?

if [ "$result" == "1" ];then
        echo -e "\e[33m您输入的文件名存在或输入为空,请重新输入!\e[0m"
elif [ "$result" == "0" ];then
        echo -e "\e[33m您输入的文件名${FILENAME}已经创建!\e[0m"
        touch /mnt/${FILENAME}
fi

If it helps, please click to accept ~

lwaooo0777 注册会员
2023-02-27 19:39

if ["$?" == "1"] is called several times in the judgment statement, each call will overwrite the previous return value. Therefore, the return value of the second call is 0 and cannot be determined.
I modified it for your reference:

#!/bin/bash
CH(){
read -p "请输入文件名:" FILENAME
if [ -e "${FILENAME}" ];then
RET=1
elif [ -z "${FILENAME}" ];then
RET=1
else
RET=0
fi
}
CH
if [ "$RET" == "1" ];then
echo -e "\e[33m您输入的文件名存在或输入为空,请重新输入!\e[0m"
elif [ "$RET" == "0" ];then
echo -e "\e[33m您输入的文件名${FILENAME}已经创建!\e[0m"
touch /mnt/${FILENAME}
fi

About the Author

Question Info

Publish Time
2023-02-27 19:39
Update Time
2023-02-27 19:39