TCSH / CSH |使用命令结果赋值变量

时间:2015-01-26 11:29:52

标签: unix sed csh tr tcsh

我写了这段代码:

      1 #!/bin/tcsh
      2
      3 set myFiles  = `ls`
      4 set i = 1;
      5 echo "argc is $#argv"
      6 while ($i <= $#argv)
      7    $myFiles = `echo $myFiles | tr "$argv[$i]" " "`
      8    echo "argv now is $argv[$i]"
      9    echo "my files are : $myFiles"
     10    @ i++;
     11 end
     12 echo "my files post proccess are $myFiles"
     13 foreach name ($myFiles)
     14    set temp = `cat $name`
     15    echo "temp is : $temp"
     16    unset temp
     17 end

此部分应获取当前文件夹中的文件名列表,并打印未指定的文件的内容 IE:文件夹包含文件:A B C D E. 输入是:A B C. 所以将打印D E的内容。

现在逻辑是正确的,但我有一些关于第7行的语法问题(tr) 我也试过 sed ,但我得到了#34;许可被拒绝&#34;出于某种原因进入控制台,我真的不知道如何解决它。

所以我需要的帮助实际上是关于使用命令输出分配变量以及在这些命令中包含其他变量的语法。

希望没事......

感谢!

1 个答案:

答案 0 :(得分:1)

请注意,tr将替换所有匹配的字符,因此如果您的输入包含“A”,它将在ls返回的所有文件名中将所有“A”替换为“”。

有一个更清洁的解决方案。您希望查找所有文件,排除与输入匹配的文件并打印剩下的内容。你走了:

#!/bin/tcsh

set exclude_names = ""

# if any argument is passed in, add it as "! -name $arg"
foreach arg ( $* )
    set exclude_names = "$exclude_names ! -name $arg"
end

# Find all files in the current dir, excluding the input
# then print out the filtered set of files
find . -maxdepth 1 -type f $exclude_names -exec cat {} +