Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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

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
File 如果shell脚本文件_File_Shell_Unix_If Statement - Fatal编程技术网

File 如果shell脚本文件

File 如果shell脚本文件,file,shell,unix,if-statement,File,Shell,Unix,If Statement,我试图编写一个shell脚本,但在编写一个特殊的if语句时遇到了一些困难。我希望,如果它在文件中找到一个值,它会做一些事情 我的文件如下: 1,2,2,80,0,3,35,0,4,51,4,5,31,0,8,7 我这样做是为了提取一些信息 sed-i'/^[0-9][0-9]*,[0-9][0-9]*,3,/d'file.txt 但我想把它放在一个if条件下,比如如果第三个数字是3、4或5,我做sed,否则我做别的,我尝试这个: if [ '/*, *, [3,5],/d' ]; then

我试图编写一个shell脚本,但在编写一个特殊的if语句时遇到了一些困难。我希望,如果它在文件中找到一个值,它会做一些事情

我的文件如下:

1,2,2,8
0,0,3,3
5,0,4,5
1,4,5,3
1,0,8,7

我这样做是为了提取一些信息

sed-i'/^[0-9][0-9]*,[0-9][0-9]*,3,/d'file.txt

但我想把它放在一个if条件下,比如如果第三个数字是3、4或5,我做sed,否则我做别的,我尝试这个:

if [ '/*, *, [3,5],/d' ]; then
    echo 'ok'
else
    echo'fail'
fi
但它不起作用,总是打印得很好


你知道我怎么做吗?

'*'
将匹配数量不确定的空格。
您应该改为放置
[0-9]*

'*'
将匹配未确定数量的空格。
您应该改为使用
[0-9]*

请下次提供更清晰的示例

#!/bin/bash
# tested with bash 4
while read -r x x Y x
do
  case "$Y" in
   "3," ) echo "ok";;
   *) echo "Not ok";;
  esac
done < file
#/bin/bash
#用bash4测试
读取时-r x Y x
做
大写“$Y”
“3,”)回显“ok”;;
*)回声“不正常”;;
以撒
完成<文件

下次请提供更清晰的示例

#!/bin/bash
# tested with bash 4
while read -r x x Y x
do
  case "$Y" in
   "3," ) echo "ok";;
   *) echo "Not ok";;
  esac
done < file
#/bin/bash
#用bash4测试
读取时-r x Y x
做
大写“$Y”
“3,”)回显“ok”;;
*)回声“不正常”;;
以撒
完成<文件

您可以使用内部字段分隔符,让shell负责解析:

#!/bin/sh
# let's create the input file
echo '5, 2, 3, 5' > /tmp/myfile

# function will output third argument
third() { IFS=', '; echo $3; }

# now let's print the third value from the input file
third $(cat /tmp/myfile)

请参阅mansh,并查找IFS以了解更多信息。

您可以使用内部字段分隔符,让shell负责解析:

#!/bin/sh
# let's create the input file
echo '5, 2, 3, 5' > /tmp/myfile

# function will output third argument
third() { IFS=', '; echo $3; }

# now let's print the third value from the input file
third $(cat /tmp/myfile)

请参阅man sh,并查找IFS以了解更多信息。

如果不强制使用仅限shell的解决方案,则可以通过awk获得第三个字段:

#!/bin/sh
THIRD=$(awk -F', ' '{print $3}' < /tmp/myfile)
#/垃圾箱/垃圾箱
第三个=$(awk-F','{print$3}'
如果不强制使用纯shell解决方案,您可以通过awk获得第三个字段:

#!/bin/sh
THIRD=$(awk -F', ' '{print $3}' < /tmp/myfile)
#/垃圾箱/垃圾箱
第三个=$(awk-F','{print$3}'
只要从文件中提取一些信息,这部分就可以了。我只是想在我想要的数字不在脚本中时改进我的脚本,然后做其他事情;然后等等?当文件在行的任意位置包含后跟逗号的空3时,将返回OK。但不清楚你到底想在这里做什么。。。文件名是否在变量中,是否已将文件读入数组?具体的条件是什么等?对于grep,我得到了这个错误:[grep:unexpected operator文件就是这样的数字>1,2,2,8>0,0,3,3>5,0,4,5>1,4,5,3>1,0,8,7我这样做是为了提取一些信息sed-I'/^[0-9][0-9]*,[0-9][0-9]*,3,/d'file.txt,但我想将其置于if条件下。例如,如果第三个数字是3、4或5,我会执行sed,否则我会执行其他操作仅从文件中提取一些信息,这部分工作正常。我只想在我想要的数字不在其中时改进脚本,执行其他操作。if有什么问题吗[
grep-s'3,$filename];然后
等等?当文件包含一个空的3,后跟一个逗号时,这将返回OK,行中的任何位置。但不清楚您在这里到底想做什么…文件名在变量中,您是否将文件读入数组?确切的条件是什么等?对于grep,我得到以下错误:[grep:unexpected operator文件就是这样的数字>1,2,2,8>0,0,3,3>5,0,4,5,5,3>1,0,8,7我这样做是为了提取一些信息sed-I'/^[0-9][0-9]*,[0-9][0-9]*,3,/d'file.txt,但我想把它放在一个if条件下。比如,如果第三个数字是3、4或5,我会执行sed,否则我会执行其他操作,但它只针对一个数字?如果我想要3-4-5,怎么样?@tranen,那么你最好给出一些更清晰的示例,说明你想要什么。指定输入文件的所有可能格式。没有足够的信息,您不会期望得到问题的准确答案。@tranen,请编辑您的问题以包含您的新信息。将它们放入问题中将使解决方案提供商能够立即知道您在寻找什么。此外,将它们放入注释部分将丢失格式。但它仅用于一个数字?如果我愿意,如何3-4-5?@tranen,那么你最好给出一些更清楚的例子来说明你想要什么。指定输入文件的所有可能格式。如果没有足够的信息,你将无法获得问题的准确答案。@tranen,请编辑你的问题以包含你的新信息。将它们放在问题中将使解决方案提供商能够即时消息立即知道你在找什么。而且,将它们放在评论部分会丢失格式。