Bash asks for replacement of strings and variables in files?
So I'm looking at how I can put together a Bash script that can give hints to use to bulk change lines and variables in files in files, and apply the changes to all files that have that line or variable so that a new one.
For example, I had a lot of files that have a string with a numeric value and I want to change it to a new value, i.e.
font-size=30
and let's say that I want to change the value 30 to another value 25. I know it can be done by doing:
find . -type f -exec sed -i 's/font-size=30/font-size=25/g' {} \;
but if I wanted it to be difficult to allow the user to change any value by entering it themselves at the Bash prompt, like so:
Search and replace all font values of %n
Font size Value = 30
Enter new value:
How can I do this as an impenetrable prompt asking for user input? Thus, not only the opportunity to find and replace all instances of the values related to Font-size=
, but also search and replace the other values, such as x
and y
position values.
All I basically want to do is create a menu where you have to select from the menu what you want to do and then do what I described above. Give it an input file or directory containing a bunch of files, like this:
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>
You can use read
to create an interactive bash
script. For more information on this utility, see the corresponding manpage .
Take a look at the following example, which you can easily expand to your needs:
#!/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
source to share
#!/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" {} \;
source to share