具有参数和值的条件构造

问题描述 投票:1回答:1

请考虑以下代码。如果我删除了eval而只写了if "$1",则它不起作用。是否可以摆脱eval

assert () {
  if eval "$1"      #Is it possible to get rid of the eval?
  then
    echo "Assertion OK:  \"$1\""
  else
    echo "Assertion FAILED:  \"$1\""
  fi
}

assert " [[ /tmp/a = /tmp/* ]]"
bash eval
1个回答
-2
投票

将括号扔掉,然后输入并完成

#!/bin/bash

assert () {

  if [[ $1 ]]
  then
    echo "Assertion OK:  \"$1\""
  else
    echo "Assertion FAILED:  \"$1\""
  fi
}

assert " /tmp/a = /tmp/* "

© www.soinside.com 2019 - 2024. All rights reserved.