You are not logged in.

#1 2009-03-20 08:47:49

plurt
Member
Registered: 2008-10-16
Posts: 88

[SOLVED] Bash - append input to multiple files after checking

I have this to append text to multiple files, so far I have this:

#!/bin/bash
for file in $(ls *.example);
do
    echo "Append this to my file." >> ${file};
done;

But I don't want to adjust the script each time so I need something more powerfull and interactive:
What I'd like it to do is: - ask me for the input (text to be appended)
                                  - ask me for wich type of file this action needs to be taken (using wildcard(.suffix))
                                  - before appending, check for duplication (if text is present, do not append)

any thoughts? smile

*edit for layout purpose tongue

Last edited by plurt (2009-03-26 11:09:02)


When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way

Offline

#2 2009-03-20 09:00:03

plurt
Member
Registered: 2008-10-16
Posts: 88

Re: [SOLVED] Bash - append input to multiple files after checking

after meddling around a bit, I've come to this (totally bogus and incomplete, but this is my first attempt to bash script tongue)

#!/bin/bash

echo -n "text to be appended?:"
read -e TEXTAPPEND
echo -n "append to what type of file?:"
read -e FILETYPE

for file in $(ls ${FILETYPE} | grep ${TEXTAPPEND});
if #output of grep=${TEXTAPPEND}
do
   #move on to next file
 else
    echo "${TEXTAPPEND}" >> ${file};
done;

am I getting anywhere with this? smile


When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way

Offline

#3 2009-03-20 13:02:24

klixon
Member
From: Nederland
Registered: 2007-01-17
Posts: 525

Re: [SOLVED] Bash - append input to multiple files after checking

You are...
change

for file in $(ls ${FILETYPE} | grep ${TEXTAPPEND});
if #output of grep=${TEXTAPPEND}
... more stuff...
done

into

for file in *.${FILETYPE}; do
  if grep "${TEXTAPPEND}" "${file}"; then
    continue
  else
    echo "${TEXTAPPEND}" >> "${file}"
  fi
done

Last edited by klixon (2009-03-20 13:04:17)


Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!

Offline

#4 2009-03-20 13:06:36

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Bash - append input to multiple files after checking

That sounds a bit dangerous and limiting.

Why don't you make a script that can interpret a file that looks like:

print exit successful
return 0
APPEND_TO
io.c
/var/log/io.c.log
bar.c
-----
print exit failure
return 1

#EOF
APPEND_TO
bio.c
foo.bar
-----
1234567
APPEND_TO
/tmp/socket.pipe

Like:

#! /bin/bash
mode=text
text=""
while read line; do
if [ "$line" = APPEND_TO ]; then mode=file; continue; fi
if [ "$line" = ----- ]; then mode=text; text=""; continue; fi
if [ $mode = text ]; then [ -n "$text" ] && text="$text
$line"; [ -z "$text" ] && text="$line"; fi
if [ $mode = file ]; then [ -f "$line" ] && echo "$text" >> "$line"; fi
done

Mind the newline that is in quotes when appending a new line to text.

EDIT:

Actually I am not so sure if it is better. Run it with ./bash_changer.sh < changes.txt by the way, and you could do the same with yours with a file that is like
line_1
foo.bar foo2.bar
line_2
foo.bar foo2.bar

It's just a different approach then.

Last edited by Procyon (2009-03-20 13:28:34)

Offline

#5 2009-03-20 17:50:01

plurt
Member
Registered: 2008-10-16
Posts: 88

Re: [SOLVED] Bash - append input to multiple files after checking

very interesting smile

thanks for the great input, i'm learning here.

funny bug I have so far, look at this testrun smile

[18:45:25][plurt@archaism:~/test] :) $ append.sh
this is a list of files in current directory:
1.test    2.test    3.test
text to be appended?:test1
append to what type of file?:test
grep: *.: No such file or directory
[18:45:38][plurt@archaism:~/test] :) $ ls
*.  1.test  2.test  3.test

the program created a *.

[strike]my[/strike] The code so far is this:

#!/bin/bash

echo -n 'this is a list of files in current directory:'
echo -e

 ls

        echo -n 'text to be appended?:'
        read -e textappend
        echo -n 'append to what type of file?:'
        read -e filetype


for file in *.${FILETYPE}; do
  if grep "${TEXTAPPEND}" "${file}"; then
    continue
  else
    echo "${TEXTAPPEND}" >> "${file}"
  fi
done;

Last edited by plurt (2009-03-20 17:57:13)


When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way

Offline

#6 2009-03-20 18:14:46

xaff
Member
Registered: 2009-02-26
Posts: 64

Re: [SOLVED] Bash - append input to multiple files after checking

if you use variable, it has to be:

for file in $(ls *.$FILETYPE); do

edit: oh and afair variables in bash are case sensitive

Last edited by xaff (2009-03-20 18:16:04)

Offline

#7 2009-03-20 18:18:58

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Bash - append input to multiple files after checking

xaff wrote:

if you use variable, it has to be:

for file in $(ls *.$FILETYPE); do

edit: oh and afair variables in bash are case sensitive

How come it works in an interactive shell?

touch {1,2,3}.test
type=test
for file in *.$type; do echo $file; done
1.test
2.test
3.test

EDIT
Ahh.... It was the case sensitive issue, I see you edited your post.

Last edited by Procyon (2009-03-20 18:29:54)

Offline

#8 2009-03-20 20:41:36

Arm-the-Homeless
Member
Registered: 2008-12-22
Posts: 273

Re: [SOLVED] Bash - append input to multiple files after checking

echo -n "text to be appended?:"
read -e TEXTAPPEND

Instead of that, you can do:

read -p "text to be appended?:" TEXTAPPEND

Offline

#9 2009-03-20 21:12:34

plurt
Member
Registered: 2008-10-16
Posts: 88

Re: [SOLVED] Bash - append input to multiple files after checking

nice!


When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way

Offline

Board footer

Powered by FluxBB