【问题标题】:Bash prompts for replacing strings and variables in files?Bash 提示替换文件中的字符串和变量?
【发布时间】:2017-07-31 07:35:18
【问题描述】:

因此,我正在寻找如何组合一个 Bash 脚本,该脚本可以提示用于批量更改文件中的文件中的字符串和变量,并将更改应用于具有该字符串或变量的所有文件到新文件。

例如,我有一堆文件,其中包含一个带有数字值的字符串,我想将其更改为新值,即;

font-size=30

假设我想将 30 更改为不同的数值 25。我知道这可以通过这样做来实现;

find . -type f -exec sed -i 's/font-size=30/font-size=25/g' {} \;

但是,如果我想让用户通过在 bash 提示符中键入自己的值来更改为任何值是棘手的,就像这样,即;

Search and replace all font values of %n
Font size Value = 30
Enter new value: 

作为一个要求用户输入的棘手提示,我如何能够做到这一点?因此,不仅能够搜索和替换属于 Font-size= 的值的所有实例,还能够搜索和替换其他值,例如 x 和 y 位置值。

我基本上要做的就是制作一个提示菜单,您必须从菜单中选择您想要做的事情,然后按照我上面描述的那样做。给它一个输入文件,或者一个包含一堆文件的目录,例如:

Choose from menu [1-3]:

1 - Replace Font-size values
2 - Replace X and Y values
3 - Exit

- - -
Select file or directory target: <user_input_here>

【问题讨论】:

  • 您的意思是交互式而不是棘手(根据 Merriam-Webster 的说法,不容易管理、管理或指导)?
  • 互动、棘手,到底有什么区别?
  • 它们是两个完全不同的术语,含义完全不同。我能想到的唯一关系是它们都是英文单词。
  • 文件的格式是什么?
  • 如果所有文件都在同一个目录中(或不超过 2 个深度),那么直接使用 sed 和文件通配比使用 find 生成一个单独的 @ 实例要高效得多987654330@ 每个文件一次,例如sed -i 's/font-size=30/font-size=25/g' *.{layout,styles}

标签: bash


【解决方案1】:

您可以使用read 创建交互式bash 脚本。有关此实用程序的更多信息,请参阅相应的manpage。

查看以下示例,您可以轻松地将其扩展到您的需求:

#!/bin/bash

function replace_fontsize {
  read -p "Old font-size: (Press Enter) " old_size
  read -p "New font-size: (Press Enter) " new_size
  read -p "Select file or directory target: (Press Enter) " path
  if [ -d "$path" ] ; then
    find "$path" -type f -exec sed -i 's/font-size=${old_size}/font-size=${new_size}/g' {} \;
  elif [ -f "$path" ] ; then
    sed -i 's/font-size=${old_size}/font-size=${new_size}/g' "$path"
  else
    echo "File/Directory ${path} doesn't exist"
    exit 1
  fi
}

function replace_x_and_y {
  # do whatever you want
  exit 0
}

echo "Choose from menu [1-3]:

1 - Replace Font-size values
2 - Replace X and Y values
3 - Exit"

read -n 1 -s menu

case $menu in
  1) replace_fontsize;;
  2) replace_x_and_y;;
  3) exit 0;;
esac

【讨论】:

    【解决方案2】:
    #!/usr/bin/env bash
    
    # put any X here for X in the format X=<number>
    options=("font-size" "x")
    
    echo "Choose from menu [1-$((${#options[@]}+1))]"
    index=1
    for i in ${options[@]}; do
        echo -e "\t$((index++)) - $i"
    done
    echo -e "\t$((index)) - exit"
    
    read -p 'select: ' sel                                                                                                                                
    if ! [[ $sel =~ [1-9][0-9]* ]]; then
        echo "invalid selection. enter a number greater than 0"
        exit 1
    fi
    if [[ $sel -eq $index ]]; then
        echo "exiting.."
        exit 0
    fi
    echo "updating '${options[$((--sel))]}'"
    read -p "enter the old value: " oval
    read -p "enter the new value: " nval
    echo "updating '${options[$sel]}=$oval' into '${options[$sel]}=$nval'"
    
    # change the directory here if required and update the find query as needed
    find . -type f -exec sed -i "s:${options[$sel]}=$oval:${options[$sel]}=$nval:g" {} \;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 2015-01-29
      • 2020-12-21
      • 2020-08-14
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多