# do ". acd_func.sh"
# acd_func 1.0.5, 10-nov-2004
# petar marinov, http:/geocities.com/h2428, this is public domain
cd_func ()
{
local x2 the_new_dir adir index
local -i cnt
if [[ $1 == "--" ]]; then
dirs -v
return 0
fi
the_new_dir=$1
[[ -z $1 ]] && the_new_dir=$HOME
if [[ ${the_new_dir:0:1} == '-' ]]; then
#
# Extract dir N from dirs
index=${the_new_dir:1}
[[ -z $index ]] && index=1
adir=$(dirs +$index)
[[ -z $adir ]] && return 1
the_new_dir=$adir
fi
#
# '~' has to be substituted by ${HOME}
[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"
#
# Now change to the new dir and add to the top of the stack
pushd "${the_new_dir}" > /dev/null
[[ $? -ne 0 ]] && return 1
the_new_dir=$(pwd)
#
# Trim down everything beyond 11th entry
popd -n +11 2>/dev/null 1>/dev/null
#
# Remove any other occurence of this dir, skipping the top of the stack
for ((cnt=1; cnt <= 10; cnt++)); do
x2=$(dirs +${cnt} 2>/dev/null)
[[ $? -ne 0 ]] && return 0
[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
if [[ "${x2}" == "${the_new_dir}" ]]; then
popd -n +$cnt 2>/dev/null 1>/dev/null
cnt=cnt-1
fi
done
return 0
}
alias cd=cd_func
if [[ $BASH_VERSION > "2.05a" ]]; then
# ctrl+w shows the menu
bind -x "\"\C-w\":cd_func -- ;"
fi
cd /some/where/long
src=`pwd`
cd /other/where/long
dest=`pwd`
cp $src/foo $dest
command completion will work by expanding the variable, so you can use tab completion to specify a file you're working with.
e="\033["
for f in 0 7 `seq 6`; do
no="" bo=""
for b in n 7 0 `seq 6`; do
co="3$f"; p=" "
[ $b = n ] || { co="$co;4$b";p=""; }
no="${no}${e}${co}m ${p}${co} ${e}0m"
bo="${bo}${e}1;${co}m ${p}1;${co} ${e}0m"
done
echo -e "$no\n$bo"
done
256彩色演示:
yes "$(seq 232 255;seq 254 -1 233)" |
while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
$ touch "with spaces" withoutspaces
$ for i in `ls *`; do echo $i; done
with
spaces
withoutspaces
$ IFS="
"
$ for i in `ls *`; do echo $i; done
with spaces
withoutspaces
$ cat myscript.sh
#!/bin/sh
echo -e "one\nthree"
$
$ ./myscript.sh
one
three
$
$ cat expected_output.txt
one
two
three
$
$ diff <(./myscript.sh) expected_output.txt
1a2
> two
$
shopt -s progcomp
complete -A stopped -P '%' bg
complete -A job -P '%' fg jobs disown wait
complete -A variable readonly export
complete -A variable -A function unset
complete -A setopt set
complete -A shopt shopt
complete -A helptopic help
complete -A alias alias unalias
complete -A binding bind
complete -A command type which \
killall pidof
complete -A builtin builtin
complete -A disabled enable
while getopts 'vo:' flag; do
case "$flag" in
'v')
VERBOSE=1
;;
'o')
OUT="$OPTARG"
;;
esac
done
shift "$((OPTIND-1))"
Xargs (1) :
我有一个三核处理器,喜欢运行脚本执行压缩,或一些其他 CPU 密集型串行操作的一组文件。我喜欢使用 xargs作为作业队列来加快速度。
if [ "$#" -gt 1 ]; then
# schedule using xargs
(for file; do
echo -n "$file"
echo -ne '\0'
done) |xargs -0 -n 1 -P "$NUM_JOBS" -- "$0"
else
# do the actual processing
fi
# TERM or QUIT probably means the system is shutting down; make sure history is
# saved to $HISTFILE (does not do this by default)
trap 'logout' TERM QUIT
# save history when signalled by cron(1) script with USR1
trap 'history -a && history -n' USR1