Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Location
    Salt Lake City, UT, USA
    Posts
    32

    Question 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:
    Code:
    #!/bin/bash
    
    for i in $(ls -d /Users/*)
    do
    
    if [ -e $i/.tcshrc ] then
    echo "$i/.tcshrc exists!"
    else
    echo "$i/.tcshrc does not exist" echo -e "alias l ls" >> $i/.tcshrc
    fi
    done
    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).

    I have tried adding a line such as this:
    Code:
    chown $(egrep -E (?<=./).* $i) $i/.tcshrc
    which doesn't do anything, also separately this:
    Code:
    export comp=$(egrep -E (?<=./).* $i)
    echo "$comp"
    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.

    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?

  2. #2
    Linux 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

  3. #3
    Just 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?

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    agreed.

    easy peasy! you had the hard work already done

  5. #5
    Just 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:
    Code:
    if [ -e $i/.tcshrc -o "$i"=Shared]
    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.

  6. #6
    Just 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:

    Code:
    elif [ "$i"="Shared" ]
    but this did...
    Code:
    elif [ "$i" = "Shared" ]
    Going back to the books....

  7. #7
    Linux 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...