[SOLVED] Writing a simple bash script to take notes
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, I'm recently following an online course about Bash scripting, in the video the teacher write a simple script to takes notes and save those in a txt file, the script file name is "tn" and is as follows:
So when he runs the script "tn this is a new note" the terminal displays the message "Note saved: this is a new note" instead of showing nothing as before. However, when I write the script myself and then run it on the terminal, it doesn't display the confirmation message I just implemented in the script(although it write my note correctly in the txt file), in fact it seems that the second line of the script as a whole is not executed because I tried replacing echo Note saved: $* with echo $(date): $* >> ~/notes2.txt so when I run the script it should create a notes2.txt file and then write my note into it but it doesn't.
I checked again and again and my script is exactly the same as the video, sorry if this is a utterly simple question, but I really don't understand why it isn't working, what could be the problem?
If you also wish to see the result in stdout, meaning the terminal, there's a notation, but I'm unsure of it. I tried a few things and wasn't successful. This is related to Linux/Unix I/O redirection.
It should echo the date and also the string 'abc'.
Does this not work correctly on your system?
Can you also provide the output you see when you type:
Code:
$ echo $SHELL
That works correctly, the output is Fri 10 Apr 2020 01:23:42 PM -05: abc
The output of $ echo $SHELL is bash: $: command not found
Quote:
Originally Posted by pan64
No, it is not the same, you told you modified it to write into notes2.txt. The script you posted will never write into that file.
My apologize, that's because seeing that echo $(date): $* >> ~/notes2.txt didn't work I remove it and put back the echo Note saved: $* original line. Anyway I tried with:
But in both cases only the first line is executed (and correctly), the second line is not.
Quote:
Originally Posted by rtmistler
Note also that this part of the term:
Code:
>> ~/notes.txt
causes the output to go to the notes.txt file.
If you also wish to see the result in stdout, meaning the terminal, there's a notation, but I'm unsure of it. I tried a few things and wasn't successful. This is related to Linux/Unix I/O redirection.
I see, but in the video the script consist just in this
Although it probably would not break the script in this simplest case, you should not name the file with a trailing * character because it will result in unexpected behavior in other cases (for example with any existing filenames which fit the glob pattern).
I wonder if you have a confusion with the path as might result from working from a subdirectory and not seeing the file written to your home directory. Are you typing the code yourself or copy/pasting it into a file which might result in some character encoding problems.
That would not explain all that you have said, but I think you need to be a little more specific about the platform (Linux distro, other?), the execution environment and the exact code and commands being given.
Last edited by astrogeek; 04-10-2020 at 03:08 PM.
Reason: tpoy
I found the problem, I have 2 'tn' files in different locations, and I was modifying the wrong one. Now I just feel dumb lol. Anyways Thank you for the help.
The script does work as posted. The author leaves out the quotes which prevent word splitting and globing. If there isn't an end of line character on the last line the script will not execute properly although I would of expected a run time error. Edit the file and move the cursor to the end of the line past the last double quote(") assuming you modify your script as above. Now press the enter key, save the file and try running it again.
How it works. The $* is an array of the command line arguments. When you run the script i.e
tn this is a new note
tn is the script that runs and this is a new note are command line arguments. Each argument is stored in a variable i.e $1=this $2=is and so on. $* expands all of the command line arguments into a string.
Code:
echo "$(date): $*" >> ~/notes.txt
This line writes the current date/time and command line arguments to the notes.txt file in your home directory
Code:
echo "Note saved: $*"
Outputs Note saved: this is a new note to the terminal.
I found the problem, I have 2 'tn' files in different locations, and I was modifying the wrong one. Now I just feel dumb lol. Anyways Thank you for the help.
Ah! A kind of path confusion indeed! Thanks for the follow-up!
The script does work as posted. The author leaves out the quotes which prevent word splitting and globing. If there isn't an end of line character on the last line the script will not execute properly although I would of expected a run time error. Edit the file and move the cursor to the end of the line past the last double quote(") assuming you modify your script as above. Now press the enter key, save the file and try running it again.
How it works. The $* is an array of the command line arguments. When you run the script i.e
tn this is a new note
tn is the script that runs and this is a new note are command line arguments. Each argument is stored in a variable i.e $1=this $2=is and so on. $* expands all of the command line arguments into a string.
Code:
echo "$(date): $*" >> ~/notes.txt
This line writes the current date/time and command line arguments to the notes.txt file in your home directory
Code:
echo "Note saved: $*"
Outputs Note saved: this is a new note to the terminal.
Nevermind.
Thank you. Although as I said it was a location mistake from me, your explanation expand a little more my knowledge about this topic.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.