【问题标题】:TCSH/CSH | assign variable with commands resultTCSH/CSH |用命令结果分配变量
【发布时间】:2015-01-26 11:29:52
【问题描述】:

我写了这段代码:

      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,但由于某种原因,控制台“权限被拒绝”,我真的不知道如何解决。

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

希望没关系..

谢谢!

【问题讨论】:

  • 您在该行缺少set。此外,在分配给变量时不要加上美元符号——$ 代表插值。

标签: unix sed csh tr tcsh


【解决方案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 {} +

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 2011-08-22
    • 2016-04-15
    • 1970-01-01
    • 2011-01-23
    • 2013-05-05
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多