tcsh 中的多行变量

发布于 2024-12-02 23:51:11 字数 337 浏览 1 评论 0原文

我想在 tcsh 中使用变量来保存脚本的使用信息,因此在我的脚本中,每当我编写 echo $usage 时,它​​都会打印

my_script
  -h : -help
  -b : do boo

etc`。

有办法做到这一点吗?这可以使用 << 来完成吗? EOF?

我尝试过类似的方法,但失败了:

set help =  << EOF
     my_script 
       -h : print help
       -b : do boo
EOF

谢谢

I want to have variable in tcsh to hold the usage info of my script, so in my script, whenever I write echo $usage, it will print

my_script
  -h : -help
  -b : do boo

etc`.

Is there a way to do this? Can this be done using the << EOF ?

I've tried something like this, but it failed:

set help =  << EOF
     my_script 
       -h : print help
       -b : do boo
EOF

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月隐月明月朦胧 2024-12-09 23:51:11
set help = 'my_script\
  -h : -help\
  -b : do boo'

echo $help:q

另一种方法:

alias help 'echo "my_script" ; echo "  -h : -help" ; echo "  -b : do boo"'

help

但另请参阅:http://www.faqs.org /faqs/unix-faq/shell/csh-whynot/

我使用 csh 和 tcsh 的时间比我愿意承认的要长,但我不得不通过反复试验来找出第一个 解决方案。例如,echo "$help" 不起作用;我不知道为什么,我怀疑我能否从文档中找出答案。

(在 Bourne shell 中,您可以这样做:

help() {
    cat <<EOF
my_script
  -h : -help
  -b : do boo
EOF
}

help

但 csh 和 tcsh 没有函数。)

set help = 'my_script\
  -h : -help\
  -b : do boo'

echo $help:q

Another approach:

alias help 'echo "my_script" ; echo "  -h : -help" ; echo "  -b : do boo"'

help

But see also: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

I've been using csh and tcsh for more years than I care to admit, but I had to resort to trial and error to figure out the first solution. For example, echo "$help" doesn't work; I don't know why, and I doubt that I could figure it out from the documentation.

(In Bourne shell, you could do it like this:

help() {
    cat <<EOF
my_script
  -h : -help
  -b : do boo
EOF
}

help

but csh and tcsh don't have functions.)

把时间冻结 2024-12-09 23:51:11

使用 '' 会停止评估变量,这在您的情况下不是问题,但在其他情况下可能会出现问题。为了使其更具可读性并允许字符串分布在多行中,我使用以下命令:

#!/bin/tcsh

set help =      "my_script   \n" 
set help = "$help  -h : -help\n"
set help = "$help  -b : do boo"

echo $help:q

Using '' stops variables being evaluated, not an issue in your case but can be in other situations. To make it a little more readable and allowing the string to be spread over multiple lines I use the following:

#!/bin/tcsh

set help =      "my_script   \n" 
set help = "$help  -h : -help\n"
set help = "$help  -b : do boo"

echo $help:q
﹏雨一样淡蓝的深情 2024-12-09 23:51:11

你的代码几乎是正确的。但是,您忘记了 cat

set help = "`cat`" << 'EOF'
     my_script 
       -h: print help
       -b: do boo
'EOF'

现在您已经设置了,您可以应用一个循环来循环文本:

while ( $#help )
  echo "$help[1]"
  shift help
end

如果更好,您可以有一个 goto 标签并递归到脚本中:

alias function 'set argv = ( _FUNC \!* ) ; source "$0"'
if ( "$1" == "_FUNC" ) goto "$2"

switch ( "$1" )
  case "-b":
    echo Boo!
    exit
  case "-h":
  default:
    ( function help )
endsw
exit -1

help:
cat << 'EOF'
     my_script 
       -h: print help
       -b: do boo
'EOF'
exit

这是一个函数的模拟。

Your code is almost correct. You forgot, however, of cat:

set help = "`cat`" << 'EOF'
     my_script 
       -h: print help
       -b: do boo
'EOF'

Now that you have set, you can apply a loop to cycle through the text:

while ( $#help )
  echo "$help[1]"
  shift help
end

If any better, you can have a goto label and recurse into the script:

alias function 'set argv = ( _FUNC \!* ) ; source "$0"'
if ( "$1" == "_FUNC" ) goto "$2"

switch ( "$1" )
  case "-b":
    echo Boo!
    exit
  case "-h":
  default:
    ( function help )
endsw
exit -1

help:
cat << 'EOF'
     my_script 
       -h: print help
       -b: do boo
'EOF'
exit

This is a simulation of a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文