Skip to content

Commit

Permalink
+ Added an script to automate the string replace process with sed. Mo…
Browse files Browse the repository at this point in the history
…stly used when provisioning vagrant boxes.

+  Small changes in the applescript scripts.
  • Loading branch information
luismartingil committed Aug 2, 2013
1 parent d93a420 commit 997e9b2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Empty file modified iterm_launcher01.applescript
100644 → 100755
Empty file.
Empty file modified iterm_launcher02.applescript
100644 → 100755
Empty file.
64 changes: 64 additions & 0 deletions sed_multiple_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
#
# Bash script to replace chars in multiple files using sed.
# Backslashes are allowed in the string
#
# Author: luismartingil
# Year: 2013
#

# Some variables to be changed.
URL=http://127.0.0.1
PORT=7788
FILE1=/etc/myprogram1.conf
FILE2=/etc/myprogram2.conf

# Be careful while editing this array.
# Items should be 'ori_str' 'dst_str' 'path_file'
array_changes=(

# Example1. 'ori_str' 'dst_str' 'path_file'
'define("URL", "_url_");'
'define("URL", "'$URL'");'
$FILE1

# Example2. 'ori_str' 'dst_str' 'path_file'
'define("PORT", "_port_");'
'define("PORT", "'$PORT'");'
$FILE1

# Example3. 'ori_str' 'dst_str' 'path_file'
';http://domain.net'
';http://domain.net/mypath'
$FILE2

# Example4. 'ori_str' 'dst_str' 'path_file'
';debug=info'
'debug=info'
$FILE2
)

# Don't modify!
# Looping over the array_changes array applying the sed translations.
# Looping technique is a little bit ugly...
i=0
for item in "${array_changes[@]}"
do
n=$((i%3))
# Source string for the sed command
if [ "$n" -eq "0" ]
then
src=$(echo "$item" | sed 's/\//\\\//g')
# Dest string for the sed command
elif [ "$n" -eq "1" ]
then
dst=$(echo "$item" | sed 's/\//\\\//g')
else
# File for the sed command
f=$item
str="s/"$src"/"$dst"/g"
cmd=( sudo sed -i "'"$str"'" $f )
eval "${cmd[@]}"
fi
(( ++i ))
done

0 comments on commit 997e9b2

Please sign in to comment.