linux 操作系统中shell过滤
当前目录有一文本,文本中有0~28若干组整数数值以逗号为间隔有可能是,0,2,12,24或者其它数值组合,也有可能,0到28数值全部都存在,数值间以逗号为间隔如何实现以下...
当前目录有一文本,文本中有0~28若干组整数数值以逗号为间隔
有可能是,
0,2,12,24或者其它数值组合,也有可能,0到28数值全部都存在,数值间以逗号为间隔
如何实现以下功能,
当文本中的数值有0时执行一条指定的指令
有1时,也执行一条不同指令,
以此类推,存在的每个数值执行一条特定的指令
如何判断文本中存在的数值,并执行对应的指令呢?如
当文本中有
1,3,8.11,13,21,28
执行各数值对应的指令!
shell该如何写? 展开
有可能是,
0,2,12,24或者其它数值组合,也有可能,0到28数值全部都存在,数值间以逗号为间隔
如何实现以下功能,
当文本中的数值有0时执行一条指定的指令
有1时,也执行一条不同指令,
以此类推,存在的每个数值执行一条特定的指令
如何判断文本中存在的数值,并执行对应的指令呢?如
当文本中有
1,3,8.11,13,21,28
执行各数值对应的指令!
shell该如何写? 展开
2个回答
展开全部
这不简单,for循环嵌套case多分枝选择结构,一楼的回答我就不说什么了,根本就没有/bash/这个路径,sh自然也不会存在在这个路径上。这个脚本就是可能的数值太多,case分支写得累……。算了,我给你写个吧。
说明:此脚本接收脚本后跟的第一个参数为读取定义数值的文本,比如说你在工作路径上有个叫test的文件,该文件定义了需要包含的数字,脚本本身的文件名是num那么执行该脚本的命令是./num ./test
每个数字对应的命令自己改吧,我这里全是打印文件里包含了对应的数值到标准输出上。以下脚本在ubuntu server 8.04测试通过。
#! /bin/bash
for var in `sed 's/,/ /g' $1` #注意,包围sed语句的是反引号,即字母键盘上方数字1左边的那个键
do
case $var in
0)
echo "0 is in the string\.";;
1)
echo "1 is in the string\.";;
2)
echo "2 is in the string\.";;
3)
echo "3 is in the string\.";;
4)
echo "4 is in the string\.";;
5)
echo "5 is in the string\.";;
6)
echo "6 is in the string\.";;
7)
echo "7 is in the string\.";;
8)
echo "8 is in the string\.";;
9)
echo "9 is in the string\.";;
10)
echo "10 is in the string\.";;
11)
echo "11 is in the string\.";;
12)
echo "12 is in the string\.";;
13)
echo "13 is in the string\.";;
14)
echo "14 is in the string\.";;
15)
echo "15 is in the string\.";;
16)
echo "16 is in the string\.";;
17)
echo "17 is in the string\.";;
18)
echo "18 is in the string\.";;
19)
echo "19 is in the string\.";;
20)
echo "20 is in the string\.";;
21)
echo "21 is in the string\.";;
22)
echo "22 is in the string\.";;
23)
echo "23 is in the string\.";;
24)
echo "24 is in the string\.";;
25)
echo "25 is in the string\.";;
26)
echo "26 is in the string\.";;
27)
echo "27 is in the string\.";;
28)
echo "28 is in the string\.";;
*)
echo "One or more variable\(s\) in $1 is out of range!";;
esac
done
说明:此脚本接收脚本后跟的第一个参数为读取定义数值的文本,比如说你在工作路径上有个叫test的文件,该文件定义了需要包含的数字,脚本本身的文件名是num那么执行该脚本的命令是./num ./test
每个数字对应的命令自己改吧,我这里全是打印文件里包含了对应的数值到标准输出上。以下脚本在ubuntu server 8.04测试通过。
#! /bin/bash
for var in `sed 's/,/ /g' $1` #注意,包围sed语句的是反引号,即字母键盘上方数字1左边的那个键
do
case $var in
0)
echo "0 is in the string\.";;
1)
echo "1 is in the string\.";;
2)
echo "2 is in the string\.";;
3)
echo "3 is in the string\.";;
4)
echo "4 is in the string\.";;
5)
echo "5 is in the string\.";;
6)
echo "6 is in the string\.";;
7)
echo "7 is in the string\.";;
8)
echo "8 is in the string\.";;
9)
echo "9 is in the string\.";;
10)
echo "10 is in the string\.";;
11)
echo "11 is in the string\.";;
12)
echo "12 is in the string\.";;
13)
echo "13 is in the string\.";;
14)
echo "14 is in the string\.";;
15)
echo "15 is in the string\.";;
16)
echo "16 is in the string\.";;
17)
echo "17 is in the string\.";;
18)
echo "18 is in the string\.";;
19)
echo "19 is in the string\.";;
20)
echo "20 is in the string\.";;
21)
echo "21 is in the string\.";;
22)
echo "22 is in the string\.";;
23)
echo "23 is in the string\.";;
24)
echo "24 is in the string\.";;
25)
echo "25 is in the string\.";;
26)
echo "26 is in the string\.";;
27)
echo "27 is in the string\.";;
28)
echo "28 is in the string\.";;
*)
echo "One or more variable\(s\) in $1 is out of range!";;
esac
done
展开全部
#! /bash/sh
FILE=/../../..
[ $grep "^0," $FILE ] || [ $grep ",0," $FILE ] || [ $grep ",0$" $FILE ] && comand
[ $grep "^1," $FILE ] || [ $grep ",1," $FILE ] || [ $grep ",1$" $FILE ] && comand
[ $grep "^2," $FILE ] || [ $grep ",2," $FILE ] || [ $grep ",2$" $FILE ] && comand
....
FILE=/../../..
[ $grep "^0," $FILE ] || [ $grep ",0," $FILE ] || [ $grep ",0$" $FILE ] && comand
[ $grep "^1," $FILE ] || [ $grep ",1," $FILE ] || [ $grep ",1$" $FILE ] && comand
[ $grep "^2," $FILE ] || [ $grep ",2," $FILE ] || [ $grep ",2$" $FILE ] && comand
....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询