From 20eeba11e95b611796aeb52c569dc71f6efe3242 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 9 Jun 2015 10:01:20 +0100 Subject: [PATCH] other sheel --- etc/bashrc | 23 ++--- etc/bashrc.function | 228 ++++++++++++++++++++++++-------------------- etc/profile.sh | 9 ++ 3 files changed, 142 insertions(+), 118 deletions(-) diff --git a/etc/bashrc b/etc/bashrc index 2992a5b5..f7d5a03e 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -15,7 +15,7 @@ NB_ROOT=$(realpath ${BASH_SOURCE%/*}/..) # # PATH # -PATH=`env-add-path "$PATH" \ +PATH=`nb_env_add_path "$PATH" \ /bin \ /sbin \ /usr/sbin \ @@ -34,9 +34,9 @@ PATH=`env-add-path "$PATH" \ ` export PATH -export RUBYLIB=`env-add-path "$RUBYLIB" $NB_ROOT/lib` -export PERL5LIB=`env-add-path "$PERL5LIB" $NB_ROOT/lib` -export PYTHONPATH=`env-add-path "$PYTHONPATH" $NB_ROOT/lib` +export RUBYLIB=`nb_env_add_path "$RUBYLIB" $NB_ROOT/lib` +export PERL5LIB=`nb_env_add_path "$PERL5LIB" $NB_ROOT/lib` +export PYTHONPATH=`nb_env_add_path "$PYTHONPATH" $NB_ROOT/lib` # # OTHERS @@ -83,15 +83,12 @@ fi # ls # ls_opt='-a' -case "$OSTYPE" in - darwin*) - [ "$color_prompt" = yes ] && ls_opt="$ls_opt -G" - ;; - *) - ls_opt="$ls_opt --time-style=long-iso" - [ "$color_prompt" = yes ] && ls_opt="$ls_opt --color=auto" - ;; -esac +if [ -n "$DARWIN" ]; then + [ "$color_prompt" = yes ] && ls_opt="$ls_opt -G" +else + ls_opt="$ls_opt --time-style=long-iso" + [ "$color_prompt" = yes ] && ls_opt="$ls_opt --color=auto" +fi [ "$ls_opt" == "" ] || alias ls="ls $ls_opt" unset ls_opt alias ll='ls -lh' diff --git a/etc/bashrc.function b/etc/bashrc.function index 30eed1e8..2eb50506 100644 --- a/etc/bashrc.function +++ b/etc/bashrc.function @@ -1,13 +1,127 @@ ################################################################################# # -# 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 \"$@\"" @@ -22,11 +136,10 @@ case "$OSTYPE" in darwin*) 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; @@ -120,89 +233,6 @@ git-commit-push-add() { 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 @@ -213,19 +243,6 @@ puppet-upgrade-modules() { 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 "$@" } @@ -240,10 +257,11 @@ ls-tree() { 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 } diff --git a/etc/profile.sh b/etc/profile.sh index ed92e40e..17a96433 100644 --- a/etc/profile.sh +++ b/etc/profile.sh @@ -1,2 +1,11 @@ #!/usr/bin/env bash + +case "$SHELL" in + */zsh) BASH_SOURCE=${(%):-%N} ;; +esac + +case "$OSTYPE" in darwin*) + DARWIN=1 +esac + [ -r "${BASH_SOURCE%/*}/bashrc" ] && . "${BASH_SOURCE%/*}/bashrc" -- 2.47.3