LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2019, 03:03 AM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 81

Rep: Reputation: Disabled
adding var to /etc/profile


Hi,

I'd like to add these 2 lines to /etc/profile but only if it not has been added yet

Code:
echo "MODDED=$(echo -e "\e[92m1.0\e[0m")" >> /etc/profile
echo "export MODDED" >> /etc/profile
How could I do that? Thanks.

I was thinking about this:
Code:
if [ "$MODDED" = '' ];then 
	echo "appending to /etc/profile"
fi
But is there a more elegant way?
 
Old 03-14-2019, 05:23 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,703

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
It should go into your own .bashrc, not the global profile

Code:
if grep -vq '^MODDED=' ~/.bashrc; then
    cat >>~/.bashrc <<"EOF"
MODDED="$(printf '\033[92m1.0\033[0m')"
export MODDED
EOF
fi
 
1 members found this post helpful.
Old 03-14-2019, 09:16 AM   #3
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 81

Original Poster
Rep: Reputation: Disabled
it should not since Id like it system wide for all future users, thanks though!
 
Old 03-14-2019, 12:46 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,200

Rep: Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541Reputation: 2541
To create a system wide environment variable you can simply add lines to create and export in the desired file but it depends on how you are using the variable. Since you are "creating" the variable there is no need to check if it exists.

https://www.tecmint.com/set-unset-en...bles-in-linux/
 
Old 03-14-2019, 04:07 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,025
Blog Entries: 7

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
not elegant.
i don't understand why you want to automatically add that to the file. there must be better ways.
think about what you want to check for:
  • the variable is defined
  • the variable is defined and not empty
  • the file contains the string "MODDED=something", and it's not commented out
and then think about what the appropriate action should be:
  • simply define & export the variable after the test
  • or really write that bit to that file? it won't be globally defined until you log out/in!
???
for the last I think NevemTeve's answer is good.
for the first two, you might want to read
Code:
help test
 
Old 03-14-2019, 06:36 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,554

Rep: Reputation: 868Reputation: 868Reputation: 868Reputation: 868Reputation: 868Reputation: 868Reputation: 868
Why do you need a script?
Open the file in your favorite text editor and modify it. When you do, you should see this:
Quote:
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
 
1 members found this post helpful.
Old 03-15-2019, 12:47 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,703

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Let's hope it's not some wannabe-haxor-activity... Why is terminal-color added into a version-number? And what is MODDED?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with scp (ssh) on adding /usr/bin/fortune to /etc/profile & .bashrc (Fedora) funkymunky Linux - Networking 6 09-12-2014 03:06 AM
[SOLVED] get var value when var name is part of another var bilyboy65 Linux - Newbie 9 10-21-2012 09:32 AM
/etc/profile V.S. /etc/bashrc Takayuki Linux - General 12 05-17-2011 11:25 PM
/etc/profile vs. /etc/bash.bashrc jbrashear Debian 3 03-11-2004 03:49 PM
Adding a file to the path in etc/profile zzzt Linux - Newbie 3 10-24-2003 03:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration