Results 1 to 7 of 7
Hey guys and girls, I'm just starting out with bash scripting (yesterday, really). I'm stumped.
I want to add a file to each user's home directory, pretty simple really, and ...
- 06-01-2011 #1Just Joined!
- Join Date
- Nov 2007
- Location
- Salt Lake City, UT, USA
- Posts
- 32
Program to use Grep w/regex
Hey guys and girls, I'm just starting out with bash scripting (yesterday, really). I'm stumped.
I want to add a file to each user's home directory, pretty simple really, and send it out via our Apple Remote Desktop system to our Macs.
Here is my script:
This script, while run as root, will create the .tcshrc file owned by root. I would like to change ownership of this newly created/modified file as well, while I'm going through this trouble. I haven't been able to figure out yet how to do this, however. It seems that in my program, I am looping through each user's directory anyway, so why not pull out the username from the folder path and do a chown with it (/Users/Joe , /Users/Fred ,/Users/Amy).Code:#!/bin/bash for i in $(ls -d /Users/*) do
if [ -e $i/.tcshrc ] thendoneecho "$i/.tcshrc exists!"elseecho "$i/.tcshrc does not exist" echo -e "alias l ls" >> $i/.tcshrcfi
I have tried adding a line such as this:
which doesn't do anything, also separately this:Code:chown $(egrep -E (?<=./).* $i) $i/.tcshrc
to with no success. I have encountered numerous syntax errors that complain about an unexpected token `)' ... I have sort of fuddled through them by separating each item of my regex with single quotes: '(''?''<''=''.''/'')''.''*' , but this hasn't worked either.Code:export comp=$(egrep -E (?<=./).* $i) echo "$comp"
I am not against changing around how the program works with this, in order to do the chown for the new file in each user's home directory. Maybe awk, or grep, or something else would work better? I don't know.
Any thoughts on where I'm going wrong with this?
- 06-02-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
how about:
Code:#!/bin/bash cd /home for i in $(ls -d *); do if [ -e $i/.tcshrc ]; then echo "$i/.tcshrc exists!" else echo "$i/.tcshrc does not exist" echo -e "alias l ls" >> $i/.tcshrc chown -v $i $i/.tcshrc fi done
- 06-02-2011 #3Just Joined!
- Join Date
- Nov 2007
- Location
- Salt Lake City, UT, USA
- Posts
- 32
It's really the simple things that make this so fun.
Thanks a lot for the help Atreyu, how easy was that?
- 06-02-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
agreed.
easy peasy! you had the hard work already done
- 06-02-2011 #5Just Joined!
- Join Date
- Nov 2007
- Location
- Salt Lake City, UT, USA
- Posts
- 32
Aw crap. Now I've broken it in one more place.
My script goes through just fine with the "cd /Users" line, but will break when it gets to the /Users/Shared folder. It tries to do "chown Shared /Users/Shared/.tcshrc" , and breaks since there is no shared user, only a shared folder.
I am trying various iterations of an if statement with an or in the conditions, but I'm clearly missing something.
I've changed my initial if statement to the following:
I've found that no matter what I do after -o, it returns true and fails to ever reach the else statement that writes the files. Can anyone shed some light on the "or" problem here? This is on a Mac, 10.6.7, btw.Code:if [ -e $i/.tcshrc -o "$i"=Shared]
- 06-02-2011 #6Just Joined!
- Join Date
- Nov 2007
- Location
- Salt Lake City, UT, USA
- Posts
- 32
OK,
I think I got it.
I found that in doing an elif statement, spacing is key.
This didn't work:
but this did...Code:elif [ "$i"="Shared" ]
Going back to the books....Code:elif [ "$i" = "Shared" ]
- 06-03-2011 #7Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
good catch. yeah, spacing is particular in bash.
also, good practice is to use double equals ( == ) when comparing strings and single equals ( = ) when assign a value to a variable.


Reply With Quote