Hi!!! what is wrong with this??
This script is supposed to grab the last line of text for all the files in a directory and create a file with those lines if it were to work at all... some lines that start with # do not work and I need to keep pressing enter for it to work when it should be automatic... Any help???
#!/usr/bin/bash # touch < date "+%Y-%m-%d-%H-%M-%S" NOW=$(date "+%Y-%m-%d-%H-%M-%S") LOGFILE="log-$NOW.log" touch $LOGFILE FILES=/Users/zaphod/Desktop/me/data/*.dat for f in $FILES #TODAY="Today is $NOW" #echo $TODAY #cat $TODAY >> $LOGFILE do echo "processing $f file..." # echo -e "\n" # tail -n 1 $f tail -n 1 $f cat -u | (tail -n 1 -q $f) > "\n" >> $LOGFILE #echo -e "\n" done |
1. use 'echo' to output strings
2. use cat to output files 3. Code:
# replace this http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ 4. To see what the parser is doing, amend top of script Code:
#!/usr/bin/bash 6. it would help to know exactly what OS, Sw you have Code:
cat /etc/*release* |
Thanks Chrism01. This code is meant to run on Debian. I ran out of time on the weekend and had to change the paths so it would work on OS X so I could work on my laptop during weekdays. I have done everything I know to make it OS neutral. But, it shows I don't know crap; debian bash is version 4.2 but OS X version is 3.2.
|
Well, hope that helped.
Have a read of those links; they're very good :) |
To start with, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
Let me go line-by-line, so I can point out some things. Code:
# touch < date "+%Y-%m-%d-%H-%M-%S" Code:
LOGFILE="log-$NOW.log" http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes Now, in this specific case it doesn't cause problems because the expanded string doesn't have any whitespace or globbing characters. But it's a good idea to always do it properly anyway. And since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them. Code:
FILES=/Users/zaphod/Desktop/me/data/*.dat Code:
files=( /Users/zaphod/Desktop/me/data/*.dat ) You might also consider not hard-coding the file path like that, but instead cd'ing into that directory at the beginning of the script, so you could use a simple "for f in *.dat". Note too that many scripters feel that it's better to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as they are not separate commands but the closing indicators for the test block, paired directly with the opening keyword. Putting them together on one line thus more clearly separates the outside block from the inside block. Code:
# echo -e "\n" Code:
cat -u | (tail -n 1 -q $f) > "\n" >> $LOGFILE chrism01 has already posted how to run tail correctly. But if you ever need to add a newline to a file, just run "echo >> filename" after the main command. Finally, beware the Useless Use Of Cat! ;) |
Thanks!!!
This is a working and more elegant version ( I think) of the script you guys helped me out with:
Code:
#!/bin/bash |
Quote:
Code:
printf '%s\n' 'H' '0a' "$NOW" '.' 'w' | ed -s d_file.txt http://mywiki.wooledge.org/BashFAQ/090 How can I redirect the output of 'time' to a variable or file? http://mywiki.wooledge.org/BashFAQ/032 How to use ed: http://wiki.bash-hackers.org/howto/edit-ed http://snap.nlc.dcccd.edu/learn/nlc/ed.html (also read the info page) PS: Now you're really using printf incorrectly. The idea behind printf is that you first define a formatting string, and this format is then used to print out the subsequent arguments. The first argument should thus always be the format string, which should generally contain at least one "%" placeholder. This is followed by one or more argument strings that will replace those placeholders. printf will then loop until all arguments are consumed. And don't forget that, unlike echo, printf does not include any newlines unless you explicitly include them. If you don't need to do fancy formatting on repeated strings, just use echo. Code:
#echo simply prints all arguments, followed by a newline. See the link I gave before for more details. And please don't forget what I said before about quoting variables! |
All times are GMT -5. The time now is 12:05 AM. |