Scanf equivalent for bash?

How to search for user keyboard input in bash shell? I thought it would just work,

int b;

scanf("%d", &b);

but he says

- bash: / Users / [name] /. bash_profile: line 17: syntax error near unexpected token ``% d ','

- bash: / Users / [name] /. bash_profile: line 17: `scanf ("% d ", & b); ''

EDIT

backdoor() {
   printf "\nAccess backdoor Mr. Fletcher?\n\n"
   read -r b
   if (( b == 1 )) ; then
     printf "\nAccessing backdoor...\n\n"
   fi   
}
+3
source share
3 answers

Just use readbuiltin:

read -r b

( %d), , () , ; , , , , .

:

(( b == 1 ))

... $b ,

[[ $b = 1 ]]

... b "1".

+7

Bash, , . , , , , . , :

while read -p "Enter integer: " integer; do
    [[ "$integer" =~ [[:digit:]]+ ]] && break
    echo "Not an integer: $integer" >&2
done

, , , .

+2

C Bash.

backdoor() {

    printf '\n%s\n\n' 'Access backdoor Mr. Fletcher?'

    read -r b

    if ((b == 1))
    then
        printf '\n%s\n\n' 'Accessing backdoor...'
    fi
}
0

All Articles