AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-22734

Brad Parks's questions

Martin Hope
Brad Parks
Asked: 2023-03-04 00:18:42 +0800 CST

bash 的简单模板引擎,可以使用 csv 数据吗?

  • 7

我经常想快速获取一些数据,并将其应用于 bash 中的模板。

例如想象做以下事情

  $ seq 1 2 | eztemplate 'this is a {1} test'
  this is a 1 test
  this is a 2 test

  $ eztemplate 'this is a {1} test' hi 'there now'
  this is a hi test
  this is a there now test

  $ eztemplate /tmp/template /tmp/alphabet  | head
  this is a a test
  of this system a
  this is a b test
  of this system b
  ...

我已经编写了一个非常简单的 bash 脚本来执行此操作,但正在考虑允许每行数据使用多个参数,例如 CSV 样式数据。

鉴于以下情况,是否已经存在比我的小脚本更好的东西?

  • 我希望它只需要基本的 unix posix 工具,以及通常安装的东西,如 perl 或 awk,但不需要 perl 额外安装模块,例如
  • 它可以接受数据文件中每行数据的多列数据。
  • 基本上是一个不需要安装任何其他东西的 bash 脚本 :D
  • 一个侧面的目的是让不擅长 bash 脚本的其他人有一个简单的工具来处理重复数据的模板

数据和模板会有很大差异,但我想做的第一个例子是将 4 个 id 应用于 JSON 负载

模板

{
  "tenantId": "{1}",
  "key": "some.key",
  "value": "some.value"
}

数据

my/super/01cbf4e9779649038f0bd753747c8b26
my/test/01cbf4e9779649038f0bd753747c8b26
ez/test/01cbf4e9779649038f0bd753747c8b26
rad/data/df3a47fed39d453f836f9b2196332029

ez模板

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; PATH="$DIR:$PATH"

function show_help()
{
  ME=$(basename "$0")
  IT=$(cat <<EOF

  replaces vars in a template, one for each line of stdin

  e.g.

  $ seq 1 2 | $ME 'this is a {1} test'
  this is a 1 test
  this is a 2 test

  $ $ME 'this is a {1} test' hi 'there now'
  this is a hi test
  this is a there now test

  $ $ME /tmp/template /tmp/alphabet
  this is a a test
  of this system a
  this is a b test
  of this system b
  ...

EOF
)
  echo "$IT"
  echo
  exit
}

if [ -z "$1" ]
then
  show_help
fi
if [ "$1" = "help" ] || [ "$1" = '?' ] || [ "$1" = "--help" ] || [ "$1" = "h" ]; then
  show_help
fi

function process_template(){
  DATA=$1
  VAR=$2

  if [ -f "$DATA" ]; then
    DATA=$(cat "$DATA")
  fi

  echo "$DATA" | sed "s#{1}#$VAR#g"
}


TEMPLATE=$1
if [ -t 0 ]
then
  if [ -f "$2" ]; then
    # allow first 2 parameters to be files, TEMPLATE and then DATA
    DATA_FILE=$2
    cat "$DATA_FILE" | while read line
    do
      process_template "$TEMPLATE" "$line"
    done 
  else
    shift;
    for line in "$@" 
    do
      process_template "$TEMPLATE" "$line"
    done
  fi
else
  # loop over lines from stdin
  while IFS= read -r line; do
    process_template "$TEMPLATE" "$line"
  done
fi
bash
  • 1 个回答
  • 68 Views
Martin Hope
Brad Parks
Asked: 2021-10-30 11:55:05 +0800 CST

bash:使用子shell分配给变量,但如果分配失败则退出主脚本

  • 0

我想要一个 bash 脚本,它允许我调用一个函数来查看文件中是否包含某些数据,如果它没有使主脚本失败,类似于以下内容,简化为保留这一点。

这不起作用(当子 shell 失败时不退出主脚本)。

我该如何编写require_line函数,这样我就可以像这样在一个文件中说出其中的 20 多个

VALUE1=$(require_line "myKey1")
VALUE2=$(require_line "myKey2")
...

并且不需要一个 if 围绕每个?

#!/bin/bash
set -eo pipefail

VALUE=$(require_line "myKey")

require_line(){
  local KEY=$1
  local DATA=$(cat /tmp/myfile)
  local INFO=$(echo "$DATA" | grep "$KEY")

  if [ ! -z "$INFO" ]
  then
    echo "Key not found in $DATA, key: $KEY"
    exit 1;
  fi
  echo "$INFO"
}
bash subshell
  • 2 个回答
  • 284 Views
Martin Hope
Brad Parks
Asked: 2021-10-01 06:27:59 +0800 CST

采购脚本来更改密码,忽略命令末尾的管道?

  • 1

我写了一个小脚本,可以让我切换到路径上的脚本目录,这有助于我快速找到我想要编辑的其他脚本,与原始脚本位于同一文件夹中。

我倾向于通过回顾历史来做到这一点,并为该命令加上前缀,无论它是什么,就像这样

$ . cdbin my_super_script

这在大多数情况下都有效 - 但是 - 当我运行的最后一个命令在某处通过管道传输时,它不起作用,

例如

# if my last command was the following
$ my_super_script | grep -v bad | grep good

# then when I try and run my cdbin script using history, it looks like this
$ . cdbin my_super_script | grep -v bad | grep good

然后它什么也没做 - 没有错误,它只是让我留在我的当前目录中。

这是我的简单cdbin脚本:

cdbin

PROG=$(which "$1")
DIR=$(dirname "$PROG")
cd "$DIR" 

编辑:请注意,我知道我可以在大多数情况下简单地输入类似以下内容来避免这种情况,例如,$ . cdbin !:0但发现输入有点尴尬:D

pipe cd-command
  • 1 个回答
  • 41 Views
Martin Hope
Brad Parks
Asked: 2019-10-26 04:15:41 +0800 CST

如何编写创建多个终端窗格的脚本,如 tmux?

  • 4

我想编写一些脚本,可以在一个窗口中打开多个终端窗格,很像tmuxor screen。

我个人非常精通tmux,并且已经使用了多年,但我们团队中的其他开发人员不想知道任何关于它或其他屏幕多路复用器的信息。

为此,我想编写可以在单个窗口的多个窗格中划分自己的脚本。这可以直接使用bash吗?

我的目标是

  • 避免要求node或需要安装的东西(如blessed-contrib),以使过程尽可能简单。
  • 使用大多数发行版上预装的东西编写它,并且与 posix 兼容。
  • 有一个终端窗口,它被分成多个窗格,其中每个窗格都可以是一个正在运行的进程或命令提示符。
  • 当有人点击ctrl+c它时,它会退出所有窗格,并返回到单个终端。
bash
  • 1 个回答
  • 880 Views
Martin Hope
Brad Parks
Asked: 2019-02-02 05:39:50 +0800 CST

Bash - 从任意文本中按名称提取键值?

  • 0

我想要一个简单的脚本,让我可以将任何文本传递给它,并从字符串中提取一个键的值。

我希望它灵活,并接受 XML 或 JSON 输入,甚至是格式不正确的输入,例如日志中的文本。

例如,给定以下任何输入,它应该能够提取test键的值。

例如

$ echo "test:5 hi there" | extract_key_value test

应该导致

5

请注意,我不在乎它是用什么写的,所以 node、ruby 等对我来说很好,但是可移植性(Linux/osx)很好;-)

输入1

this is test:5 i saw a value

输入2

this is test:'another value' i saw a value

输入3

this is test=5 i saw a value

输入4

test='a string value here'

输入5

my data
on line 2 test='a string value here'
more data

我对此的快速破解如下,我觉得可以大大改进,感觉应该在某个地方解决!

提取键值

#!/usr/bin/env bash

function show_help()
{
  IT=$(cat <<EOF
  
  Helps you extract a key value from a string, typically a log msg

  usage: key {keyBeginDelim} {keyEndDelim}

  e.g. given "asd f asdf asdf test=easy asdf me=you" as input

  extract_key_value test        
  => returns easy
EOF
)
  echo "$IT"
  exit
}

if [ "$1" == "help" ]
then
  show_help
fi
if [ -z "$1" ]
then
  show_help
fi

INPUT=$(cat -)
KEY="$1"

function getVal()
{
  DELIM1="$1"
  DELIM2="$2"
  echo "$INPUT" | awk -F "$DELIM1" '{print $2}' | awk -F "$DELIM2" '{print $1}'
}

# Try whatever the user passed in or defaults for delims
if [ -n "$2" ]
then
  IT=$(getVal "$2" "$3")
fi

# Try other use cases
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY:'" "'")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY='" "'")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY=\"" "\"")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY:\"" "\"")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY:" " ")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY=" " ")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY=" ";")
fi
if [ -z "$IT" ]
then
  IT=$(getVal "$KEY:" ";")
fi

echo "$IT"
bash
  • 2 个回答
  • 546 Views
Martin Hope
Brad Parks
Asked: 2018-04-14 07:10:05 +0800 CST

如何等待程序完成,并在完成后将标准输入管道传输到标准输出?

  • 6

我试图弄清楚如何等待命令完成,然后将标准输入通过管道传输到标准输出。我在mac上,但我认为我的问题更多是关于如何等待进程完成和管道输出,然后是与mac有关的任何事情。

我注意到在 mac 上,我可以一起运行几个say命令,如果我使用操作符将它们​​连接起来,它们会等待每个短语完全说出,然后再开始下一个&&。

$ say "stage 1" && say "stage 2"

这就是真正的用例所在——我有一个 bash 脚本,我想让它在它说完之后将它传递给 stdout 。

$ cat /etc/passwd | say_and_pass "stage 1" | grep -v test | say_and_pass "stage 2"

所以从概念上讲,这会大声说“阶段 1”,然后立即说“阶段 2”,然后将 grepped 的内容转储/etc/password到标准输出。

我最初对say_and_pass脚本的破解是这样的:

say_and_pass

#!/usr/bin/env bash
OUT="$*"
say "$OUT" && cat 

但这似乎不起作用;-)

编辑:我将上面的示例更改为say_and_pass "stage2"用作最终命令,这是我的解决方案工作所必需的......

bash
  • 6 个回答
  • 10760 Views
Martin Hope
Brad Parks
Asked: 2018-03-24 05:35:14 +0800 CST

Bash:从标准输入或命令行参数循环遍历行的简洁方法?

  • 10

我有一个 bash 脚本,我想遍历 stdin 中的行,或者遍历传入的每个参数。

有没有一种干净的方法来写这个,所以我不必有 2 个循环?

#!/bin/bash

# if we have command line args... 
if [ -t 0 ]
then
  # loop over arguments
  for arg in "$@" 
  do
    # process each argument
  done
else
  # loop over lines from stdin
  while IFS= read -r line; do
    # process each line
  done
fi

编辑:我正在寻找一个只使用单个循环的通用解决方案,因为我发现我想经常这样做,但总是写出 2 个循环然后调用一个函数。所以也许有什么东西可以把标准输入变成一个数组,所以我可以用一个循环来代替?

bash
  • 3 个回答
  • 8637 Views
Martin Hope
Brad Parks
Asked: 2018-02-01 09:10:25 +0800 CST

对所有应用程序强制使用 TLS 1.2 系统范围?

  • 2

我工作的组织有很多运行 Linux 的服务器。

我们最近的任务是确保 TLS 1.2 用于我们所有应用程序的所有出站连接,无论我们的应用程序是在哪个开发平台上编写的,这变化很大(Ruby/Node/Java/PHP)

有没有办法在系统级别强制一切使用 TLS 1.2?

linux security
  • 2 个回答
  • 3561 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve