linux .cshrc file compile error - linux

in .cshrc file under linux, i have
set path=(/usr/local/gams/24.2.2 $path ) #GAMS DIRECTORY (SOCL)
set path=(/usr/local/bin $path)
set path=(/usr/local/cplex/v12.4/cplex/bin/x86-64_sles10_4.1 $path)
and when i run command source .cshrc i receive this error
-bash: .cshrc: line 1: syntax error near unexpected token `('
-bash: .cshrc: line 1: `set path=(/usr/local/gams/24.2.2 $path ) #GAMS DIRECTORY'
any help?
I am new to linux, so if i made an obvious mistake asking this question, sorry about that
thanks a lot

It seems that you were using bash, not csh family shells. When you ran command source .cshrc, it used bash way to process .cshrc; .cshrc is just the input file, regardless of its file name, so you need to either run csh family shells or use bash syntax for the instructions in .cshrc. (I wouldn't suggest the later way, as it could be confusing.) If you would like to stick with bash, you should do this in .bash_profile or .bashrc.

.cshrc is for the C-shell, not the bash shell. bash uses .bashrc.
I don't know about the c-shell, but in the bash shell, you'd set the PATH variable like this:
PATH="/usr/local/gams/24.2.2:/usr/local/bin:/usr/local/cplex/v12.4/cplex/bin/x86-64_sles10_4.1:$path"
Try echo $PATH first to see if any of those paths (particularly /usr/local/bin) are already in it.

Related

How to reload /etc/environment from shell script

So I have this shell script that checks and then concats an environmental variable to /etc/environment, then reloads the file without having to logout/login:
#!/bin/sh
portvar="PORT=5000"
echo $portvar
grep -q $portvar /etc/environment && echo "EV already in" || echo $portvar >> /etc/environment
set -a; source /etc/environment; set +a;
When I run it, I get the error ./test.sh: 5: ./test.sh: source: not found. However, if I run set -a; source /etc/environment; set +a; directly in the terminal it updates the environmental variable just fine. I have no idea what the set command does, I just found it in another stack overflow question.
Any idea why it runs in the terminal directly but not in the .sh file?
Thanks
/bin/sh on your system is likely some shell that isn't bash and doesn't implement the source command. On my Ubuntu 20.04 system /bin/sh is actually dash.
The source command is not defined by POSIX as part of the shell command language nor is it one of the required special built-in utilities. It's a non-standard feature provided by bash. However, the . command, which does the same thing, is specified by POSIX.
So you can use . instead, e.g. . /etc/environment. Or if you want to keep using source, then you need to have your script run by bash or some other shell that supports it, by changing the shebang line to #!/bin/bash.
There is a tool called checkbashisms that can help you find unintentional uses of bash-specific features in your scripts. When run on your script, it flags this:
possible bashism in foo.sh line 5 (should be '.', not 'source'):

syntax error when compare two files using shell script [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

I need to load some modules but load module doesn't work in my environment unless I change the bash profile. How do I do this in tcsh?

. ~/.bashrc is what I'm use to source the bash script in bash shell. But I have quite a few scripts that I want to run from tcsh.But this command doesn't work for tcsh. Why doesn't this work? Is there a different file similar to bash profile when I work on t shell?
Any links to look up?
Thx!
The tcsh equivalent to the bash and posix shell . is source.
That said, bash and tcsh are entirely different shells. You will not be able to source ~/.bashrc from tcsh, if that was your intent.
You can run a shell script of any type as long as that shell script has the appropriate shebang in its first line, but it'll run in its own process, and not in the context of your interactive tcsh instance.
If, for example, you have a directory: ~/.tcshrc.d, and you want to include all the files in that directory in your login shell, you might include the following in your .tcshrc file:
foreach i ( ~/.tcshrc.d/* )
source $i
end
Note that this is tcsh code, and is not compatible with bash.

export: Command not found raised by linux source command

after I installed Anaconda package on the server, I'm then trying source ~/.bashrc to set env variable, but it raise an error of export: Command not found, my .bashrc file is like this:
# added by Anaconda3 4.2.0 installer
export PATH="/projdata3/info_fil/wangtao/conda/bin:$PATH"
can anyone help? thanks very much!
Since you're using tcsh, not bash, you should edit your .cshrc and add the line:
set path = ( /projdata3/info_fil/wangtao/conda/bin $path )
Then use source .cshrc.
If your shell is .csh please use setenv to export a variable which in bash you would normally do with export
In bash, export My_VARIABLE=/some/location/or/.something/
In csh shell, it would be setenv My_VARIABLE /some/location/or/.something/
If echo $SHELL command is giving /usr/local/bin/tcsh then it means you are working in csh shell.
In csh shell, if you want to set environment variables, the syntax goes like this
setenv <variable_name> <variable_value>
Here is the useful csh commands link for reference
If you want to enter bash shell from csh shell, enter command bash
Then this command will work: export <variable_name>=<variable_value>

adding non standard library to #inc in PERL in tcsh

I am trying to use a few perl modules which are located in my own directory.
I read that I need to add the "export" command in this form -
export PERL5LIB=PERL5LIB:/location/of/personal/modules
However I was doing this in bash, and once I tried to source bash after the modification I started to get the "if: Expression Syntax" error.
This tells me that it means my shell is not bash. I queried by echo $SHELL, which gives me tcsh ( I guess its the C shell)
I opened tcsh with the intention of adding the "export" command as written above, however its completely blank and I am now confused as to how to add the non standard directory to #INC.
Any help is greatly appreciated
I think the equivalent tcsh expression is
setenv PERL5LIB PERL5LIB:/location/of/personal/modules
Though I expect that line was supposed to be
export PERL5LIB=$PERL5LIB:/location/of/personal/modules
which would mean you want
setenv PERL5LIB $PERL5LIB:/location/of/personal/modules
But if you don't have anything in the PERL5LIB variable already then you can just use
setenv PERL5LIB /location/of/personal/modules

Resources