tcsh:打印所有参数,首先忽略,然后与 grep 匹配

tcsh: print all arguments, ignore first, and match with grep

我是 tcsh 的新手。我发现了如何使用

将所有参数打印到单独的行中
#! /bin/tcsh
foreach i ($*)
   echo $i
end

太棒了!现在,我想 不打印出第一个元素 并使用 grep 来测试第一个参数是否匹配任何模式 在其他参数中。

这个想法是,如果有人输入 ./prog bread '^b' 'x' 它应该输出

^b : b
x : no match

谢谢!

你可以使用这个:

#!/bin/tcsh

# Store first element in variable
set first=""

# `shift` removes the first (from the left) element from $*
shift

# Now iterate trough the remaining args
foreach i ($*)
    # Grep for $i in $first and send results to /dev/null
    echo "$first" | grep "$i" >& /dev/null

    # Check the return value of the last command
    if ( $? == 0 ) then
        echo "$i : matched"
    else
        echo "$i : no match"
    endif
end