LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH script (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-4175504071/)

sachin.davra 05-06-2014 08:12 AM

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

bloodstreetboy 05-06-2014 08:21 AM

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.

sachin.davra 05-06-2014 08:25 AM

Quote:

Originally Posted by bloodstreetboy (Post 5165625)
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.

bloodstreetboy 05-06-2014 08:34 AM

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


sag47 05-06-2014 08:42 AM

*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.

szboardstretcher 05-06-2014 08:45 AM

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?

jpollard 05-06-2014 09:11 AM

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.

sachin.davra 05-07-2014 01:43 AM

Thnx guys for your support.
i was looking exactly same stuff.


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