2

I have a bunch of aliases in my .cshrc file which use the path:

/projects/project_name/my_name/$CLIENT

I've written a short script which is intended to take a folder name from my working directory path and store it into an environment variable to be used in various aliases.

set client_name = `echo $PWD | awk -F / '{ print $5; }' | xargs`

setenv CLIENT $client_name  

source ~/.cshrc   

The issue currently is that the environment variable doesn't update from its previous value when this is run. Unfortunately the C shell doesn't support using functions (a potential alternative method) as could be done in Bash.

How can I get the variable to update from the command line? Is there a better method of accomplishing this?

Thanks

2
  • Have you thought about moving away from using aliases and writing a short sh script instead? Dec 10, 2019 at 15:06
  • Solved my issue now, added it as an answer below if you're interested
    – tombam95
    Dec 10, 2019 at 17:53

3 Answers 3

1

Your script may be placing a blank string into $5 because there aren't five variables in your working directory path (pwd), but it's hard to say without seeing the input string. If you want more help with this issue you will need to update your question with more information and show us what the input getting sent to awk looks like.

I don't know if there is a way to make an alias dynamic, but you could simply convert each alias into a bash function. Bash functions take the form of funct_name () {commands}, and they even take arguments which get stored in the variables $1, $2, $3, ... etc. So, you could create a bash function which reads in the dynamic data as an argument. For example:

test () {
  echo $1
}

Will behave as follows:

$ test abc
abc
$ test foobar
foobar

With this method, you would take the alias command and place it where echo is in my example, using $1 instead of $CLIENT. However, you don't even have to recreate each alias as a bash function and use arguments in this way.

From the sample commands you gave me, it looks like you are taking a folder name from your working directory path and storing it into an environment variable to use it in various aliases. You could simply create a bash function to do this task for you. As an example here is a bash function which will take the path of your working directory, find the first folder name (you seem to want the 4th so change this), and save it to the $TEST environment variable.

test () {
  var=$(pwd | awk -F / '{ print $2; }')
  export TEST=$var
}

If you want this function to be available to you always, add the above into your .bashrc file. Anytime you start a new terminal window the test command will be at your disposal. If you want to add it to your current working terminal (instead of having to open a new one), run source ~/.bashrc. You can then run the function in the terminal like it is a command. You will just have run test and the environment variable will update and your alias commands should work. Here is an example of it in action:

$ pwd
/home/user
$ echo $TEST

$ test
$ echo $TEST
home

Be aware: this command depends on which directory you are working in. If you change folders to root, my example will place an empty string into $TEST because there is no string right after the first slash. This may be why awk is printing an empty string in your case, but again, I can't say for sure without seeing the input data.

3
  • Hey, thanks for your answer. Yes I looked at using functions, but it seems that this isn't supported within the C shell, although I'm not 100% sure of this. I managed to solve the blank return by using: echo $PWD | awk -F / '{ print $5; }' | xargs Which returns the correct string when tested independently
    – tombam95
    Dec 6, 2019 at 16:01
  • Ah, I notice this now from your mention of the ".cshrc file", though I would edit your question to make it more clear you are using C shell. I would also edit your question to include the information that you just told me, that you've tried using functions but C shell doesn't support them. Dec 6, 2019 at 16:04
  • Yes, just did this :)
    – tombam95
    Dec 6, 2019 at 16:10
1

Consider adding an alias to cd that will update the $CLIENT variable every time you change directories.

alias cd 'cd \!*; set client_name = `echo $PWD | awk -F / '\''{ print $5; }'\'' | xargs`; setenv CLIENT $client_name'

This has shortcomings, of course. For instance, it doesn't work with pushd or popd, though you could probably work around that with similar aliases. There might be other corner cases I didn't fully consider.

Also consider whether the aliases you keep in your cshrc file actually need to be aliases. Unless they modify the shell environment in some way (by setting variables or changing directories), they could be standalone shell scripts instead. In which case, you could actually write them using a shell that does support functions.

4
  • I considered doing this, but in the past aliasing cd breaks some other scripts without warning so I prefer to avoid that approach. Good point about them being scripts instead, that could work
    – tombam95
    Dec 6, 2019 at 17:17
  • @tombam95 Consider switching to a more modern shell... Sticking with C shell is likely to only increase your grief at this point. It's 2019 after all. 😁
    – filbranden
    Dec 6, 2019 at 17:20
  • Couldn't agree more, but unfortunately work dictates that I'm stuck with it :)
    – tombam95
    Dec 6, 2019 at 17:34
  • That should be against the Geneva Conventions 🤣 If you do consider updating your resume, I'd recommend leaving csh out... 😜
    – filbranden
    Dec 6, 2019 at 17:37
0

In the end my solution was to use a script to pass the $CLIENT variable into my aliases.

Example:

Create find_client.sh, and use the echo $PWD | awk -F / '{ print $5; }' | xargs method to find the client directory within this script. Echo the output with the full path attached.

In my aliases (.cshrc file) create an alias such as:

alias wk 'cd `find_client.sh`'

So far this seems to work as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.