分页: 1 / 1

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

发表于 : 2009-05-08 14:45
justinux
直接上图

代码: 全选

#! /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

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

发表于 : 2009-05-08 15:15
qq274980
不要用 ~/.thum ,改用 $HOME/.thum

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

发表于 : 2009-05-08 15:27
justinux
为什么不能用~呢?是何道理呀?
:em03

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

发表于 : 2009-05-14 21:01
ambitiousboy
挺有意思的问题。

我开始的时候也没有反应过来,然后自己试了一次,就明白了。关键就是系统对 "~/.thumbnail" 和 "$HOME/.thumbnail" 的解释。注意双引号就明白啦。 :em09

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

发表于 : 2009-05-18 17:04
543082593
恩是 ~/thumbnail的问题
这个 在平时的命令行里用还可以 但是 在脚本里 最好 少用 我写了几个脚本里 总是 出错的就是 ~/

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

发表于 : 2009-05-18 17:24
BigSnake.NET
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

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

发表于 : 2009-05-24 2:40
linuxleio
shell script里‘ ’与“ ”惹的祸。虽然单双引用的作用都禁止特殊字符扩展,' '比“ ”禁止更加严格,在'~'里~只是一个普通字符~;而“~”未被禁止扩展,代表$HOME

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

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

代码: 全选

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

echo "[$HOME]"
echo '[$HOME]'
echo "[~/]"
echo '[~/]'
biff@lenovo:~$

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

发表于 : 2009-05-29 1:46
justinux
差点忘记这帖了>__<
現在弄明白了,要用~扩展成HOME路径名,不能把它放在引号内。
引号内的~就成了普通字符了。
:em11

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

发表于 : 2009-06-06 9:11
tusooa
1 echo ~/
2 echo "~/"
3 echo $HOME
4 echo "$HOME"

代码: 全选

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

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

发表于 : 2009-06-07 10:02
wyfhyl
学习一下