, Как я могу пропустить остальную часть сценария, не выходя из раковины призыва, поставляя сценарий?

Вы используете bash? В этом случае попробуйте что-то вроде этого:

MAXCOUNT=10
count=1

while [ "$count" -le $MAXCOUNT ]; do
 number[$count]=$RANDOM
 let "count += 1"
done

echo "${number[*]}"

Вы также можете заменить последнюю строку на:

echo "${number[@]}"

Здесь есть документация: http://www.tutorialspoint.com/unix/unix-using-arrays.htm

6
02.08.2018, 19:23
2 ответа

Используйте return.

Встроенная функция return bash закрывает исходный скрипт, не останавливая вызывающий (родительский/исходный )скрипт.

От ман баш:

return [n]
Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body. … If return is used outside a function, but during execution of a script by the. (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script.

9
27.01.2020, 20:25

return закрывает исходные скрипты (и функции).

В вашем случае:

while getopts ":t:" opt; do
    case $opt in
        t)
            timelen="$OPTARG"
            ;;
        \?) printf "illegal option: -%s\n" "$OPTARG" >&2
            echo "$usage" >&2
            return 1
            ;;
        :) printf "missing argument for -%s\n" "$OPTARG" >&2
           echo "$usage" >&2
           return 1
           ;;
    esac
done

Пример теста:

$ cat script1.sh
echo script1
source./script2.sh
echo script1 ends
$ cat script2.sh
echo script2

while true; do
    return
done

echo script2 ends
$ bash script1.sh
script1
script2
script1 ends

Также источник script2.shработает правильно (без выхода из текущего сеанса оболочки):

$ source script2.sh
script2
1
27.01.2020, 20:25

Теги

Похожие вопросы