From: Nicolas Boisselier Date: Mon, 17 Nov 2014 22:14:51 +0000 (+0100) Subject: bin + etc X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=a2030a12a3678652cfb67adcc2e04afb06c18bbb;p=nb.git bin + etc --- diff --git a/bin/nb-install b/bin/nb-install index ff5dea78..eab3f617 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -1,17 +1,28 @@ #!/usr/bin/env bash +. "${BASH_SOURCE%/*}/../etc/profile.sh" || exit declare -r NAME="$(basename "${0}")" -declare -r NB_ROOT="$(readlink -f $(dirname $0)/../)" + +declare install="rsync -au --force" +case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac +case "$@" in *-v*) install="$install -v";; esac +case "$@" in *-n*) install="$install -n";; esac + +function verbose() { + printf '\n> %s\n\n' "$@" +} # # Cron # -rsync -au --force etc/cron/nb /etc/cron.d/nb +verbose "Install /etc/cron.d/nb" +$install "$NB_ROOT/etc/cron/nb" /etc/cron.d/nb # # Profile # +verbose "Install /etc/profile.d/nb-profile.sh" echo "[ -x $NB_ROOT/etc/profile.sh ] && . $ROOT/etc/profile.sh" > /tmp/nb-profile.sh \ - && rsync -c -au --force /tmp/nb-profile.sh /etc/profile.d/nb-profile.sh \ + && $install -c /tmp/nb-profile.sh /etc/profile.d/nb-profile.sh \ && chmod 755 /etc/profile.d/nb-profile.sh \ ; diff --git a/bin/nb-update b/bin/nb-update index ffadf6f3..0e6faedb 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -1,8 +1,6 @@ #!/usr/bin/env bash - +. "${BASH_SOURCE%/*}/../etc/profile.sh" || exit declare -r NAME="$(basename "${0}")" -declare -r NB_ROOT="$(readlink -f $(dirname $0)/../)" -PATH="$PATH:$NB_ROOT/bin" ##################################################################### # Git pull diff --git a/etc/bashrc b/etc/bashrc index b2c6c515..3c859453 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -1,40 +1,15 @@ -################################################################################# -# -# FUNCTIONS -# -################################################################################# -git-commit-push() { - git commit -m "$@" -a - git 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" -} - +declare -r NB_ROOT=$(realpath ${BASH_SOURCE%/*}/..) +. "${BASH_SOURCE%/*}/bashrc.function" +. "${BASH_SOURCE%/*}/bashrc.alias" ################################################################################# # # VARS # ################################################################################# -declare -r NB_ROOT="$(readlink -f $(dirname $0)/../)" [ -z "$HOSTNAME" ] && HOSTNAME=`hostname` [ -z "$UID" ] && UID=`id -u` [ -z "$USER" ] && USER=`whoami` -[ -z "$HOME" ] && HOME=`readlink -f ~/` +[ -z "$HOME" ] && HOME=`realpath ~/` ################################################################################# # @@ -102,220 +77,3 @@ else fi unset color char -################################################################################# -# -# Aliases -# -################################################################################# - -#-------------------------------------------------------------------------------- -# Others -# -alias uuid-hds='blkid -o full -s UUID' -alias myip='lynx --dump --source http://www.ipchicken.com/ | sed -ne "s/^.*[^0-9]\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*$/\1/p"' -alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" -alias password-gener='apg -m 12 -x 12 -a 0 -M L|head -1;' -alias mysqlshow-view="mysql -e \"SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'\"" -alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" -alias arp-list="arp -an" - -#-------------------------------------------------------------------------------- -# 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 -[ "$ls_opt" == "" ] || alias ls="ls $ls_opt" -unset ls_opt -alias ll='ls -lh' -unset color_prompt - -#-------------------------------------------------------------------------------- -# Etherwake -# Cable: alias advent-on="etherwake -i eth0 00:22:68:7b:fb:99 -D -b" -# -#which etherwake &> /dev/null && alias advent-on="etherwake -i eth0 48:5b:39:eb:db:8a -D -b" - -#-------------------------------------------------------------------------------- -# seq / jot -# -case "$OSTYPE" in - darwin*) alias seq='jot' ;; - *) alias jot='seq' ;; -esac - -#-------------------------------------------------------------------------------- -# Beep -# -alias beep='printf "\a"' - -#-------------------------------------------------------------------------------- -# Mac iTerm.app -# -test "$TERM_PROGRAM" = "iTerm.app" && export PROMPT_COMMAND='printf "\033]0;%s\007" "${USER}@${HOSTNAME%%.*}"' - -#-------------------------------------------------------------------------------- -# Get / Head -# -alias Head="lynx -dump -head -width=1000" -alias Get="lynx -source -width=1000" - -#-------------------------------------------------------------------------------- -# bc -# -alias bc="bc -q" - -#-------------------------------------------------------------------------------- -# iconv -# -alias latin1_utf8='iconv -f latin1 -t utf8' -alias utf8_latin1='iconv -f utf8 -t latin1' - -#-------------------------------------------------------------------------------- -# vim -# -alias vi=vim - -#-------------------------------------------------------------------------------- -# xterm -# -[ -z /usr/bin/xterm ] && alias xterm="xterm -bg black -fg gray -fn 10x20" - -#-------------------------------------------------------------------------------- -# Torrent -# -opt="--max_upload_rate 1 --minport 6881 --maxport 6999" -if which btlaunchmanycurses.py &> /dev/null; then - - opt="$opt --max_download_rate 150 --max_files_open 0" - - alias torrent="btdownloadcurses.py $opt" - - alias torrentdir="btlaunchmanycurses.py $opt" - -elif which btdownloadcurses &> /dev/null; then - - alias torrent="btdownloadcurses $opt" - alias torrentdir="btlaunchmanycurses $opt" - -fi -unset opt - -#-------------------------------------------------------------------------------- -# debian upgrade -# -alias servers-upgrade='server-package.pl update,upgrade $(printf "localhost\nbig\n";mysql -h mysql izi -NBe "select host from server where not hidden")' - -#-------------------------------------------------------------------------------- -# Screen -# -#alias "screen-ssh"="screen -Rd" - -#-------------------------------------------------------------------------------- -# Mac OS -# - -case "$OSTYPE" in darwin*) - - alias strace='dtruss' - alias updatedb='bash -c "cd / && /usr/libexec/locate.updatedb &"' - alias mac-dns-flush='dscacheutil -flushcache' - #alias mac-wakeup-screen="pmset schedule wake '\$(date +\"%d/%m/%Y %T\")'" - #alias mac-wakeup-screen2='osascript -e \'tell application "System Events" to key code 123\'' - alias mac-sleep='pmset sleepnow' - alias mac-halt='halt=1 && bash -c "(sleep 3 && halt) &" && logout' - alias mac-top-proc="echo 'TOP PROCESSES:'; ps -Aro'%cpu, ucomm, user' | grep $USER | grep -v ' 0.0 '" - alias mac-automount='sudo automount -v' - alias mac-vlc-playlists="open -a vlc --args /home/nico/Music/*/*.m3u" - # NB 25.02.14 alias hibernate='sudo pmset -a hibernatemode 25' - # NB 25.02.14 alias sleep='sudo pmset -a hibernatemode 0' - # NB 25.02.14 alias safesleep='sudo pmset -a hibernatemode 3' - # NB 25.02.14 alias smartsleep='sudo pmset -a hibernatemode 2' - mac-xmessage() { - osascript -e 'tell app "System Events" to display dialog "'$@'"' - } - - mac_config() { - echo "Finder path : YES" - defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES - echo "Finder quit : YES" - defaults write com.apple.finder QuitMenuItem -bool YES - echo "Finder show all files : FALSE" - defaults write com.apple.finder AppleShowAllFiles FALSE - } - -esac - -################################################################################# -# -# Functions -# -################################################################################# -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 -} diff --git a/etc/bashrc.alias b/etc/bashrc.alias new file mode 100644 index 00000000..37b1c53b --- /dev/null +++ b/etc/bashrc.alias @@ -0,0 +1,137 @@ +################################################################################# +# +# Aliases +# +################################################################################# + +#-------------------------------------------------------------------------------- +# Others +# +alias uuid-hds='blkid -o full -s UUID' +alias myip='lynx --dump --source http://www.ipchicken.com/ | sed -ne "s/^.*[^0-9]\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*$/\1/p"' +alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" +alias password-gener='apg -m 12 -x 12 -a 0 -M L|head -1;' +alias mysqlshow-view="mysql -e \"SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'\"" +alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" +alias arp-list="arp -an" + +#-------------------------------------------------------------------------------- +# 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 +[ "$ls_opt" == "" ] || alias ls="ls $ls_opt" +unset ls_opt +alias ll='ls -lh' +unset color_prompt + +#-------------------------------------------------------------------------------- +# Etherwake +# Cable: alias advent-on="etherwake -i eth0 00:22:68:7b:fb:99 -D -b" +# +#which etherwake &> /dev/null && alias advent-on="etherwake -i eth0 48:5b:39:eb:db:8a -D -b" + +#-------------------------------------------------------------------------------- +# seq / jot +# +case "$OSTYPE" in + darwin*) alias seq='jot' ;; + *) alias jot='seq' ;; +esac + +#-------------------------------------------------------------------------------- +# Beep +# +alias beep='printf "\a"' + +#-------------------------------------------------------------------------------- +# Mac iTerm.app +# +test "$TERM_PROGRAM" = "iTerm.app" && export PROMPT_COMMAND='printf "\033]0;%s\007" "${USER}@${HOSTNAME%%.*}"' + +#-------------------------------------------------------------------------------- +# Get / Head +# +alias Head="lynx -dump -head -width=1000" +alias Get="lynx -source -width=1000" + +#-------------------------------------------------------------------------------- +# bc +# +alias bc="bc -q" + +#-------------------------------------------------------------------------------- +# iconv +# +alias latin1_utf8='iconv -f latin1 -t utf8' +alias utf8_latin1='iconv -f utf8 -t latin1' + +#-------------------------------------------------------------------------------- +# vim +# +alias vi=vim + +#-------------------------------------------------------------------------------- +# xterm +# +[ -z /usr/bin/xterm ] && alias xterm="xterm -bg black -fg gray -fn 10x20" + +#-------------------------------------------------------------------------------- +# Torrent +# +opt="--max_upload_rate 1 --minport 6881 --maxport 6999" +if which btlaunchmanycurses.py &> /dev/null; then + + opt="$opt --max_download_rate 150 --max_files_open 0" + + alias torrent="btdownloadcurses.py $opt" + + alias torrentdir="btlaunchmanycurses.py $opt" + +elif which btdownloadcurses &> /dev/null; then + + alias torrent="btdownloadcurses $opt" + alias torrentdir="btlaunchmanycurses $opt" + +fi +unset opt + +#-------------------------------------------------------------------------------- +# debian upgrade +# +alias servers-upgrade='server-package.pl update,upgrade $(printf "localhost\nbig\n";mysql -h mysql izi -NBe "select host from server where not hidden")' + +#-------------------------------------------------------------------------------- +# Screen +# +#alias "screen-ssh"="screen -Rd" + +#-------------------------------------------------------------------------------- +# Mac OS +# +case "$OSTYPE" in darwin*) + + alias strace='dtruss' + alias updatedb='bash -c "cd / && /usr/libexec/locate.updatedb &"' + alias mac-dns-flush='dscacheutil -flushcache' + #alias mac-wakeup-screen="pmset schedule wake '\$(date +\"%d/%m/%Y %T\")'" + #alias mac-wakeup-screen2='osascript -e \'tell application "System Events" to key code 123\'' + alias mac-sleep='pmset sleepnow' + alias mac-halt='halt=1 && bash -c "(sleep 3 && halt) &" && logout' + alias mac-top-proc="echo 'TOP PROCESSES:'; ps -Aro'%cpu, ucomm, user' | grep $USER | grep -v ' 0.0 '" + alias mac-automount='sudo automount -v' + alias mac-vlc-playlists="open -a vlc --args /home/nico/Music/*/*.m3u" + # NB 25.02.14 alias hibernate='sudo pmset -a hibernatemode 25' + # NB 25.02.14 alias sleep='sudo pmset -a hibernatemode 0' + # NB 25.02.14 alias safesleep='sudo pmset -a hibernatemode 3' + # NB 25.02.14 alias smartsleep='sudo pmset -a hibernatemode 2' + +esac diff --git a/etc/bashrc.function b/etc/bashrc.function new file mode 100644 index 00000000..d8c260ac --- /dev/null +++ b/etc/bashrc.function @@ -0,0 +1,108 @@ +################################################################################# +# +# FUNCTIONS +# +################################################################################# +case "$OSTYPE" in darwin*) + mac-xmessage() { + osascript -e 'tell app "System Events" to display dialog "'$@'"' + } + + mac-config() { + echo "Finder path : YES" + defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES + echo "Finder quit : YES" + defaults write com.apple.finder QuitMenuItem -bool YES + echo "Finder show all files : FALSE" + defaults write com.apple.finder AppleShowAllFiles FALSE + } + +esac + +git-commit-push() { + git commit -m "$@" -a + git 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" +} + +function realpath() { + perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )' $1 +} + +function ascii() { + perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }' +} + +function 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 +} + +function 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" +} + +function 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 +} diff --git a/etc/profile.sh b/etc/profile.sh index 6286e163..ed92e40e 100644 --- a/etc/profile.sh +++ b/etc/profile.sh @@ -1,3 +1,2 @@ #!/usr/bin/env bash -declare -r NB_ROOT="$(readlink -f $(dirname $0)/../)" -[ -r "$NB_ROOT/etc/bashrc" ] && . "$NB_ROOT/etc/bashrc" +[ -r "${BASH_SOURCE%/*}/bashrc" ] && . "${BASH_SOURCE%/*}/bashrc"