LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2014, 09:12 AM   #1
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Rep: Reputation: Disabled
BASH script


i want to write a script. i am using centos 6 x64.
basically i want to write a script in which i need to go to home directory of user in every machine. but i dont remember the username of every user. so what is the simbol to use before username. script is something like below. basically i want to enter something in .bashrc file for every user.
Code:
echo "export LD_LIBRARY_PATH=/opt/R-2.15.2/lib:$LD_LIBRARY_PATH" >> /home/$username/.bashrc
 
Old 05-06-2014, 09:21 AM   #2
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Rep: Reputation: 37
The symbol is tild (~)
If you use
Code:
cd ~
You reach into the logged in user's home directory.

If you use
Code:
cat ~/.bashrc
It opens the .bashrc file of every user, you do not have to write the username. It is valid for every user.

Last edited by bloodstreetboy; 05-06-2014 at 09:23 AM.
 
Old 05-06-2014, 09:25 AM   #3
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bloodstreetboy View Post
The symbol is tild (~)
If you use
Code:
cd ~
You reach into the logged in user's home directory.

If you use
Code:
cat ~/.bashrc
It opens the .bashrc file of every user, you do not have to write the username. It is valid for every user.
basically i want to go to user home directory and there is only one directory under /home
could you help me to know how can i achieve this task.
 
Old 05-06-2014, 09:34 AM   #4
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Rep: Reputation: 37
If you are root and you have to go to the user's home directory and if there is only one directory in /home
Code:
#!/bin/bash
dir1=$(ls /home)
cd "/home/$dir1"
You can reach into user's home directory

If you are root and you have to go to the user's home directory and if there are more than one directory in /home
Code:
i=1
dir3=$(ls -1 /home | wc -l )
while [ $i -le $dir3 ]
do
dir2=$(ls -1 /home | tail -n +$i | head -n 1)
cd "/home/$dir2"
## now root is in the user's home directory, write the task here what you want to achive after going into user's directory
## the task should be common for all users because when loop will take its second round, it will be in second user's home directory then third and so on
i=`expr $i + 1`
done

Last edited by bloodstreetboy; 05-06-2014 at 09:48 AM.
 
1 members found this post helpful.
Old 05-06-2014, 09:42 AM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
*EDIT jpollard makes a very valid point. You should refer to his post #7 instead of mine.

Not all users necessarily have their home directory in /home. ~ and ~user will require you to know the usernames beforehand. Your best bet is to parse /etc/passwd for users greater than some UID. In my case uid >=1000 and excluding the nobody user. Take for example this entry in /etc/passwd...

Code:
sam:x:1000:1000:Sam,,,:/home/sam:/bin/bash
It is a colon separated value. The first entry is the user name. The third entry is the uid. The sixth entry is the home directory. That's where you should start. A quick awk script prints all users' home directories whom have a uid 1000 or greater and not the nobody user.

Code:
awk 'BEGIN {FS=":"};$3 > 999 && $1 != "nobody" {print $6}' /etc/passwd
You can pipe the output into a while loop and start processing whatever you want to process. You may need to adjust the script for the uid range of your system. Note I corrected your echo statement because I assume you want the literal text to output and not the variable to be replaced.

Code:
#!/bin/bash
awk 'BEGIN {FS=":"};$3 > 999 && $1 != "nobody" {print $6}' /etc/passwd | while read x;do
  echo 'export LD_LIBRARY_PATH="/opt/R-2.15.2/lib:$LD_LIBRARY_PATH"' >> "${x}/.bashrc"
done
You should fully understand the above code before executing it.

Last edited by sag47; 05-06-2014 at 12:00 PM.
 
1 members found this post helpful.
Old 05-06-2014, 09:45 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
If you have multiple users on each machine, you will want to apply this to each user no? So maybe something like:

Code:
list1=$(ls /home)
for i in $list1; do echo "your text" >> $i/.bashrc; done
and don't forget root, if you need it there as well?

Last edited by szboardstretcher; 05-06-2014 at 09:46 AM.
 
1 members found this post helpful.
Old 05-06-2014, 10:11 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Why would you add that to every user?

Put it in the /etc/profile or /etc/bashrc and let everyone get it at once.

Makes it easier to adjust later instead of just accumulating more errors as the system evolves.
 
1 members found this post helpful.
Old 05-07-2014, 02:43 AM   #8
sachin.davra
Member
 
Registered: Jul 2013
Posts: 213

Original Poster
Rep: Reputation: Disabled
Thnx guys for your support.
i was looking exactly same stuff.
 
  


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
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 02:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 09:25 AM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 01:05 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 10:33 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