2

Background: I have a bunch of filenames named username.sub in single letter directories under script_testing (first letter of username is the folder name). For every username.sub, I need to check if the line user.$username.contacts exists and, if not, append the line followed by a real tab.

Question: Given the code I have below, why is it not appending to the file? I think I am missing something simple. I keep getting "contacts already subscribed" even if that line is not there.

#!/bin/bash
 Path_to_files=/home/user/script_testing/^[A-z]+$/
 FULLNAME="${Path_to_files##*/}"
 NAME="${FULLNAME%.*}"
 if grep 'contacts' $NAME.sub; then
   echo 'contacts already subscribed'
else
    echo "subscribing to contacts"
    echo -e user.$NAME.Contacts \t >> $NAME.sub
fi
1
  • Can the file username.sub have more than one lines with contacts in them? Commented Jun 10, 2013 at 19:51

2 Answers 2

1

You're grepping for the word contacts - which, depending on what else you have in those files, may always be present.

Instead, use grep -q "^user\.$NAME\.Contacts" to look for your line.

Sign up to request clarification or add additional context in comments.

5 Comments

Granted, that was overlooked. On my test file, there is only 1 other line - user.$NAME.folder Now I am getting the expected echo response, but nothing is actually being written to the file
If you're getting "subscribing to contacts" but nothing is added to the file, you probably don't have write permission to it. Also, I'd recommend double quoting the argument to echo, so your \t doesn't get lost on the way.
I feel like I'm looking for something right in front of my face and can't find it. I am now getting "contacts already subscribed" no matter what, and the file is not being written to. I do have write permissions. As it stands now, I made the suggested changes and I eliminated the regex in line 2 just in case I had it wrong. Testing on a single file now - which, if I hardcode everything in w/out variables, works fine.
@user2472122: Suggest you look at $?, which for grep returns 2 if it can't open the file, and 1 if it can't find a match.
I'm a dork. It is creating/appending to a file in the same directory as the script instead of the directory where the username.sub files are. I will have to figure out those adjustments now
1

Fixed with the following:

#!/bin/bash
#testing directory
#p=$HOME/script_testing
for f in "$p"/*/*.sub ; do
# if this is a file
 if [ -f "$f" ]; then
# define variables
F="${f##*/}"
u="${F%%.*}"
cont=$(grep "user.$u.Contacts" "$f")
cal=$(grep "user.$u.Calendar" "$f")
# if our file doesn't contain Contacts subscription
if [ -z "$cont" ]; then
 # add Contacts subscription
 echo -e "user.$u.Contacts\t" >> "$f"
#fi
# if our file doesn't contain Calendar subscription
elif [ -z "$cal" ]; then
 # add Calendar subscription
 echo -e "user.$u.Calendar\t" >> "$f"
fi
fi
done

Also added extra line(s) to append. Please, let me know if there is an issue with this so I can learn, but I haven't encountered any problems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.