Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Created August 2, 2024 06:11
Show Gist options
  • Save kjunichi/470b35f6d8ac7f36d244ddd04910e5a0 to your computer and use it in GitHub Desktop.
Save kjunichi/470b35f6d8ac7f36d244ddd04910e5a0 to your computer and use it in GitHub Desktop.

シェルでの入力処理の自動化の検討

#!/bin/sh

myinp1() {
    read ANSWER
    case $ANSWER in
    "a")
        return 1
        ;;
    *)
        echo "$ANSWER"
        ;;
    esac
    return 0
}

myinp2() {
    read ANSWER
    case $ANSWER in
    "b")
        return 1
        ;;
    *)
        echo "$ANSWER"
        ;;
    esac
    return 0
}

myinp3() {
    read ANSWER
    case $ANSWER in
    "c")
        return 1
        ;;
    *)
        echo "$ANSWER"
        ;;
    esac
    return 0
}

echo "start"

echo "1"
myinp1
STS=$?
if [ $STS -ne 1 ]; then
    exit 1
fi
echo "2"
myinp2
STS=$?
if [ $STS -ne 1 ]; then
    exit 1
fi
echo "3"
myinp3
STS=$?
if [ $STS -ne 1  ]; then
    exit 1
fi

echo "end"
% ./test2.sh </dev/null
start
1

% echo "a\nb\nc"| ./test2.sh
start
1
2
3
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment