Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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
Bash 打印元素列表的Shell脚本_Bash_Shell_Tcl_Sh - Fatal编程技术网

Bash 打印元素列表的Shell脚本

Bash 打印元素列表的Shell脚本,bash,shell,tcl,sh,Bash,Shell,Tcl,Sh,shell脚本中是否有类似于tcl中“list”的命令?我想将元素列表写入一个文件(每一行都是单独的)。但是,如果元素匹配一个特定的模式,那么它旁边的元素和元素本身应该打印在同一行中。shell脚本中是否有执行此操作的命令 示例:我的字符串类似于“执行命令run abcd.v” 我想在文件的单独行中写入每个单词,但如果单词为“run”,则必须在同一行中打印abcd.v和run。所以,输出应该是 execute the command run abcd.v 如何在shell脚本中执行此操作?以下

shell脚本中是否有类似于tcl中“list”的命令?我想将元素列表写入一个文件(每一行都是单独的)。但是,如果元素匹配一个特定的模式,那么它旁边的元素和元素本身应该打印在同一行中。shell脚本中是否有执行此操作的命令

示例:我的字符串类似于“执行命令run abcd.v” 我想在文件的单独行中写入每个单词,但如果单词为“run”,则必须在同一行中打印abcd.v和run。所以,输出应该是

execute
the
command
run abcd.v

如何在shell脚本中执行此操作?

以下是如何在bash中执行此操作:

  • 将以下脚本命名为
    list
  • 将其设置为可执行
  • 将其复制到您的
    ~/bin/
列表:

#!/bin/bash
# list

while [[ -n "$1" ]]
do
   if [[ "$1" == "run" ]]; then
       echo "$1 $2"
   else
       echo "$1"
   fi
   shift
done
以下是如何在命令提示符下使用它:

list execute the command run abcd.v > outputfile.txt
您的
outputfile.txt
将被写入:

execute
the
command
run abcd.v

您可以使用下面的脚本来完成它。这不是一个单一的命令。下面是一个for循环,它有一个if语句来检查关键字run。它不附加新行字符(
echo-n

for i in `echo "execute the command run abcd.v"`
do 
  if [ $i = "run" ] ; then  
    echo -n "$i " >> fileOutput
  else 
    echo $i >> fileOutput
  fi
done
line="execute the command run abcd.v"
for word in $line    # the variable needs to be unquoted to get "word splitting"
do
    case $word in
        run|open|etc) sep=" " ;;  
        *) sep=$'\n' ;;
    esac
    printf "%s%s" $word "$sep"
done