1

So I have this bash script

function exec_do(){
while [[ 1 ]]; do
  read _INPUT
  if [$_INPUT -eq "exit"]
  then
    break
  else
    echo $_INPUT
  fi
done
}

The intention is so that if I type in exec_do then it does a while loop which reads the input and do stuff based on the input.

If the input is exit, then it exits the while loop

If the input is not exit then it echoes it

However when I run exec_do and then type in input It instead returns input: command not found And moreover typing in "exit" doesn't break the loop and also return command not found

What did I do wrong and how can I fix this?

4

1 回答 1

3

你的比较_$INPUT是一个字符串,那么你需要==. 如果比较整数,则需要-eq. $_INPUT如果字符串变量可能包含空格或元字符,也要双引号引起来。

#!/bin/bash  

function exec_do(){ 
  while [[ 1 ]]; do  
    read _INPUT
    if[ "$_INPUT" == "exit" ]; then
      break
    else
      echo $_INPUT
    fi 
  done
  }

exec_do
于 2013-08-26T15:58:02.243 回答