分页: 1 / 1
为什么会重复显示
发表于 : 2011-08-26 18:24
由 anth
cdd(){
clear
case "$1" in
"h" ) cd /media/15extended/home ;;
"d" ) cdd h; cdd dTa ;; #2011.08.26
"r" ) cdd h; cdd Txt/records ;;
* ) cd "$1" ;;
esac
ls
}
cdd r的实际运行結果是 clear -> cd 主文件夹 -> ls -> clear -> cd 记录文件夹 -> ls -> ls 最后重复了
----------------------
回3楼,也不行,还是重复,看来递归调用自身这个函数只是看起来简单,我绝望了,改回去了
#last modified on 2011.08.26
cdd(){
if [ "$1" = "h" ]; then cd /media/15extended/home; fi
if [ "$1" = "d" ]; then cd /media/15extended/home/dTa; fi
if [ "$1" = "r" ]; then cd /media/15extended/home/Txt/records; fi
cd "$1" 2>/dev/null
clear
ls
}
Re: 为什么会重复显示
发表于 : 2011-08-26 19:32
由 我就是我2

看得有点头痛.函数重用?似乎这个定义有些混乱.
Re: 为什么会重复显示
发表于 : 2011-08-26 20:15
由 fnan
cdd(){
clear
case "$1" in
"h" ) cd /media/15extended/home && ls ;;
"d" ) cdd h; cdd dTa ;; #2011.08.26
"r" ) cdd h; cdd Txt/records ;;
* ) cd "$1" && ls ;;
esac
}
#这样试试
Re: 为什么会重复显示
发表于 : 2011-09-03 16:18
由 tusooa
代码: 全选
Associative arrays are created using declare -A name.
代码: 全选
declare -A directories
directories[h]=...
directories[r]=...
...
ccdd ()
{
if [ "${directories[$1]}" ] ; then
cd "${directories[$1]}"
else
cd "$1"
fi
ls
}
Re: 为什么会重复显示
发表于 : 2011-09-04 21:04
由 fnan
#你这clear放在函数里会造成错觉:
kose5@kose5-Aspire-4552:~$ cat tmp4.sh
#!/bin/bash
cdd(){
cd
case "$1" in
"h" ) cd /media/15extended/home && echo $PWD && ls;;
"d" ) cdd h; cdd dTa ;; #2011.08.26
"r" ) cdd h; cdd Txt/records ;;
* ) cd "$1" && echo $PWD && ls;;
esac
}
clear
cdd "$1"
kose5@kose5-Aspire-4552:~$ ./tmp4.sh r
/media/15extended/home
15e
/home/kose5/Txt/records
recod
kose5@kose5-Aspire-4552:~$
#测试正常。
Re: 为什么会重复显示
发表于 : 2011-09-04 21:08
由 fnan
kose5@kose5-Aspire-4552:~$ ./tmp4.sh /bin
/bin
bash chgrp dnsdomainname hostname ls netstat rbash sync zcat
bunzip2 chmod domainname init-checkconf lsmod nisdomainname readlink tailf zcmp
busybox chown dumpkeys initctl2dot mkdir ntfs-3g rm tar zdiff
bzcat chvt echo ip mknod ntfs-3g.probe rmdir tempfile zegrep
bzcmp cp ed kbd_mode mktemp ntfs-3g.secaudit rnano touch zfgrep
bzdiff cpio egrep kill more ntfs-3g.usermap run-parts true zforce
bzegrep dash false less mount open sed ulockmgr_server zgrep
bzexe date fgconsole lessecho mountpoint openvt setfont umount zless
bzfgrep dbus-cleanup-sockets fgrep lessfile mt pidof setupcon uname zmore
bzgrep dbus-daemon fuser lesskey mt-gnu ping sh uncompress znew
bzip2 dbus-uuidgen fusermount lesspipe mv ping6 sh.distrib unicode_start
bzip2recover dd grep ln nano plymouth sleep vdir
bzless df gunzip loadkeys nc plymouth-upstart-bridge static-sh vmmouse_detect
bzmore dir gzexe login nc.openbsd ps stty which
cat dmesg gzip lowntfs-3g netcat pwd su ypdomainname
kose5@kose5-Aspire-4552:~$
Re: 为什么会重复显示
发表于 : 2011-09-08 13:28
由 anth
tusooa 写了:代码: 全选
Associative arrays are created using declare -A name.
代码: 全选
declare -A directories
directories[h]=...
directories[r]=...
...
ccdd ()
{
if [ "${directories[$1]}" ] ; then
cd "${directories[$1]}"
else
cd "$1"
fi
ls
}
谢谢,这个方法好用,我格了硬盘后只改了directories[h]就一切正常了