Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
Linux 使用:分隔符将变量值插入到列中_Linux_Shell - Fatal编程技术网

Linux 使用:分隔符将变量值插入到列中

Linux 使用:分隔符将变量值插入到列中,linux,shell,Linux,Shell,我有一个文本文件,其中填充了以下示例值: 1:0:0:Monitoring stuff with Zabbix/OCS:24 HOURS 我想用一个变量来改变第二个字段。我正在尝试这样做: #!/bin/bash PRIOR="Priority = 1 - Must have | 2 - Nice to have | 3 - Interesting | 0 - Not interesting" while read -r line; do echo $line echo $PRIOR

我有一个文本文件,其中填充了以下示例值:

 1:0:0:Monitoring stuff with Zabbix/OCS:24 HOURS
我想用一个变量来改变第二个字段。我正在尝试这样做:

#!/bin/bash

PRIOR="Priority = 1 - Must have | 2 - Nice to have | 3 - Interesting | 0 - Not interesting"

while read -r line;
do
echo $line
echo $PRIOR
echo -n "Set your priority: "
read SETP</dev/tty
echo "Priority defined: "$SETP
<change my 2nd column value with $SETP>
done < courses.txt
#/bin/bash
PRIOR=“Priority=1-必须拥有| 2-拥有很好| 3-有趣| 0-不有趣”
而read-r行;
做
回音$线
回音$preor
echo-n“设置优先级:”
阅读SETP这里有一种方法

newline=$( echo "$line" | sed "s/:[^:]\+/:$SETP/")
这将用冒号和用户输入替换后跟非冒号字符的第一个冒号

一些代码检查注释:

  • 养成使用良好缩进的习惯——你未来的自己会感谢你编写了可读和可维护的代码
  • 不要使用所有的大写字母,把它们留给shell——总有一天你会写
    readpath
    ,然后想知道为什么你的脚本会被破坏。
    • priorities=“…”
      读取setp
  • --总是这样做,除非你确切知道什么时候不引用它们。
    • echo“$line”
    • echo“优先级定义:$setp”
      ——引号内的变量
  • 验证用户的输入:
    • if[[$setp==*[^0-9]*];然后回显“仅限数字!重试”;fi
  • 在bash中,您可以编写
    read-p“设置优先级:”setp
    ——不需要单独的echo语句
  • 了解您的语言支持的数据结构很重要——学习如何使用bash数组

很好,感谢glenn的回答和代码审查,我将在下一个脚本中对此进行说明。