运行 shell 脚本时出现“命令未找到”错误

运行 shell 脚本时出现“命令未找到”错误
#!/bin/bash
# pre.sh revision 4.0.0  Date: 2011/12/24

### Path ###
FASTAR_DIR=$(cd $(dirname $0);pwd)

##### Function s #####
. ${FASTAR_DIR}/"script.func"

usage(){
  echo "Usage: " $COMNAME " [option]"
  echo "  option   : <NONE> ...... by 1 process"
  echo "             -n <CPUs> ... batch process by <CPUs> process"
  echo "             -all ........ batch process"
  echo "             -e .......... use exact wall distance mode"
  echo "             -metis ...... use pre-fastar metis version"
  echo "             -fsi ........ use pre-fastar metis version with FSI mode"
  echo "             -emetis ..... use pre-fastar metis version with exact wd mode"
  echo "             -efsi ....... use pre-fastar metis version with FSI and exact wd mode"
  echo "             -h .......... show this statement"
}

##### Function e #####

#======================================#
# Main                                 #
#======================================#
#=== initialize ===#
COMNAME=`basename $0`
set_variables
if [ $# -eq 1 ]; then
  case $1 in
    "-all" | "-e" | "-metis" | "-fsi" | "-emetis" | "-efsi") opt=$1      ;;
                                                       "-h") usage; exit ;;
                                                          *) usage; exit ;;
  esac
  cpu=1
elif [ $# -eq 2 ]; then
  case $1 in
    "-n") opt=$1      ;;
       *) usage; exit ;;
  esac
  if [ $2 -ge 1 ]; then
    cpu=$2
  else
    cpu=1
  fi
elif [ $# -gt 2 ]; then
  usage; exit
else
  opt="hoge"
  cpu=1
fi

#=== read inputfile ===#
read_input
case $reorder in
  ".true." | ".TRUE." | "T" | "t")
    reorder_check="TRUE"
    ;;
                                *)
    reorder_check="FALSE"
    ;;
esac

#=== ABORT ===#
trap 'abort 1 $COMNAME' 1 2 3 9 15

#=== Make directories ===#
make_dir

#=== preprocess ===#
if test $opt = "-all" ;then
  echo $NPREFAST...
  rm -f $TMP_FILE
  if [ $domain -ge 2 ]; then
    n_loop=$domain
    id=1
    for ((id=1; id<=$n_loop; id++))
    do
      echo "$NPREFAST $id  > $TMP_FILE$id &"
      $PREFAST $id  > $TMP_FILE$id &
    done
    wait

    i=1
    for ((i=1; i<=$domain; i++))
    do
      cat $TMP_FILE$i >> $TMP_FILE
      rm -f $TMP_FILE$i
    done

  elif [ $domain -eq 1 ]; then
    $PREFAST
    echo ""
  else
    echo "ERROR: It is wrong that total number of domain in $INPUT."
    exit
  fi

elif test $opt = "-n"; then
  echo $NPREFAST...
  rm -f $TMP_FILE
  n_loop=`expr $domain / $cpu`
  id=1
  i=1
  for ((i=1; i<=$n_loop; i++))
  do
    var=`expr $cpu \* \( $i - 1 \) + 1`

    id=$var
    j=1
    for ((j=1; j<=$cpu; j++))
    do
      echo "$NPREFAST $id  > $TMP_FILE$id &"
      $PREFAST $id  > $TMP_FILE$id &
      id=`expr $id + 1`
    done
    wait
  done

  if [ `expr $id - 1` -lt $domain ]; then
    while [ $id -le $domain ];
    do
      echo "$NPREFAST $id  > $TMP_FILE$id &"
      $PREFAST $id  > $TMP_FILE$id &
      id=`expr $id + 1`
    done
    wait
  fi

  i=1
  for ((i=1; i<=$domain; i++))
  do
    cat $TMP_FILE$i >> $TMP_FILE
    rm -f $TMP_FILE$i
  done

elif test $opt = "-e" ;then
  echo $NPREFAST...
  rm -f $TMP_FILE
  $PREFAST -e

elif test $opt = "-metis" ;then
  echo $NPREMETIS...
  rm -f $TMP_FILE
  $PREMETIS

elif test $opt = "-fsi" ;then
  make_dir_fsi
  echo $NPREMETIS...
  rm -f $TMP_FILE
  $PREMETIS FSI

elif test $opt = "-emetis" ;then
  echo $NPREMETIS...
  rm -f $TMP_FILE
  $PREMETIS -e

elif test $opt = "-efsi" ;then
  make_dir_fsi
  echo $NPREMETIS...
  rm -f $TMP_FILE
  $PREMETIS -eFSI

else
  echo $NPREFAST...
  rm -f $TMP_FILE
  $PREFAST
fi

#=== reorder ===#
case $reorder_check in
  "TRUE")
    echo $NREORDGRID...
    $REORDGRID
    echo $NREORDINDX...
    $REORDINDX
    ;;
esac

exit

我尝试运行 shell 脚本,但出现以下错误:

[gakubu@localhost Kenkyuu]$ pre.sh

/home/gakubu/FaSTAR2019/Bridget2019/Kenkyuu/pre.sh: 第 8 行: /home/gakubu/FaSTAR2019/Bridget2019/Kenkyuu/script.func: 没有这样的文件或目录 /home/gakubu/FaSTAR2019/Bridget2019/Kenkyuu /pre.sh:第 30 行:set_variables:找不到命令 /home/gakubu/FaSTAR2019/Bridget2019/Kenkyuu/pre.sh:行:read_input:找不到命令 /home/gakubu/FaSTAR2019/Bridget2019/Kenkyuu/pre.sh:行:make_dir:找不到命令

...

[gakubu@localhost Kenkyuu]$

enter code here

这是什么意思以及如何解决这个问题?

答案1

该脚本的第 8 行包含以下代码:

. ${FASTAR_DIR}/"script.func"

这会将文件来源到特定目录。该文件可能包含 shell 函数定义。根据错误消息,找不到该文件,并且找不到该文件的影响是找不到它定义的函数(这些是您收到的“命令未找到”错误)。

script.func假定该文件与脚本本身位于同一目录中。如果您已将脚本移至当前所在位置,但忘记将文件script.func也随之移动,则这可能是导致错误的原因。

我不会进一步评论脚本本身,只是说它使用不带引号的变量扩展使其非常脆弱,并且可能不安全。

相关内容