34421

Is there a better way to double escape (or escape twice) user input in BASH than calling printf twic

Question:

This script will read an old and a new value from the user and then use sed to find and replace them in a file. For example if I entered T<em>T</em>z and B<em>B</em>z it would look for T\<em>T\</em>z in the file and replace with B\<em>B\</em>z. It works but I've been trying to make this more concise.

I don't have any need for the intermediate variables $ESC_OLD_PW and $ESC_NEW_PW. Is there a more <strong><em>sensible</em></strong> way to do this?

#!/bin/bash read -sp "Old:" OLD_PW && echo read -sp "New:" NEW_PW && echo # Add escape characters to what user entered printf -v ESC_OLD_PW "%q" "${OLD_PW}" printf -v ESC_NEW_PW "%q" "${NEW_PW}" # Escape again for the sed evaluation. printf -v ESC_ESC_OLD_PW "%q" "${ESC_OLD_PW}" printf -v ESC_ESC_NEW_PW "%q" "${ESC_NEW_PW}" sed -i -e s/"${ESC_ESC_OLD_PW}"/"${ESC_ESC_NEW_PW}"/g $1

I've tried the following:

~$ OLD_PW="T*T*z" ~$ printf "%q" $OLD_PW | xargs printf "%q" printf: %q: invalid conversion specification ~$

And I've tried many variations on piping things into printf... Any suggestions?

Answer1:

Intermediate variable-free:

sed -i -e "s/$(printf '%q' $(printf '%q' $OLD_PW))/$(printf '%q' $(printf '%q' $NEW_PW))/g" $1

Recommend

  • simplexml and accessing feedburner's : [duplicate]
  • interpolation in 3d computer graphics
  • check if numbers have the same sign
  • How to remove just the index name and not the content in Pandas multiindex data frame
  • InputDispatcher Error
  • Programmatically check if PHP is installed using Python
  • How to properly add strings to QListWidgets?
  • Quickly or concisely determine the longest string per column in a row-based data collection
  • Concise regex extract function in XSLT 2.0
  • Xamarin iOS debugger not hitting breakpoints
  • How to return a number as a binary string with a set number of bits in python
  • Best way to produce 'colour correct' raster from a PDF or PS with ghostscript
  • Rails 3.2 from SQLite locally to Postgres on Heroku
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • Building jamvm 1.5.4 on OS X Lion
  • How to resolve this packager error on react native Android
  • Select value from xtype selection type checkbox CQ5
  • Portable JRE on Linux - possible?
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Write output of for loop to multiple files
  • How can I get the full list of running processes on a Mac from a python app
  • joining two bezier curves
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Convert SQLite database to XML
  • Get specific string
  • Eclipse CDT error: Unable to compile
  • Read a local file using javascript
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How to extract text from Word files using C#?
  • SSO with signing and signature validation doesn't work
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • WOWZA + RTMP + HTML5 Playback?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Does armcc optimizes non-volatile variables with -O0?
  • Conditional In-Line CSS for IE and Others?