×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

好像没问题呀。当字符还是当文件名depends on the parameter definition of the cmd. add -x at the end of the fist line to identify which statement has problem. below is my practise:

本文发表在 rolia.net 枫下论坛cd:/tmp/test # ls -l
total 40
drwxr-xr-x 4 root root 4096 Apr 21 04:55 .
drwxrwxrwt 103 root root 12288 Apr 21 04:51 ..
-rw-r--r-- 1 root root 11 Apr 21 04:55 a
-rw-r--r-- 1 root root 15 Apr 21 04:55 b
drwxr-xr-x 2 root root 4096 Apr 21 04:54 bb
-rw-r--r-- 1 root root 13 Apr 21 04:52 c
-rwxr-xr-x 1 root root 124 Apr 21 04:53 thesh
drwxr-xr-x 2 root root 4096 Apr 21 04:53 x

cd:/tmp/test # cat a
aab
bb
cc

:wqcd:/tmp/test # cat b
aab
bb
cc
:wq

cd:/tmp/test # cat thesh
#!/bin/sh -x
mkdir $1
for File in *
do
result=`grep -l $1 $File`
if test "$File" = "$result"
then
cp $File $1/$File
fi
done
cd:/tmp/test # ./thesh bb
+ mkdir bb
++ grep -l bb a
+ result=a
+ test a = a
+ cp a bb/a
++ grep -l bb b
+ result=b
+ test b = b
+ cp b bb/b
++ grep -l bb bb
+ result=
+ test bb = ''
++ grep -l bb c
+ result=
+ test c = ''
++ grep -l bb thesh
+ result=
+ test thesh = ''
++ grep -l bb x
+ result=
+ test x = ''
cd:/tmp/test # ls bb
. .. a b
cd:/tmp/test #更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 问一个Shell 问题
    下面的这个脚本根据提供的参数, 搜索当前目录下的文件是否含有规定内容,
    如有, 拷到一个目录下.

    find-and-copy
    --------------------

    #!/bin/sh
    mkdir $1
    for File in *
    do
    result=`grep -l $1 $File`
    if test $File = $result
    then
    cp $File $1/$File
    fi
    done
    ----------------------------------

    问题是系统把$File(文件名)的内容当成变量. 碰到每一个文件总是提示 xxx not found. 怎样让系统把$File当成是字符?

    谢了先.
    • 好像没问题呀。当字符还是当文件名depends on the parameter definition of the cmd. add -x at the end of the fist line to identify which statement has problem. below is my practise:
      本文发表在 rolia.net 枫下论坛cd:/tmp/test # ls -l
      total 40
      drwxr-xr-x 4 root root 4096 Apr 21 04:55 .
      drwxrwxrwt 103 root root 12288 Apr 21 04:51 ..
      -rw-r--r-- 1 root root 11 Apr 21 04:55 a
      -rw-r--r-- 1 root root 15 Apr 21 04:55 b
      drwxr-xr-x 2 root root 4096 Apr 21 04:54 bb
      -rw-r--r-- 1 root root 13 Apr 21 04:52 c
      -rwxr-xr-x 1 root root 124 Apr 21 04:53 thesh
      drwxr-xr-x 2 root root 4096 Apr 21 04:53 x

      cd:/tmp/test # cat a
      aab
      bb
      cc

      :wqcd:/tmp/test # cat b
      aab
      bb
      cc
      :wq

      cd:/tmp/test # cat thesh
      #!/bin/sh -x
      mkdir $1
      for File in *
      do
      result=`grep -l $1 $File`
      if test "$File" = "$result"
      then
      cp $File $1/$File
      fi
      done
      cd:/tmp/test # ./thesh bb
      + mkdir bb
      ++ grep -l bb a
      + result=a
      + test a = a
      + cp a bb/a
      ++ grep -l bb b
      + result=b
      + test b = b
      + cp b bb/b
      ++ grep -l bb bb
      + result=
      + test bb = ''
      ++ grep -l bb c
      + result=
      + test c = ''
      ++ grep -l bb thesh
      + result=
      + test thesh = ''
      ++ grep -l bb x
      + result=
      + test x = ''
      cd:/tmp/test # ls bb
      . .. a b
      cd:/tmp/test #更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • double quote need to be added in the "test" line, in case there is no matach.
        • Thanks
    • >>. 怎样让系统把$File当成是字符? A: \$File