LoveUnix » 编程开发 & Rational » Shell 新手报到
让LU留住您的每

一天 让LU博客留住您的每一天
2005-3-24 16:01 jinxin112688
大家好!我从现在开始也加入了编写shell的队伍了,呵呵,不知晚否?<br />刚来就遇到了个问题:<br />1、在shell中调用系统命令时:如下<br />#!/bin/bash<br /><br />for loop in &#39;ls -l&#39;<br />do <br />     echo $loop<br />done<br />结果竟然出来的是:ls -l。我想要的是打印出当前目录下的所有文件。<br />2、还有:<br />  DIR=$1<br />      if  [ &quot; &#39;ls -A $DIR&#39; &quot; = &quot;&quot; ]<br />      then<br />              echo &quot;$DIR is empty&#33;&quot;<br />      else   echo &quot;$DIR is not empty&quot;<br />      fi<br />我明明在当前目录下创建了一个空目录,但结果仍然是&quot;not empty&#33;&quot;, 发现&#39;ls -A $DIR&#39;这句话返回为1,就是说没有执行。不知什么原因??<br />3。&quot; &#39;basename $0&#39; &quot;在shell中也是一样的结果,没有起作用<br />我想这三个应该是同类的问题吧?

2005-3-24 19:52 Komtas
1<br />试用一个变量保存<br />&#39;ls -l&#39;结果看看 <br /><br />

2005-3-24 21:54 ghostfly
将&#39;改成`试试呢?<br />echo &quot;$DIR is empty&#33;&quot;返回结还是<br />$DIR is empty&#33;吧<br />

2005-3-25 09:21 jinxin112688
ghostfly真是牛人啊!!我这么细微的错误,你一眼就看出来了,我一直用的是单引号“ &#39; ”,应该用“ ` ”, 呵呵,这两个还真是不好区别呢!这样一改,以前的好多疑问都解决了,给ghostfly加10000分!呵呵

2005-3-25 10:59 Komtas
<!--emo&:blush:--><img src='style_emoticons/default/blush.gif' border='0' style='vertical-align:middle' alt='blush.gif' /><!--endemo--> 原来这样子D<br /><br />偶都看不出来  <!--emo&:grin:--><img src='style_emoticons/default/grin.gif' border='0' style='vertical-align:middle' alt='grin.gif' /><!--endemo-->

2005-3-25 17:14 jinxin112688
Komtas&#33; 也谢谢你了!现在遇到了一个问题。就是判断一个目录中的文件是否有&quot;_&quot;开头的,如果不是,就进行备份,我是这样写的: (文件都在myDir中)<br />file_path=&quot;home/myDir&quot;<br />declare -i counter=0<br />file_mark= &quot;_&quot;<br /><br />for file in `ls -t  $file_path`<br />do <br />      if [ $counter = 2 ]<br />      then  return 1;<br />      if [ -f $file_path&quot;/&quot;$file ] &amp;&amp; [ `basename $file_path&quot;/&quot;$file` &#33;= $file_mark* ]<br />      then <br />            echo $file<br />            fileRename=&quot;_&quot;`basename $file`&quot;_&quot;`date +%d%m%Y`<br />            cp $file_path&quot;/&quot;$file   $file_path&quot;/&quot;$file<br />            if [ $? &#33;= 0 ] <br />            then return 1<br />            fi<br />      fi<br />done<br />我现在的问题是:<br />1、为什么在结果中还是没有能排除掉&quot;_&quot;的文件?不知[ `basename $file_path&quot;/&quot;$file` &#33;= $file_mark*这儿哪儿错了?<br />2、当我在&quot;date +%d%m%Y&quot;改为“date +%d/%m/%Y&quot;时,提示有误??<br /><br />

2005-3-28 11:01 jinxin112688
顶一下!大家帮帮我

2005-3-28 12:27 无双
for file in `ls -t $file_path`<br />这里修改成<br />`ls -t $file_path/_*` 不可以了

2005-3-28 13:44 jinxin112688
无双兄可能没有明白我的意思。我的目的是这样:<br />遍历file_path目录,如果在file_path目录中找到以“_&quot;开头的文件,刚不备份,不是以“_”开始的文件名, 则备份下来。你改了后,则只能显示以“_”开头的文件,<br />我上面的写法,好象不能过滤掉“_”开头的文件 ,不知哪儿有问题??

2005-3-28 19:02 无双
这可能是应该使用正则表达式吧<br /><br />sorry 对于shell不是很清楚 我是用perl的

2005-3-30 22:00 c1l2d3
无双说的对,加点正则式<br /><br />ls -t $file_path/[^_]*`<br /><br />只取出不是以“_”开头的文件,就备份吧

2006-4-2 16:32 celavi
第一个问题,看看这篇帖子([url]http://bbs.loveunix.net/viewthread.php?tid=22585&extra=page%3D1[/url])就不会错了。基础很重要啊。

页: [1]
查看完整版本: Shell 新手报到


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.