AndreaBuffa
AndreaBuffa
Reputation Top 5%
Andrea Buffa
3 Snippets  (216th place)
Published
0 Channels
Created
2 Channels
Following
97 points  (295th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Junior Judge
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by AndreaBuffa

public by AndreaBuffa  2096  7  5  2

Open a subshell remotely

Suppose you want a script that create a tar, copy and unzip it on a server...
//  A typical use is when you are working on a web site locally and you want to deploy it to a server.
tar -C . -cf - "$dirname" | ssh user@192.168.0.254 "tar -C /www/ -xvf -; # other commands.."
// furthermore you can add whatever command you want, like chmod for your files permissions.. 
;

public by AndreaBuffa  2335  1  5  0

sh: recursive find and replace

sh
# find "X_ABC_IPv4Address" into every *.xml file located under /tmp/test, and replace it with X_ABC_ACLRule. 
# 2>/dev/null avoid error prints when for istance, a symbolink link is found under a subdir 

grep -lr X_ABC_IPv4Address /tmp/test --include *.xml 2>/dev/null | xargs sed -i "s|X_ABC_IPv4Address|X_ABC_ACLRule|g"


;

public by AndreaBuffa  2263  0  5  2

sh: avoid runtime error when a variable is supposed to contain a number but it is actually empty

sh
myNum='';
[ ${myNum:-0} -eq 0 ] && echo ok
# ok

[ ${myNum:-1} -eq 1 ] && echo ok
# ok

[ ${myNum:-1} -gt 0 ] && echo ok
# ok

[ ${myNum:-0} -gt 1 ] || echo "0 is not greater than 1"
# 0 is not greater than 1

# This behaviour comes from the common way to extract a substring from a 
# variable
tmp='mississippi'; echo ${tmp:0:2}
# mi

echo ${tmp:1:1}
# i

# 
echo ${tmp:2}
# ssissippi
;