-1

I'm trying to truncate a part of the file name of my present working directory. This is the code I am using:

set test = "$cwd" | awk -F "/" '{print $3}'
set USER_CUST = ${test:s/abc_//}

(Explanantion: I want to cut out the "abc_" part from the third folder from the root)

When I run the script(script_check.csh) I am getting this in my console :

tcsh -x script_check.csh
set test = /proj/proj_name/abc_username/folder/sub_folder
awk -F / {print $3}
test: Undefined variable.

Why is test an undefined variable? Is there another possible workaround?

Ambareesh S J
  • 97
  • 1
  • 9

1 Answers1

1

The right-hind side of a variable declaration isn't a shell command, so you can't use pipes in there. You can see this with:

$ set test = ls
$ echo $test
ls

Why does it give the confusing "undefined variable" error? Who knows. The (t)csh parser is quirky and full of strange things like this, especially when given bad input. It's one of the main reasons scripting in (t)csh is generally discouraged (as someone pointed out in the comments) ;-)

To make it a shell command, add backticks like so:

$ set test = `echo "$cwd" | awk -F "/" '{print $3}'`
$ echo $test
martin

You can make this a bit shorter with pwd by the way:

$ set test = `pwd | awk -F "/" '{print $2}'`
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146