From c87a9f9aaed119cfb371e44c589d1c08a75fc47c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 19 Dec 2016 15:40:58 +0100 Subject: [PATCH] fix bug sh compatible --- bin/nb-update | 3 ++- etc/profile | 2 +- etc/profile.d/aliases | 20 +++++++++--------- etc/profile.d/envs | 2 +- etc/profile.d/functions | 46 ++++++++++++++++++++++++++--------------- etc/profile.d/git.sh | 2 +- etc/profile.d/nico.sh | 8 +++---- 7 files changed, 48 insertions(+), 35 deletions(-) diff --git a/bin/nb-update b/bin/nb-update index 559c039f..9847bd20 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -9,7 +9,8 @@ #. "${BASH_SOURCE%/*}/../etc/profile" || exit #echo "$0 | $(dirname "$0")/.. | $PATH" . $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit -#nb_repos exit; +#exit; +#nb_repos; exit; NAME="$(basename "${0}")" TMP="/tmp/$NAME.tmp" CLEANUP=0 diff --git a/etc/profile b/etc/profile index 152ab35c..41f7c00e 100755 --- a/etc/profile +++ b/etc/profile @@ -46,7 +46,7 @@ else tmp=$PWD for i in /*/*/etc/profile.d/functions /*/*/*/etc/profile.d/functions; do - cd "$(dirname "$i")"/.. &> /dev/null || continue + cd "$(dirname "$i")"/.. 1>/dev/null 2>&1 || continue # Check other files [ -e profile.d/envs ] || continue diff --git a/etc/profile.d/aliases b/etc/profile.d/aliases index a522e17a..273fea3d 100644 --- a/etc/profile.d/aliases +++ b/etc/profile.d/aliases @@ -3,19 +3,19 @@ # Aliases # ################################################################################# -[ -n "$MYVIMRC" ] && shopt -s expand_aliases +[ -n "$MYVIMRC" -a -n "$BASH" ] && shopt -s expand_aliases #-------------------------------------------------------------------------------- # Others # -which blkid &>/dev/null && alias uuid-hds='blkid -o full -s UUID' -which dpkg-query &>/dev/null && alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" -which apg &>/dev/null && alias password-gener='apg -m 12 -x 12 -a 0 -M L|head -1;' -which mysql &>/dev/null && alias mysqlshow-view="mysql -e \"SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'\"" -which nmap &>/dev/null && alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" -which arp &>/dev/null && alias arp-list="arp -an" -which gs &>/dev/null && alias pdf2jpegs='gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=pdf2jpegs%d.jpg -dJPEGQ=100 -r500 -c quit' -which maildirmake &>/dev/null && alias nb-maildir-make="maildirmake -S Maildir; for d in Spam Trash Sent;do maildirmake -f $d Maildir; done" +which blkid 1>/dev/null && alias uuid-hds='blkid -o full -s UUID' +which dpkg-query 1>/dev/null && alias dpkg-sort-size="dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1nr" +which apg 1>/dev/null && alias password-gener='apg -m 12 -x 12 -a 0 -M L|head -1;' +which mysql 1>/dev/null && alias mysqlshow-view="mysql -e \"SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'\"" +which nmap 1>/dev/null && alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" +which arp 1>/dev/null && alias arp-list="arp -an" +which gs 1>/dev/null && alias pdf2jpegs='gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=pdf2jpegs%d.jpg -dJPEGQ=100 -r500 -c quit' +which maildirmake 1>/dev/null && alias nb-maildir-make="maildirmake -S Maildir; for d in Spam Trash Sent;do maildirmake -f $d Maildir; done" #-------------------------------------------------------------------------------- # Beep @@ -30,4 +30,4 @@ which bc 1>/dev/null && alias bc="bc -q" #-------------------------------------------------------------------------------- # vim # -which vim &>/dev/null && alias vi=vim +which vim 1>/dev/null && alias vi=vim diff --git a/etc/profile.d/envs b/etc/profile.d/envs index a2e66b0f..654a26b2 100644 --- a/etc/profile.d/envs +++ b/etc/profile.d/envs @@ -19,7 +19,7 @@ # > env_add_path #We don't want to destroyed variables if function does not exists ! -if true || env_add_path &>/dev/null; then +if true || env_add_path 1>/dev/null; then # # PATH diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 8252cc68..efa1856b 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -14,7 +14,9 @@ nb_repos() { [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" # Realpath - local IFS=$'\n' + #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null + local IFS=" +" for dir in $(ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do # Accept directories or files (ex: use to tag a directory: .nb-install) @@ -34,7 +36,9 @@ nb_repos_ls() { [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos_ls' # for busybox # Look for paths - local d f i repo IFS=$'\n' + local d f i repo + local IFS=" +" for d in `nb_repos`; do for f in "$@"; do @@ -117,7 +121,11 @@ shell_help() { # # Print help message and return true if args contains -?-h(elp)? # - local msg="$1"; shift + local msg="" + if [ "$#" -gt 0 ]; then + msg="$1" + shift + fi local i #while [ $# -gt 0 ]; do @@ -131,7 +139,7 @@ shell_help() { return 0 ;; esac - shift + shift 2>/dev/null || break done return 1 @@ -141,9 +149,13 @@ shell_help_noarg() { # # Print help message and return true if args contains -?-h(elp)? or is empty # - local msg="$1"; shift + local msg="" + if [ "$#" -gt 0 ]; then + msg="$1" + shift + fi - local args=""; args="$@" # zsh compatible + local args="$*" [ -z "$args" ] && args="--help" shell_help "$msg" "$args" @@ -156,7 +168,7 @@ shell_functions() { } shell_ssh() { - local ssh_opt; case "$1" in -*) ssh_opt="$1"; shift ;; esac + local ssh_opt; case "$1" in -*) ssh_opt="$1"; [ "$#" -gt 0 ] && shift ;; esac local server; server="$1"; shift local tmp; tmp="/tmp/$FUNCNAME.$USER" ( @@ -207,7 +219,7 @@ env_add_path() { if ! which realpath >/dev/null; then realpath() { - if which perl &> /dev/null; then + if which perl 1>/dev/null; then perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" else case "$OSTYPE" in @@ -417,7 +429,9 @@ cron_d_install() { fi shell_help_noarg "$usage" "$@" && return 1 - case "$1" in -t|--test) test=1; shift ;; esac + if [ "$#" -gt 0 ]; then + case "$1" in -t|--test) test=1; shift ;; esac + fi #echo "$#"; return if [ $# -gt 3 ]; then @@ -734,8 +748,6 @@ file_user() { ls -dl "$1" | awk '{print $3}' } -# NB 08.12.16: TODEL -unalias alias myip &> /dev/null myip() { local url=''; for url in http://ipinfo.io/ip http://www.ipchicken.com http://icanhazip.com http://ident.me http://nicolas.boisselier.free.f/myip/; @@ -747,13 +759,13 @@ myip() { } http_get() { - if which curl &>/dev/null; then + if which curl 1>/dev/null; then curl -sk $* - elif which wget &>/dev/null; then + elif which wget 1>/dev/null; then wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* - elif which lynx &>/dev/null; then + elif which lynx 1>/dev/null; then lynx --source -dump $* else @@ -763,13 +775,13 @@ http_get() { alias Get=http_get http_head() { - if which curl &>/dev/null; then + if which curl 1>/dev/null; then curl -sk --head $* - elif which wget &>/dev/null; then + elif which wget 1>/dev/null; then zGet --save-headers - $* | perl -ne '/^\s*$/ and last;print' - elif which lynx &>/dev/null; then + elif which lynx 1>/dev/null; then lynx --head -dump $* else diff --git a/etc/profile.d/git.sh b/etc/profile.d/git.sh index de74b713..22fcd79a 100644 --- a/etc/profile.d/git.sh +++ b/etc/profile.d/git.sh @@ -148,7 +148,7 @@ git_conn_check() { ok=0 case "$dest" in *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;; - *) nc -z -w 5 $dest &>/dev/null && ok=1 ;; + *) nc -z -w 5 $dest >/dev/null 2>&1 && ok=1 ;; esac [ -z "$_git_conn_check" ] && _git_conn_check=" " _git_conn_check="${_git_conn_check}$dest=$ok " diff --git a/etc/profile.d/nico.sh b/etc/profile.d/nico.sh index 1fd8d47c..1664b222 100644 --- a/etc/profile.d/nico.sh +++ b/etc/profile.d/nico.sh @@ -21,8 +21,8 @@ #alias torrentdir="btlaunchmanycurses.py --max_download_rate 150 --max_files_open 0" # btdownloadcurses -which btdownloadcurses &>/dev/null && alias torrent="btdownloadcurses --max_upload_rate 1 --minport 6881 --maxport 6999" -which btlaunchmanycurses &>/dev/null && alias torrentdir="btlaunchmanycurses --max_upload_rate 1 --minport 6881 --maxport 6999" +which btdownloadcurses 1>/dev/null && alias torrent="btdownloadcurses --max_upload_rate 1 --minport 6881 --maxport 6999" +which btlaunchmanycurses 1>/dev/null && alias torrentdir="btlaunchmanycurses --max_upload_rate 1 --minport 6881 --maxport 6999" #-------------------------------------------------------------------------------- # xterm @@ -32,7 +32,7 @@ which btlaunchmanycurses &>/dev/null && alias torrentdir="btlaunchmanycurses --m #-------------------------------------------------------------------------------- # debian upgrade # -which server-package.pl &>/dev/null && alias servers-upgrade='server-package.pl update,upgrade $((printf "big.cascais.loc\n";mysql -h mysql izi -NBe "select host from server where not hidden") | sort -u)' +which server-package.pl 1>/dev/null && alias servers-upgrade='server-package.pl update,upgrade $((printf "big.cascais.loc\n";mysql -h mysql izi -NBe "select host from server where not hidden") | sort -u)' #-------------------------------------------------------------------------------- # @@ -75,7 +75,7 @@ nb_alert() { local ip=`dig +short $host` [ -z "$ip" ] && echo "Can't resolve $host" 1>&2 && return 1 - ping -qc1 "$host" &> /dev/null || ( echo "Can't ping $host" 1>&2; return 1 ) + ping -qc1 "$host" >/dev/null 2>&1 || ( echo "Can't ping $host" 1>&2; return 1 ) ips | grep -qxFm1 "$ip" || cmd="ssh -o BatchMode=yes $host -- '$cmd'" -- 2.47.3