You are not logged in.
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?
*edit for layout purpose
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
after meddling around a bit, I've come to this (totally bogus and incomplete, but this is my first attempt to bash script )
#!/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?
When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way
Offline
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
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
very interesting
thanks for the great input, i'm learning here.
funny bug I have so far, look at this testrun
[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
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
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
echo -n "text to be appended?:"
read -e TEXTAPPEND
Instead of that, you can do:
read -p "text to be appended?:" TEXTAPPEND
Offline
nice!
When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way
Offline