通用“头"和“页脚"要原样包含在脚本中以实现多行语句 [英] Generic "header" and "footer", to be included unchanged in a script to achieve multiline statements

查看:75
本文介绍了通用“头"和“页脚"要原样包含在脚本中以实现多行语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为达到目标,以下内容(源自 Charles Duffy 的回复相关问题-请参见他的答案),这是我可以接受的格式:

To cut the chase to the point, the following (originating from a Charles Duffy's reply to a related question - see his answer) would be an acceptable format for my requirements:

eval "$(
  { sed -E -e 's/^[ ]+//;' -e ':a;N;$!ba;s/\n//g' | tr -d '\n'; } <<'____COMMAND'
sshfs 
foo_user@fooserver.com:/sftp_folder 
  /var/sshfs.sandbox/server.com 
  -o 
    user=foo_user
    ,reconnect
    ,ServerAliveInterval=15
    ,ServerAliveCountMax=3
____COMMAND
)"

请注意,我尝试将其分成多行的命令会在需要时使用尾随空格:

Note that there is a trailing space whenever it is needed by the command I'm trying to split into multple lines:

sshfs 
foo_user@fooserver.com:/sftp_folder 
  /var/sshfs.sandbox/server.com 
  -o 

对于没有中间空格的必须串联的选项,没有尾随空格:

And no trailing spaces for options that have to be concatenated without an intervening blank space:

user=foo_user
,reconnect
,ServerAliveInterval=15
,ServerAliveCountMax=3

连接后,命令将变为:

sshfs foo_user@fooserver.com:/sftp_folder /var/sshfs.sandbox/server.com -o user=foo_user,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3

但是,有人告诉我尾随空格是个坏主意(再次,请参阅 Charles Duffy 回复以获取动机)

However, I am told trailing spaces are a bad idea all around (once again, refer to Charles Duffy's reply for the motivations)

因此,我想知道.有没有办法实现这三个目标:

Hence I am wondering. Is there a way to achieve all these three:

  1. 将语句全部保持在一起".
  2. 没有尾随空格.
  3. 固定页眉"和页脚",并复制其他脚本中的可粘贴内容而无需进行修改.

我当时正在考虑使用结尾的反斜杠并修改解析表达式:

I was thinking of using a trailing backslash and modifying the parsing expression:

{ sed -E -e 's/^[ ]+//;' -e ':a;N;$!ba;s/\n//g' | tr -d '\n'; }

这样我就可以实现上面提到的所有三个.

so that the I can achieve all three mentioned above.

推荐答案

希望这会做您想要的事情.

Hopefully, this will do what you want.

首先,定义一个旨在解析在标准输入上接收到的命令的函数.

First, define a function intended to parse a command received on standard input.

#!/bin/bash

execute_command()
{
  local -a command=()
  local arg=
  while IFS= read -r line
  do
    [[ $line =~ [[:blank:]]*(.*[^[:blank:]\\])(\\?)[[:blank:]]*$ ]]
    if
      [[ ${BASH_REMATCH[2]} ]]
    then
      arg+=${BASH_REMATCH[1]}
    else
      command+=("$arg${BASH_REMATCH[1]}")
      arg=
    fi
  done
  "${command[@]}"
}

然后,通过在每行的末尾留下一个斜杠来调用此函数,在该行中您希望将下一行视为同一参数的一部分.上面的代码将不执行任何解析,因此您不能在同一行上放置两个参数(在您的示例中,您没有这样做,因此我认为这是您可以接受的限制).

Then, call this function by leaving a trailing slash at the end of each line where you want the following line to be considered part of the same argument. The above code will not perform any parsing, so you cannot put two arguments on the same line (which, in your example, you are not doing, and I therefore assume is an acceptable limitation for you).

execute_command <<'____COMMAND'
sshfs
foo_user@fooserver.com:/sftp_folder
  /var/sshfs.sandbox/server.com
  -o
    user=foo_user\
    ,reconnect\
    ,ServerAliveInterval=15\
    ,ServerAliveCountMax=3
____COMMAND

这为您提供了一个非常简单,可重用的标题",不需要(但可以容忍)尾随空格,并允许在所有行上使用前导空格,同时引入了尽可能少的重/怪异语法.

This provides you with a very simple, reusable "header", does not require (but tolerates) trailing spaces and allows leading spaces on all lines, while introducing as little heavy/weird syntax as possible.

这篇关于通用“头"和“页脚"要原样包含在脚本中以实现多行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆