免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3204 | 回复: 15
打印 上一主题 下一主题

如何取得任意一个文件的名字 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-04 20:36 |只看该作者 |倒序浏览
用shell如何取得目录下任意一个文件的名字并赋值到变量?  只要取得任意一个文件的名字就可以了

论坛徽章:
0
2 [报告]
发表于 2009-03-04 20:44 |只看该作者
没明白你什么意思。你是要取随机字符串?

论坛徽章:
0
3 [报告]
发表于 2009-03-04 20:45 |只看该作者
不是,目录下有很多文件,我只想把其中任意一个文件的名字赋值到变量里面

论坛徽章:
0
4 [报告]
发表于 2009-03-04 20:47 |只看该作者

回复 #3 fish-fly 的帖子

a=$(ls|tail -1)

论坛徽章:
0
5 [报告]
发表于 2009-03-04 20:51 |只看该作者
谢谢楼上
要的就是这个结果

论坛徽章:
0
6 [报告]
发表于 2009-03-04 21:04 |只看该作者
ls -al|awk '{print $9}'|while read line;do echo $line;done

论坛徽章:
0
7 [报告]
发表于 2009-03-04 21:12 |只看该作者

回复 #6 飞鸿无痕 的帖子

your not output,you try

try this
[yujx@fedora9 tmp]$ ls|while read line;do echo "----------"$line;done
----------gconfd-gdm
----------gconfd-tister
----------gedit-svn.tister.3216040869
----------keyring-LceagY
----------orbit-gdm
----------orbit-tister
----------pulse-gdm
----------pulse-root
----------pulse-tister
----------scim_confim::0.0-tister
----------scim-helper-manager-socket-tister
----------scim-panel-socket:0-tister
----------scim-socket-frontend-tister
----------ssh-rqOEdV2472
----------virtual-tister.5BbLWg
----------virtual-tister.8TEW6q
----------virtual-tister.Cuo46m
----------virtual-tister.Dfbftd
----------virtual-tister.LEdaWQ
----------virtual-tister.t1QbCh
----------virtual-tister.womawS
----------virtual-tister.ZiRuMp

论坛徽章:
0
8 [报告]
发表于 2009-03-04 21:14 |只看该作者
原帖由 wtuter 于 2009-3-4 21:12 发表
your not output,you try

try this
[yujx@fedora9 tmp]$ ls|while read line;do echo "----------"$line;done
----------gconfd-gdm
----------gconfd-tister
----------gedit-svn.tister.3216040869
...


try了。没有问题:
[root@apache ~]# ls -al|awk '{print $9}'|while read line;do echo $line;done

.
..
a
anaconda-ks.cfg
apache_1.3.41.tar.gz
.bash_history
.bash_logout
.bash_profile
.bashrc
.cshrc
curl-debuginfo-7.12.1-8.rhel4.i386.rpm
gcc-3.4.6-3.i386.rpm
.gtkrc
install.log
install.log.syslog
mysql-5.0.37.tar.gz
mysql_backup
mysql-devel-4.1.20-1.RHEL4.1.i386.rpm
.mysql_history
nginx
python-curl-7.12.1-1.2.el4.rf.i386.rpm
shell
.tcshrc
test
yum
yum.gz

论坛徽章:
0
9 [报告]
发表于 2009-03-04 21:18 |只看该作者
ls |sed -n '1p'

论坛徽章:
0
10 [报告]
发表于 2009-03-04 21:22 |只看该作者

回复 #8 飞鸿无痕 的帖子

realy?  my system not output ;which is your system
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP