#################################################################################
#
-# FUNCTIONS
+# FUNCTIONS - SH
#
#################################################################################
+nb_env_add_path() {
+ # Add paths to a variables
+ # Usage PATH=`sem-nb_env_add_path "$PATH" "/blbabla"`
+ declare env_value=$1; shift
+
+ for p in $@; do
+ [ -e "$p" ] || continue
+ case "${env_value}" in
+ *:$p|*:$p:*|$p:*|$p) continue;;
+ esac
+ [ -z "$env_value" ] || env_value=":${env_value}"
+ env_value="${p}${env_value}"
+ done
+
+ echo "$env_value"
+}
+
+realpath() {
+ perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n"' $1
+}
+
+ascii() {
+ perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }'
+}
+
+psmem() {
+ #ps -C $1 -O rss | xargs -r cat | gawk '\
+ #ps -p `pgrep -f $1` -O rss 2>/dev/null| xargs -r | gawk '\
+ ps=`ps ax -O rss 2>/dev/null`
+ echo "$ps"| awk "\
+BEGIN { count = 0; sum= 0; }
+/^ *[0-9]+/ && /$1/ { count ++; sum += \$2 }
+END {
+ print \"Number of processes =\",count;
+ if (count < 1) { count = 1 }
+ print \"Memory usage per process =\",sum/1024/count, \"MB\";
+ print \"Total memory usage =\", sum/1024, \"MB\";
+}
+"
+ return
+}
+
+unzipurl() {
+ declare url=$1
+ #declare file=`echo "$url"|sed 's,^.*?\([^/]+\.zip\)$,\1,'`
+ declare file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'`
+ [ -z "$file" ] && file="$FUNCNAME.zip"
+ #echo "$url -> $file"
+ wget "$url" -O "$file" && unzip "$file"
+ rm "$file"
+}
+
+bytes2h() {
+ declare in="cat" args=""
+ if [ "$1" == "-regex" ]; then
+ args="regex=$2"
+ shift
+ shift
+ fi
+ [ -z "$@" ] || in="echo $@"
+ $in | perl -pe 'BEGIN{
+ $exp = "([\\d\\.]+)";
+ if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) {
+ shift @ARGV;
+ $exp = $1;
+ }
+ sub oct2h {
+
+ my $o = shift @_;
+ $o = $o * 1024;
+
+ if ($o >= 1099511627776) { return(sprintf("%.2fT",($o/1099511627776))); } # T
+ elsif ($o >= 1073741824) { return(sprintf("%.2fG",($o/1073741824))); } # G
+ elsif ($o >= 1048576) { return(int($o/1048576)."M"); } # M
+ elsif ($o >= 1024) { return(int($o/1024)."K"); } # K
+ else { return $o."bytes"; }
+
+ }
+};
+s/$exp/oct2h($1)/ge;
+' $args
+}
-case "$OSTYPE" in darwin*)
+yaml2perl() {
+ perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@
+}
+
+puppet2yaml() {
+ perl -pe '
+s/^(.*)\{(.*?:)\s*/$1:\n $2\n/;
+s/\s*=>\s*/: /;
+s/,\s*/\n/;
+s/\s*\}\s*//;
+' $@
+}
+
+if [ -n "$DARWIN" ]; then
md5sum() {
md5 $@ | sed "s/^MD5 (\(.*\)) = \(.*\)$/\2 \1/"
}
+fi
+
+#################################################################################
+#
+# CHECK SHELL
+#
+#################################################################################
+case "$SHELL" in
+ */bash) ;;
+ */zsh) ;;
+ *) return 0 ;;
+esac
+
+#################################################################################
+#
+# FUNCTIONS - BASH
+#
+#################################################################################
+if [ -n "$DARWIN" ]; then
mac-xmessage() {
osascript -e "tell app \"System Events\" to display dialog \"$@\""
defaults write com.apple.finder AppleShowAllFiles FALSE
}
-esac
+fi
nb-mails() {
-#perl -MDate::Manip -MEncode -ne '
-printf '| 1-$ cat\nx\n' | mail | perl -MDate::Manip -MEncode -ne '
+ printf '| 1-$ cat\nx\n' | mail | perl -MDate::Manip -MEncode -ne '
BEGIN { $SIG{__WARN__} = sub{ } }
$. == 2 and print;
/^From / .. /^$/ or next;
git-commit-push "$@"
}
-env-add-path() {
- # Add paths to a variables
- # Usage PATH=`sem-env-add-path "$PATH" "/blbabla"`
- declare env_value=$1; shift
-
- for p in $@; do
- [ -e "$p" ] || continue
- case "${env_value}" in
- *:$p|*:$p:*|$p:*|$p) continue;;
- esac
- [ -z "$env_value" ] || env_value=":${env_value}"
- env_value="${p}${env_value}"
- done
-
- echo "$env_value"
-}
-
-realpath() {
- perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n"' $1
-}
-
-ascii() {
- perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }'
-}
-
-psmem() {
- #ps -C $1 -O rss | xargs -r cat | gawk '\
- #ps -p `pgrep -f $1` -O rss 2>/dev/null| xargs -r | gawk '\
- ps=`ps ax -O rss 2>/dev/null`
- echo "$ps"| awk "\
-BEGIN { count = 0; sum= 0; }
-/^ *[0-9]+/ && /$1/ { count ++; sum += \$2 }
-END {
- print \"Number of processes =\",count;
- if (count < 1) { count = 1 }
- print \"Memory usage per process =\",sum/1024/count, \"MB\";
- print \"Total memory usage =\", sum/1024, \"MB\";
-}
-"
- return
-}
-
-unzipurl() {
- declare url=$1
- #declare file=`echo "$url"|sed 's,^.*?\([^/]+\.zip\)$,\1,'`
- declare file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'`
- [ -z "$file" ] && file="$FUNCNAME.zip"
- #echo "$url -> $file"
- wget "$url" -O "$file" && unzip "$file"
- rm "$file"
-}
-
-bytes2h() {
- declare in="cat" args=""
- if [ "$1" == "-regex" ]; then
- args="regex=$2"
- shift
- shift
- fi
- [ -z "$@" ] || in="echo $@"
- $in | perl -pe 'BEGIN{
- $exp = "([\\d\\.]+)";
- if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) {
- shift @ARGV;
- $exp = $1;
- }
- sub oct2h {
-
- my $o = shift @_;
- $o = $o * 1024;
-
- if ($o >= 1099511627776) { return(sprintf("%.2fT",($o/1099511627776))); } # T
- elsif ($o >= 1073741824) { return(sprintf("%.2fG",($o/1073741824))); } # G
- elsif ($o >= 1048576) { return(int($o/1048576)."M"); } # M
- elsif ($o >= 1024) { return(int($o/1024)."K"); } # K
- else { return $o."bytes"; }
-
- }
-};
-s/$exp/oct2h($1)/ge;
-' $args
-}
-
puppet-upgrade-modules() {
local m
local pwd=$PWD
cd $pwd
}
-yaml2perl() {
- perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@
-}
-
-puppet2yaml() {
- perl -pe '
-s/^(.*)\{(.*?:)\s*/$1:\n $2\n/;
-s/\s*=>\s*/: /;
-s/,\s*/\n/;
-s/\s*\}\s*//;
-' $@
-}
-
nb-rb() {
ruby -r/etc/puppet/modules/nb/lib/nb.rb -e "$@"
}
find-sort-mtime() {
(
- case "$OSTYPE" in
- darwin*) find $@ -type f -exec stat -f '%m'$'\t''%N' {} \; ;;
- *) find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.0\+/\1/' ;;
- esac
+ if [ -n "$DARWIN" ]; then
+ find $@ -type f -exec stat -f '%m'$'\t''%N' {} \;
+ else
+ find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.0\+/\1/'
+ fi
) | sort -k1 -n | cut -f 2 | head
}