shell脚本创建符号连接的问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

shell脚本创建符号连接的问题

#1

帖子 543082593 » 2009-05-29 2:09

由于用rhythmbox听歌有的时候实在是不爽 比如 有的时候 把网页向下滑动 会使歌曲突然卡了一下
于是 就写个脚本可以 直接用mplayer播放的 既省了图形界面 又高效率
我在我的个人目录下建了一个 song的文件夹 里面存放我所有歌曲的符号连接
创建符号连接的时候 由于有很多音乐文件 所以 必须自然就用到了 批处理
下面是脚本
#! /bin/bash
DIR=/media/windows_multimedia/music /*这个是我的音乐文件夹

link(){
for i in "$elem"/*
do
if [ -d "$i" ] ; then
elem="$i" /*有点困惑的是 如果 没有这句 会出现类似 死循环的问题 不知道为什么
link "$i" /*递归
else
ln -s "$i" "$HOME"/song/
fi
done
}
for elem in "$DIR"/*
do
if [ -d "$elem" ] ; then
link "$elem"
else
ln -s "$elem" "$HOME"/song/
fi
done


我想说的是 不管 我的"$HOME"/song/ 文件夹里有没有已经建立过某些文件的符号连接
运行脚本的结果都是

ln: 创建符号链接 “/home/myname/song/文件名”: 文件已存在

为什么啊
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
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

Re: shell脚本创建符号连接的问题

#2

帖子 yjcong » 2009-05-29 2:15

代码: 全选

mplayer /media/windows_multimedia/music/* -shuffle -loop 0
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

Re: shell脚本创建符号连接的问题

#3

帖子 543082593 » 2009-05-29 2:26

yjcong 写了:

代码: 全选

mplayer /media/windows_multimedia/music/* -shuffle -loop 0
额 我不是问这个啊
我是问上面那个脚本里的 问题啊
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
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

Re: shell脚本创建符号连接的问题

#4

帖子 yjcong » 2009-05-29 2:49

我这没有任何问题, 你换个
/home/myname/song/
看看
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

Re: shell脚本创建符号连接的问题

#5

帖子 543082593 » 2009-05-29 3:06

yjcong 写了:我这没有任何问题, 你换个
/home/myname/song/
看看
其实 符号连接都可以 很正常 但是 就是 每次 你在命令行 明显可以看到
ln: 创建符号链接 “/home/myname/song/文件名”: 文件已存在

这个地方我不知道 为什么 我试了一下 我把 所有已经创建过的符号连接都删了 重新运行脚本 还是 出现 文件已存在的提示
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
头像
c\nc
帖子: 231
注册时间: 2007-12-25 12:51

Re: shell脚本创建符号连接的问题

#6

帖子 c\nc » 2009-05-29 14:32

这个递归函数有问题:for i in "$elem"/* 这句不应该用 $elem 应该用 $* 之类来接受参数。
重写脚本,搞清 ln 使用相对路径的情况。
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: shell脚本创建符号连接的问题

#7

帖子 aerofox » 2009-05-29 23:33

你想要实现的功能是不是:

代码: 全选

find /media/windows_multimedia/music -type f -print0 | xargs -0 ln -t ~/song -s
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

Re: shell脚本创建符号连接的问题

#8

帖子 543082593 » 2009-05-30 2:00

aerofox 写了:你想要实现的功能是不是:

代码: 全选

find /media/windows_multimedia/music -type f -print0 | xargs -0 ln -t ~/song -s
你太拽了 恩恩
这个很明显比我的那个好多了 谢谢
但是我的那个 还是没检查出来问题唉 就是那个 总提示“连接已存在”
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
回复