Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套if-in-shell脚本_Shell_Unix_Conditional Statements_Ls - Fatal编程技术网

嵌套if-in-shell脚本

嵌套if-in-shell脚本,shell,unix,conditional-statements,ls,Shell,Unix,Conditional Statements,Ls,我想写一个脚本,它接受1个命令行参数(一个目录),然后提示输入2个数字,然后它将打印出大小在2个数字之间的任何文件(每个文件位于不同的行中),这是我的脚本 echo -n "Enter the first number: " read a echo -n "Enter the second, bigger number: " read b if [ $b -lt $a ] then echo 'The first number must be smaller' else echo

我想写一个脚本,它接受1个命令行参数(一个目录),然后提示输入2个数字,然后它将打印出大小在2个数字之间的任何文件(每个文件位于不同的行中),这是我的脚本

echo -n "Enter the first number: "
read a
echo -n "Enter the second, bigger number: "
read b
    if
 [ $b -lt $a ]
then
 echo 'The first number must be smaller'
else
 echo The files in $1 that are between $a and $b bytes are the following
 echo
 for var in 'ls $1'
 do
  if
   [ -f $var ]
  then
    size='ls -l $var | '{ print $5 }''
     if
      [ $size -le $b && $size -ge $a ]
     then
      echo $var is $size bytes
     fi
  fi
 done
fi

问题是在我输入数字后,它会打印出“文件…”,然后什么也不打印。另外,我使用Vi来编辑它,但最后三行的颜色不太正确(颜色应该与第一个“fi”匹配,但不是)。谁能告诉我出了什么问题吗?谢谢。

这里有几个问题,但我认为让您困扰的是,单引号(')在几个应该使用反勾号(`)的地方使用。这是一个微妙的印刷区别,所以有时候以前没有遇到过的人不会注意到这种区别。在我的键盘上,您只需按数字1左侧的键,就可以得到一个反勾号字符,它与波浪线(~)配对,但您的键盘可能不同

backtick允许您将命令的输出分配给变量,例如:

my_variable=`ls - l` # <- uses backtick, assigns output of 'ls -l' command to $my_variable

#As opposed to:
my_variable='ls -l' # <- uses single-quote, makes $my_variable equal to the text "ls -l"

my_variable=`ls-l`

这里有几个问题,但我认为让您感到困惑的是,单引号(')在几个应该使用反勾号(`)的地方使用。这是一个微妙的印刷区别,所以有时候以前没有遇到过的人不会注意到这种区别。在我的键盘上,您只需按数字1左侧的键,就可以得到一个反勾号字符,它与波浪线(~)配对,但您的键盘可能不同

backtick允许您将命令的输出分配给变量,例如:

my_variable=`ls - l` # <- uses backtick, assigns output of 'ls -l' command to $my_variable

#As opposed to:
my_variable='ls -l' # <- uses single-quote, makes $my_variable equal to the text "ls -l"

my_variable=`ls-l`

如其他人所述,使用shebang和backticks来执行命令。其他错误,
ls-l$var |{print$5}
应该是
ls-l“$1$var”| awk{print$5}”
(awk命令丢失),测试文件时应该使用文件的完整路径,如
[-f“$1$var”]
,因为用户可能与作为脚本参数提供的路径不在同一目录中。另一个问题是
[$size-le$b&&$size-ge$a]
。您不能这样使用
&&
运算符,而是使用
[$size-le$b]&&[$size-ge$a]

这些都是我对你的代码所做的更改。希望对你有用

echo -n "Enter the first number: "
read a
echo -n "Enter the second, bigger number: "
read b
if [ $b -lt $a ]
then
  echo 'The first number must be smaller'
else
  echo The files in "$1" that are between "$a" and "$b" bytes are the following
  echo
  for var in `ls "$1"`
  do
    if [ -f $1$var ]
    then
      size=`ls -l "$1$var" | awk '{ print $5 }'`
      if [ $size -le $b ] && [ $size -ge $a ]
      then
        echo "$var" is "$size" bytes
      fi
    fi
  done
fi

正如其他人所说,在命令中使用shebang和backticks。其他错误,
ls-l$var |{print$5}
应该是
ls-l“$1$var”| awk{print$5}”
(awk命令丢失),测试文件时应该使用文件的完整路径,如
[-f“$1$var”]
,因为用户可能与作为脚本参数提供的路径不在同一目录中。另一个问题是
[$size-le$b&&$size-ge$a]
。您不能这样使用
&&
运算符,而是使用
[$size-le$b]&&[$size-ge$a]

这些都是我对你的代码所做的更改。希望对你有用

echo -n "Enter the first number: "
read a
echo -n "Enter the second, bigger number: "
read b
if [ $b -lt $a ]
then
  echo 'The first number must be smaller'
else
  echo The files in "$1" that are between "$a" and "$b" bytes are the following
  echo
  for var in `ls "$1"`
  do
    if [ -f $1$var ]
    then
      size=`ls -l "$1$var" | awk '{ print $5 }'`
      if [ $size -le $b ] && [ $size -ge $a ]
      then
        echo "$var" is "$size" bytes
      fi
    fi
  done
fi

直接的问题是在需要命令替换的地方使用了单引号。但是,这是迭代文件的错误方式。您应该改用模式匹配。您的
for
循环应为

for var in $1/*
do
  if [ -f "$var" ]
  then
    # Check 'man stat' for the correct format string on your system
    size=$(stat +%s "$var")
    if [ $size -le $b ] &&  [ $size -ge $a ]
    then
      echo $var is $size bytes
    fi
  fi
done

直接的问题是在需要命令替换的地方使用了单引号。但是,这是迭代文件的错误方式。您应该改用模式匹配。您的
for
循环应为

for var in $1/*
do
  if [ -f "$var" ]
  then
    # Check 'man stat' for the correct format string on your system
    size=$(stat +%s "$var")
    if [ $size -le $b ] &&  [ $size -ge $a ]
    then
      echo $var is $size bytes
    fi
  fi
done

您需要一个“shebang”行作为脚本的最顶层。你应该有
#/bin/ksh
#/bin/bash
或从echo$SHELL
获得的任何值。只要在shell debug/trace选项设置为on的情况下运行这段代码,您就会发现大部分问题。添加
set-vx
只是
echo-n…
。调试会在执行前显示每个代码块,并使用前导的
+
符号显示该代码块中执行的每一行,其中包含变量的值。您应该看到,
$size
不是您可能认为的那样。祝你好运。我为什么需要那条哈斯邦线?我读得很快,但还是不明白为什么我需要它。非常感谢,我不知道有一个调试器用于此:)@Nguyễn您是否应该包括
#/bin/??sh
行,本质上,声明运行脚本所需的shell。如果它与旧的bourne shell兼容,您可以使用
#/bin/sh
但如果您使用的功能(内置)仅在zsh或bash中可用,则应使用
#/bin/bash
或“#”/bin/zsh`-或其他什么。您还可以编写类似于以
#开头的Perl脚本之类的东西/usr/bin/perl
然后是
chmod+x myPerlScript
,您不必在文件上使用类似
.pl
扩展名的垃圾。您需要一个“shebang”行作为脚本的最上面一行。你应该有
#/bin/ksh
#/bin/bash
或从echo$SHELL
获得的任何值。只要在shell debug/trace选项设置为on的情况下运行这段代码,您就会发现大部分问题。添加
set-vx
只是
echo-n…
。调试会在执行前显示每个代码块,并使用前导的
+
符号显示该代码块中执行的每一行,其中包含变量的值。您应该看到,
$size
不是您可能认为的那样。祝你好运。我为什么需要那条哈斯邦线?我读得很快,但还是不明白为什么我需要它。非常感谢,我不知道有一个调试器用于此:)@Nguyễn您是否应该包括
#/bin/??sh
行,本质上,声明运行脚本所需的shell。如果它与