Environment variable is not setting globally - linux

I am using csh terminal.
.cshrc
setenv $files /home/ec2-user/files
.login
if [ -f ~/.cshrc ]; then
. ~/.cshrc
fi
I am trying to echo $files values from plink.
It showing the error files: undefined variable

You don't use a dollar sign when setting a variable, you only use it when you refer to that variable.
setenv files /home/ec2-user/files
The test command should be a built-in in most csh/tcsh implementations, and has most of the same functionality you'll see listed under man test.
test -f ~/.cshrc && source ~/.cshrc
Note that normally, csh/tcsh will run your .cshrc or .tcshrc file automatically, before it runs .login.

Related

How can I get the path to the currently running shell script (cshell linux)

Linux/CSHELL:
How can I get the path to a tcsh script that is being sourced?
This is not $PWD. Rather, the path to the script (file).
Versions/specifics:
% echo $SHELL $PLATFORM
/tool/pandora/bin/tcsh linux_3.10.0_64
ls -l /tool/pandora/bin/tcsh
lrwxrwxrwx 1 pandora pandora 33 Oct 20 2016 /tool/pandora/bin/tcsh -> ../.package/tcsh-6.19.00/bin/tcsh
There are lots of notes in here for this type of thing. But I didn't see one for linux c-shell.
Thanks in Advance !
Example:
I have a file /home/myid/my_cshell.csh...
set a = `readlink -f ${0}`
echo $a
echo ${0}
source /home/myid/my_cshell.csh
/tool/pandora/.package/tcsh-6.19.00/bin/tcsh
/tool/pandora/bin/tcsh
This is giving me the path to tcsh, not /home/myid/my_cshell.csh
The script above works fine if I tcsh it, but not source it.
I need the path to the file being sourced.
assuming you have readlink available:
readlink -f "${0}"
should work. ${0} will give you the relative path, and readlink -f will resolve it to the absolute path.
There must already be some way to reference the file... If not, how could it be sourced in the first place? So I suppose the problem is that ${0} referecnes the parent's file system location, and not the sourced file. In that case, readlink -f <file> should still work, but maybe try calling it first, and then pass the result when you run source. Like this:
main.csh:
#!/bin/csh
set p=`readlink -f source.csh`
source source.csh "${p}"
source.csh:
#!/bin/csh
set pmain=`readlink -f "${0}"`
echo "my path is ${1}. main.csh's path is ${pmain}"

Create a script that adds lines of code to .bashrc then reloads the terminal

I am trying to create a shell script that once run, adds a line of code to the end of .bashrc then reloads the terminal. This is the code i have written in my install.sh
function addbashaliases() {
echo 'if [ -f ~/aliases/.bash_aliases ]; then
. ~/aliases/.bash_aliases
fi' >> ~/.bashrc;
source ~/.bashrc
}
Nothing is happening. How should I write the code so that it runs and adds the text to the .bashrc file?
For the sake of clarity I prefer to append the information on the .bashrc file using the cat command instead of the echo. However, this should work also using your echo command.
This said you should make sure that:
The script calls the addbashaliases function
The ~/aliases/.bash_aliases file exists (I would expect to have something more similar to ~/.aliases/.bash_aliases)
You can check that the script has correctly run by checking the content of the ~/.bashrc file and printing some environment variable set on the .bash_aliases file after the source command.
#!/bin/bash
function addbashaliases() {
# Add source bash_aliases on .bashrc
cat >> ~/.bashrc << EOT
if [ -f ~/aliases/.bash_aliases ]; then
. ~/aliases/.bash_aliases
fi
EOT
# Reload current environment
source ~/.bashrc
}
# Execute the function
addbashaliases
I am just correcting your script. As per your logic it should be like below.
function addbashaliases() {
if [ -f ~/aliases/.bash_aliases ]; then
output=$(. ~/aliases/.bash_aliases)
echo $output >> ~/.bashrc
fi
source ~/.bashrc
}

Setting Path variable using shell scripting - Using a shell variable

I have variable difine as SCRPT_PATH="/home/dasitha" I need to add this path to .bashrc file using shell scirpt.
What I tired was something like this.
echo 'export PATH=$PATH:$SCRPT_PATH")' >> /root/.bashrc
After opening my .bashrc file it looks like this
export PATH=$PATH:$SCRPT_PATH")
What I actually need is export PATH=$PATH:/home/dasitha. How should I do this by changing the shell script?
You've got the wrong quotes (in addition to a spurious looking parenthesis and quote mark). You're looking for something more like
echo "export PATH=$PATH:$SCRPT_PATH" >> /root/.bashrc
Here's a quick example that demonstrates quoting:
krismatth#earth ~$ echo $HOME
/Users/krismatth
krismatth#earth ~$ echo '$HOME'
$HOME
krismatth#earth ~$ echo "$HOME"
/Users/krismatth

How to execute a command on the remote at login with ssh, after .bashrc sourcing?

I am working on different machines where my home is NFS-mounted. I want to switch easily to another machine if the one I am working on is too much loaded.
I often modify my environment in the shell I am working, and I would like to find the same modified (with respect to the bashrc) environment when I switch to another machine. I tried the following script, but it does not work because the .bashrc is sourced after source $HOME/.env-dump.txt.
Is there a clean way to execute some commands when logging to a machine with ssh as if you type them at the prompt after logged?
#!/usr/bin/env sh
if [[ $# != 1 ]];
echo 'sssh USAGE:'
echo ' sssh remotename'
exit 1
fi
printenv | sed -e '/_=.*/ d;s/\([^=]\+\)=\(.*\)/export \1="\2"/' > $HOME/.env-dump.txt
ssh $1 -t 'source $HOME/.env-dump.txt; bash -l'
Add the following lines to your ~/.bash_profile
[ -f "$HOME/.bashrc" ] && . $HOME/bashrc
[ -f "$HOME/.env-dump.txt" ] && source $HOME/.env-dump.txt
And create a ~/.bash_logout file with the line
[ -f "$HOME/.env-dump.txt" ] && rm $HOME/.env-dump.txt
Now you can simply call ssh $1 -t 'bash -l' in the last line of your script.
WARNING
The output of printenv contains some variables which are machine dependent like GNOME_KEYRING_CONTROL, SESSION_MANAGER, DBUS_SESSION_BUS_ADDRESS ... (These variables are form a Ubuntu 12.04). These variables should be removed from the ~/.env-dump.txt file.

Equivalent of %~dp0 (retrieving source file name) in sh

I'm converting some Windows batch files to Unix scripts using sh. I have problems because some behavior is dependent on the %~dp0 macro available in batch files.
Is there any sh equivalent to this? Any way to obtain the directory where the executing script lives?
The problem (for you) with $0 is that it is set to whatever command line was use to invoke the script, not the location of the script itself. This can make it difficult to get the full path of the directory containing the script which is what you get from %~dp0 in a Windows batch file.
For example, consider the following script, dollar.sh:
#!/bin/bash
echo $0
If you'd run it you'll get the following output:
# ./dollar.sh
./dollar.sh
# /tmp/dollar.sh
/tmp/dollar.sh
So to get the fully qualified directory name of a script I do the following:
cd `dirname $0`
SCRIPTDIR=`pwd`
cd -
This works as follows:
cd to the directory of the script, using either the relative or absolute path from the command line.
Gets the absolute path of this directory and stores it in SCRIPTDIR.
Goes back to the previous working directory using "cd -".
Yes, you can! It's in the arguments. :)
look at
${0}
combining that with
{$var%Pattern}
Remove from $var the shortest part of $Pattern that matches the back end of $var.
what you want is just
${0%/*}
I recommend the Advanced Bash Scripting Guide
(that is also where the above information is from).
Especiall the part on Converting DOS Batch Files to Shell Scripts
might be useful for you. :)
If I have misunderstood you, you may have to combine that with the output of "pwd". Since it only contains the path the script was called with!
Try the following script:
#!/bin/bash
called_path=${0%/*}
stripped=${called_path#[^/]*}
real_path=`pwd`$stripped
echo "called path: $called_path"
echo "stripped: $stripped"
echo "pwd: `pwd`"
echo "real path: $real_path
This needs some work though.
I recommend using Dave Webb's approach unless that is impossible.
In bash under linux you can get the full path to the command with:
readlink /proc/$$/fd/255
and to get the directory:
dir=$(dirname $(readlink /proc/$$/fd/255))
It's ugly, but I have yet to find another way.
I was trying to find the path for a script that was being sourced from another script. And that was my problem, when sourcing the text just gets copied into the calling script, so $0 always returns information about the calling script.
I found a workaround, that only works in bash, $BASH_SOURCE always has the info about the script in which it is referred to. Even if the script is sourced it is correctly resolved to the original (sourced) script.
The correct answer is this one:
How do I determine the location of my script? I want to read some config files from the same place.
It is important to realize that in the general case, this problem has no solution. Any approach you might have heard of, and any approach that will be detailed below, has flaws and will only work in specific cases. First and foremost, try to avoid the problem entirely by not depending on the location of your script!
Before we dive into solutions, let's clear up some misunderstandings. It is important to understand that:
Your script does not actually have a location! Wherever the bytes end up coming from, there is no "one canonical path" for it. Never.
$0 is NOT the answer to your problem. If you think it is, you can either stop reading and write more bugs, or you can accept this and read on.
...
Try this:
${0%/*}
This should work for bash shell:
dir=$(dirname $(readlink -m $BASH_SOURCE))
Test script:
#!/bin/bash
echo $(dirname $(readlink -m $BASH_SOURCE))
Run test:
$ ./somedir/test.sh
/tmp/somedir
$ source ./somedir/test.sh
/tmp/somedir
$ bash ./somedir/test.sh
/tmp/somedir
$ . ./somedir/test.sh
/tmp/somedir
This is a script can get the shell file real path when executed or sourced.
Tested in bash, zsh, ksh, dash.
BTW: you shall clean the verbose code by yourself.
#!/usr/bin/env bash
echo "---------------- GET SELF PATH ----------------"
echo "NOW \$(pwd) >>> $(pwd)"
ORIGINAL_PWD_GETSELFPATHVAR=$(pwd)
echo "NOW \$0 >>> $0"
echo "NOW \$_ >>> $_"
echo "NOW \${0##*/} >>> ${0##*/}"
if test -n "$BASH"; then
echo "RUNNING IN BASH..."
SH_FILE_RUN_PATH_GETSELFPATHVAR=${BASH_SOURCE[0]}
elif test -n "$ZSH_NAME"; then
echo "RUNNING IN ZSH..."
SH_FILE_RUN_PATH_GETSELFPATHVAR=${(%):-%x}
elif test -n "$KSH_VERSION"; then
echo "RUNNING IN KSH..."
SH_FILE_RUN_PATH_GETSELFPATHVAR=${.sh.file}
else
echo "RUNNING IN DASH OR OTHERS ELSE..."
SH_FILE_RUN_PATH_GETSELFPATHVAR=$(lsof -p $$ -Fn0 | tr -d '\0' | grep "${0##*/}" | tail -1 | sed 's/^[^\/]*//g')
fi
echo "EXECUTING FILE PATH: $SH_FILE_RUN_PATH_GETSELFPATHVAR"
cd "$(dirname "$SH_FILE_RUN_PATH_GETSELFPATHVAR")" || return 1
SH_FILE_RUN_BASENAME_GETSELFPATHVAR=$(basename "$SH_FILE_RUN_PATH_GETSELFPATHVAR")
# Iterate down a (possible) chain of symlinks as lsof of macOS doesn't have -f option.
while [ -L "$SH_FILE_RUN_BASENAME_GETSELFPATHVAR" ]; do
SH_FILE_REAL_PATH_GETSELFPATHVAR=$(readlink "$SH_FILE_RUN_BASENAME_GETSELFPATHVAR")
cd "$(dirname "$SH_FILE_REAL_PATH_GETSELFPATHVAR")" || return 1
SH_FILE_RUN_BASENAME_GETSELFPATHVAR=$(basename "$SH_FILE_REAL_PATH_GETSELFPATHVAR")
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
SH_SELF_PATH_DIR_RESULT=$(pwd -P)
SH_FILE_REAL_PATH_GETSELFPATHVAR=$SH_SELF_PATH_DIR_RESULT/$SH_FILE_RUN_BASENAME_GETSELFPATHVAR
echo "EXECUTING REAL PATH: $SH_FILE_REAL_PATH_GETSELFPATHVAR"
echo "EXECUTING FILE DIR: $SH_SELF_PATH_DIR_RESULT"
cd "$ORIGINAL_PWD_GETSELFPATHVAR" || return 1
unset ORIGINAL_PWD_GETSELFPATHVAR
unset SH_FILE_RUN_PATH_GETSELFPATHVAR
unset SH_FILE_RUN_BASENAME_GETSELFPATHVAR
unset SH_FILE_REAL_PATH_GETSELFPATHVAR
echo "---------------- GET SELF PATH ----------------"
# USE $SH_SELF_PATH_DIR_RESULT BEBLOW
I have tried $0 before, namely:
dirname $0
and it just returns "." even when the script is being sourced by another script:
. ../somedir/somescript.sh

Resources