How to trace which enviroment variable is coming from - centos

My colleague has created an JAVA_HOME variable somewhere but he could not remember.
I have check /etc/profile, /etc/bashrc, /root/.bash_profile, /root/.bashrc. All these files does not have a line to set JAVA_HOME, but it still keep coming back with old values.
So, is there a way to find out where is it coming from?

Here's one crude but effective way:
cd /
grep -r "JAVA_HOME" .
This will recursively search all subdirectories under the starting point (the UNIX root directory, in this example) for any file containing the string 'JAVA_HOME'.
You can use grep -r --include="*.ext" "JAVA_HOME" . if you want to restrict your searches to files having the extension .ext.

Related

Why does Yocto use absolute paths in TMPDIR?

Changing the path of a Yocto environment is not a good idea, as I found out. This also explains why e.g. bitbake can be run regardless the current working directory. Absolute paths are stored in many places during the build process, even subdirectory structures are created into the tmp directory tree. I ended up in rebuilding from scratch - which takes a long time.
A documentation of how I tried to modify all paths:
find . -name *.conf -exec sed -i 's/media\/rob\/3210bcd4-49ef-473e-97a6-e4b7a2c1973e/home/g' {} +
This step replaces absolute paths, within many dynamic conf files (from xx/xx/linux to /home/linux - where linux was chosen for historical reasons. I could mount the partition also as /home/yocto or whatever name).
Next was deletion of subdirectory structures with the old path in the hope that the build process would recognize these deletions, and still rebuild quickly:
find . -name *3210bcd4-49ef-473e-97a6-e4b7a2c1973e* -exec fakeroot rm -r {} +
It was not recognized. Then I gave up.
From a user new to Yocto, familiar with former/classic crossbuild environments based on make menuconfig etc.
My question is:
Why are absolute paths generated & used throughout tmp instead of treating everything as relative?
Or, asked differently:
Why not use something like ${TOPDIR}/tmp throughout the build configuration, instead of hardcoding the absolute path to tmp?

PATH in shell getting mysterious entry (fish shell, ubuntu system)

I am using fish shell on a Ubuntu sytem. This question is about redundancy in $PATH that I am setting unintentionally.
When I type echo $PATH, I get:
/opt/anaconda3/bin/ /opt/anaconda3/etc/fish/conf.d/ /opt/anaconda3/bin/ /opt/anaconda3/etc/fish/conf.d/ /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games .
Multiple annoyances with this: /opt/anaconda3/bin showing up twice, showing some non-existant directories: /usr/local/games /usr/games.
My /etc/login.defs reads:
ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin
So the redundancy is not coming from there. Of course, my PATH in config.fish is set as:
set -gx PATH /opt/anaconda3/bin/ (/opt/anaconda3/bin/conda info --root)/etc/fish/conf.d/ $PATH .
My Question: where does fish get its PATH from, other than what is set in my environment and what is handed to it by: /etc/login.defs?
UPDATE: I changed to using fish_user_paths variable, per documentation which got rid of /opt/anaconda3/bin added twice. Still the /usr/games/ and /usr/local/games are getting added automagically (and those directories don't exist on my system!).
After looking around on the Internet, this is not a shell related issue: fish shell or any other shell. It's a Linux issue. The kernel, somewhere before forking the init, reads the:
/etc/environment file and sets the system wide default path.
In a single user environment, we could just edit that file - if we insist. Personally, I added lines to my fish profile to purge the non-existant directories from the path :
if set -l index (contains -i -- /usr/local/games $PATH)
set --erase PATH[$index]
end
if set -l index (contains -i -- /usr/games $PATH)
set --erase PATH[$index]
end

Copy multiple files to different directories in Makefile

I have a Makefile where I currently have two files that should be copied to different directories. Currently, I've tested
echo ${dirs} | xargs -n 1 cp ${sources}
So I understand that this will not work since it will try to copy both source files to one of the directory every time. But is there a way that I can execute the copy command for every source file and directory each?
Best regards,
Simon
I think it is possible to deduce what you want from what you wrote, but as others pointed out, you should be more clear, so we don't have to spend time deducing it.
Anyway, since you want to not copy all files to all directories, you must somehow tell Make where you want to copy which files. The easiest way is to list the full paths of the copies you want in a variable such as $(COPIES), and not just ${dirs}. In this answer I am going to assume the destination directories already exist.
.PHONY: all
all: $(COPIES)
PERCENT := %
.SECONDEXPANSION:
$(COPIES): %: $$(filter $$(PERCENT)/$$(notdir $$*), $(sources)) Makefile
cp $< $#

How do we replace PATH in all the files with an env variable

I have around 230 files which are *.pl , *.txt and some are *.conf files which has a default path set to the current environment say /home/AD/USR/perl/5.8.0/bin/perl. I need to replace "/home/AD/USR" with an environment variable ${USR_PATH}. The files I want to modify are in subdirectories. Which means my script should find e.g find .|xargs grep -l "/home/AD/USR" all the files and then replace the string.
OLD: /home/AD/USR/perl/5.8.0/bin/perl
New : ${USR_PATH}/perl/5.8.0/bin/perl
Can some one give me a clue how do I do that?
Shell : /bin/bash
Env : Linux x86_64
If you replace part of a string with ${USR_PATH} you will refer to the perl variable $USR_PATH, not the environment variable, which is in perl referred to as $ENV{USR_PATH}.
perl -pi.bak -we 's#/home/AD/USR(?=/perl/5.8.0/bin/perl)#\$ENV{USR_PATH}#g'
*.pl *.txt *.conf
Using the lookahead will save you the trouble of replacing the rest of the path afterwards.
I assume you want to replace it with the literal value. If you want to replace it with the actual value in the environment variable, just remove the backslash in front of $ENV.
While using an environment variable seems handy and all, it will reduce your scripts portability. Why not use a configuration file? If you had done that from the start, you wouldn't be having this trouble. Search CPAN for a nice module.
perl -i -pe 's|/home/AD/USR/perl/5.8.0/bin/perl|\${USR_PATH}/perl/5.8.0/bin/perl|' <your files>

using grep and find commands - basic questions to help me sort it out in my simple mind

I am back with a second no-brainer question, but I would like to get this straight in my head.
I have an assignment in which I am charged with providing a command to find a file named test in my home directory (one command using find, and one using grep). I understand that using find is just 'find ~/test', but using grep, wouldn't I have to search out a pattern within the file 'test'? Or is there a way to search for the file (using grep), even if the file is empty?
ls ~ | grep test
I understand that using find is just 'find ~/test'
No. find ~/test will also have a match for every file or directory under the directory $HOME/test/. Rather use find ~ -type f -name test.
The assignment sounds unclear. But yes, if you give any filenames to grep, it will look at the contents of the files and ignore the names of the files. Perhaps you can grep the output of another command? Maybe ls as #Reese suggested, or maybe a different find command.
ls -R ~ | grep test
Explanation: ls -R ~ will recursively list all files and directories in your home folder. grep test will narrow down that list to files (and directories) that have "test" in their name.