Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing this script to work as a fast draft. It opens my text editor with date and time, so I can insert some draft text to save quickly.

1. At first I'd like to append the date and time at the beginning of the file instead of at the end, as >> $file does. But I guess that it is not so easy, so this is not my priority.

2. I defined an argument $1. If I have only few words to insert I believe that it is better to run draft "foo foo" instead of opening the editor to write just few words.

Well, it is working but with the argument the last line is not necessary. So I'd like to make it a conditional line. If I pass no argument, then open editor; else none.

Any help to improve my script?

#!/bin/bash
file=rascunho
folder=/home/$USER/
#linha=$(wc -l < $file)
content=$1
#rm $folder$file
printf "\n\n" >> $file
#echo " " >> $file
echo "------< $(date "+%b %d, %Y - %H:%M:%S") >------" >> $file
echo "$1" >> $file
exec leafpad  $file
share|improve this question
    
Use a version control system, it will keep track of such stuff for you. –  vonbrand Jan 21 '13 at 17:49
add comment

2 Answers

up vote 3 down vote accepted

You can use if to check. For example, you can do something like this instead of the last two lines in your script above:

if [ -n "$1" ]; then
  echo "$1" >> $file
else
  exec leafpad $file
fi

This says: if the first argument is not an empty string (this is what -n test does), then run echo, else run leafpad.

You can read more about this here:

Hope this helps.

share|improve this answer
add comment

To be more generic, use "$@" instead of "$1" -- this lets you write

draft use many words without quoting.
#!/bin/bash
file=rascunho
printf "\n\n------< %s >------\n" "$(date "+%b %d, %Y - %T")" >> $file
# if any arguments were given, write them to the file, else edit the file
if [[ $# -gt 0 ]]; then
    echo "$@" >> $file
else
    exec leafpad $file
fi

To write a line at the beginning of the file, you can use a grouping construct:

{ echo "first line"; cat $file; } > temp && mv temp $file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.