Bash脚本不输出任何内容

我的脚本只运行不产生任何输出。如果我不输入任何参数,它将正确地进行错误检查,但是如果我为该参数输入一个适当的文件,它将仅运行而没有输出且没有错误。该脚本的目的是检查文件是否可执行,然后在未设置权限时询问用户是否要更改权限。

这是代码

 #!/bin/bash

#making sure and argument is entered

if [ $# -ne 1 ]
then
  echo "Invalid arguments. Must pass 1 argument"
  echo "Usage: $0 <filename>"
  exit 1
fi

#filename argument variable
fname=$1

#storing file to verify its a executable
is_executable=`file $fname`

#getting permissions
lsOut=`ls -l $fname|awk '{print $1}'`

ownerPerm=`echo ${lsOut:1:3}`
groupPerm=`echo ${lsOut:4:3}`
otherPerm=`echo ${lsOut:7:3}`

# checking if the file is a executable
if [[ "$is_executable" =~ .*"executable".* ]]
then
  if [[ $ownerPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Owner(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod u+x $fname
    fi
  fi

  if [[ $groupPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Group(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod g+x $fname
    fi
  fi

  if [[ $otherPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Other(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod o+x $fname
    fi
  fi
else
  if [[ $ownerPerm =~ .*"x".* ]]
  then
    chmod u-x $fname
    echo "Execute permission removed from owner for the file $fname"
  fi

  if [[ $groupPerm =~ .*"x".* ]]
  then
    chmod g-x $fname
    echo "Execute permission removed from group for the file $fname"
  fi
  if [[ $otherPerm =~ .*"x".* ]]
  then
    chmod o-x $fname
    echo "Execute permission removed from other for the file $fname"
  fi
fi
aniu8258 回答:Bash脚本不输出任何内容

我发现那是错的。我是个白痴,为我尝试运行的测试错误地设置了文件权限。 bash -x命令允许我弄清楚它。

本文链接:https://www.f2er.com/3127793.html

大家都在问