eran
eran

Reputation: 6921

multi-line variable in tcsh

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

Upvotes: 7

Views: 13587

Answers (3)

Matheus Garcia
Matheus Garcia

Reputation: 39

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.

Upvotes: 0

Morgan
Morgan

Reputation: 20514

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

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263357

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.)

Upvotes: 8

Related Questions