帮忙看下这个脚本哪里有错?很短。

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
justinux
帖子: 80
注册时间: 2008-10-10 21:32

帮忙看下这个脚本哪里有错?很短。

#1

帖子 justinux » 2009-05-08 14:45

直接上图

代码: 全选

#! /bin/bash

clr_dir=
clearDir()
{
	if [ -d $clr_dir ]
	then
		rm -R $clr_dir/*
		echo "$clr_dir --- cleared."
	else
		echo "$clr_dir --- not exist!"
	fi
}

clr_dir="~/.thumbnails"
clearDir
~/.thumbnails这个目录是在的,我用bash -x调试,结果如下

代码: 全选

+ clr_dir=
+ clr_dir='~/.thumbnails'
+ clearDir
+ '[' -d '~/.thumbnails' ']'
+ echo '~/.thumbnails --- not exist!'
~/.thumbnails --- not exist!
明明展开名称是目录,怎么走到else那去了? :em20
CPU: AMD Barton 2600+
主板: EPOX 8RDA3+
内存: Kingstone DDR400 512MB x 2
显卡: R9550(128MB)
硬盘: Seagate 250GB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ubuntu 10.04
Opera 10
头像
qq274980
帖子: 69
注册时间: 2009-04-09 9:16

Re: 帮忙看下这个脚本哪里有错?很短。

#2

帖子 qq274980 » 2009-05-08 15:15

不要用 ~/.thum ,改用 $HOME/.thum
头像
justinux
帖子: 80
注册时间: 2008-10-10 21:32

Re: 帮忙看下这个脚本哪里有错?很短。

#3

帖子 justinux » 2009-05-08 15:27

为什么不能用~呢?是何道理呀?
:em03
CPU: AMD Barton 2600+
主板: EPOX 8RDA3+
内存: Kingstone DDR400 512MB x 2
显卡: R9550(128MB)
硬盘: Seagate 250GB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ubuntu 10.04
Opera 10
ambitiousboy
帖子: 44
注册时间: 2007-02-04 14:22

Re: 帮忙看下这个脚本哪里有错?很短。

#4

帖子 ambitiousboy » 2009-05-14 21:01

挺有意思的问题。

我开始的时候也没有反应过来,然后自己试了一次,就明白了。关键就是系统对 "~/.thumbnail" 和 "$HOME/.thumbnail" 的解释。注意双引号就明白啦。 :em09
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

Re: 帮忙看下这个脚本哪里有错?很短。

#5

帖子 543082593 » 2009-05-18 17:04

恩是 ~/thumbnail的问题
这个 在平时的命令行里用还可以 但是 在脚本里 最好 少用 我写了几个脚本里 总是 出错的就是 ~/
fall again
smooth criminal
they don't care about us
billie jean
beat it
dangerous
the lost children
childhood
ben
i will be there
speechless
she is out of my life
rock with you
...
LOVE U FOREVER
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 帮忙看下这个脚本哪里有错?很短。

#6

帖子 BigSnake.NET » 2009-05-18 17:24

qq274980 写了:不要用 ~/.thum ,改用 $HOME/.thum

代码: 全选

   Tilde Expansion
       If a word begins with an unquoted tilde character  (`~'),  all  of  the
       characters  preceding  the  first unquoted slash (or all characters, if
       there is no unquoted slash) are considered a tilde-prefix.  If none  of
       the  characters  in  the tilde-prefix are quoted, the characters in the
       tilde-prefix following the tilde are treated as a possible login  name.
       If  this  login name is the null string, the tilde is replaced with the
       value of the shell parameter HOME.  If HOME is unset, the  home  direc-
       tory  of  the  user executing the shell is substituted instead.  Other-
       wise, the tilde-prefix is replaced with the home  directory  associated
       with the specified login name.

       If  the  tilde-prefix  is  a  `~+', the value of the shell variable PWD
       replaces the tilde-prefix.  If the tilde-prefix is a `~-', the value of
       the  shell variable OLDPWD, if it is set, is substituted.  If the char-
       acters following the tilde in the tilde-prefix consist of a  number  N,
       optionally  prefixed  by  a  `+' or a `-', the tilde-prefix is replaced
       with the corresponding element from the directory stack, as it would be
       displayed by the dirs builtin invoked with the tilde-prefix as an argu-
       ment.  If the characters following the tilde in the  tilde-prefix  con-
       sist of a number without a leading `+' or `-', `+' is assumed.

       If the login name is invalid, or the tilde expansion fails, the word is
       unchanged.

       Each variable assignment is checked for unquoted tilde-prefixes immedi-
       ately following a : or the first =.  In these cases, tilde expansion is
       also performed.  Consequently, one may use file names  with  tildes  in
       assignments  to  PATH,  MAILPATH, and CDPATH, and the shell assigns the
       expanded value.
unquoted tilde character
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
linuxleio
帖子: 11
注册时间: 2009-05-18 6:47

Re: 帮忙看下这个脚本哪里有错?很短。

#7

帖子 linuxleio » 2009-05-24 2:40

shell script里‘ ’与“ ”惹的祸。虽然单双引用的作用都禁止特殊字符扩展,' '比“ ”禁止更加严格,在'~'里~只是一个普通字符~;而“~”未被禁止扩展,代表$HOME
头像
qq274980
帖子: 69
注册时间: 2009-04-09 9:16

Re: 帮忙看下这个脚本哪里有错?很短。

#8

帖子 qq274980 » 2009-05-24 2:46

linuxleio 写了:shell script里‘ ’与“ ”惹的祸。虽然单双引用的作用都禁止特殊字符扩展,' '比“ ”禁止更加严格,在'~'里~只是一个普通字符~;而“~”未被禁止扩展,代表$HOME
不完全正确,简单试试就知道了,

代码: 全选

biff@lenovo:~$ sh a
[/home/biff]
[$HOME]
[~/]
[~/]
biff@lenovo:~$ cat a

echo "[$HOME]"
echo '[$HOME]'
echo "[~/]"
echo '[~/]'
biff@lenovo:~$
头像
justinux
帖子: 80
注册时间: 2008-10-10 21:32

Re: 帮忙看下这个脚本哪里有错?很短。

#9

帖子 justinux » 2009-05-29 1:46

差点忘记这帖了>__<
現在弄明白了,要用~扩展成HOME路径名,不能把它放在引号内。
引号内的~就成了普通字符了。
:em11
CPU: AMD Barton 2600+
主板: EPOX 8RDA3+
内存: Kingstone DDR400 512MB x 2
显卡: R9550(128MB)
硬盘: Seagate 250GB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ubuntu 10.04
Opera 10
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 帮忙看下这个脚本哪里有错?很短。

#10

帖子 tusooa » 2009-06-06 9:11

1 echo ~/
2 echo "~/"
3 echo $HOME
4 echo "$HOME"

代码: 全选

tusooa@tusooa-laptop:~$ sh bashtest
/home/tusooa/
~/
/home/tusooa
/home/tusooa
tusooa@tusooa-laptop:~$

代码: 全选

] ls -ld //
wyfhyl
帖子: 75
注册时间: 2008-06-15 10:54

Re: 帮忙看下这个脚本哪里有错?很短。

#11

帖子 wyfhyl » 2009-06-07 10:02

学习一下
回复