LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2020, 12:55 PM   #1
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Rep: Reputation: Disabled
Writing a simple bash script to take notes


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:

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
Next, he add a line of code to show a confirmation message to the user with the note:

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo Note saved: $*
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?
 
Old 04-10-2020, 12:57 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,992

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
would be nice to post your script.
 
Old 04-10-2020, 01:00 PM   #3
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
would be nice to post your script.
I didn't because is the same as the one that is in the video which I include in the initial post.

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo Note saved: $*
 
Old 04-10-2020, 01:01 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
If you type into the command line exactly this:
Code:
echo $(date): abc
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
 
Old 04-10-2020, 01:03 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,992

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by Ghost-Order View Post
I didn't because is the same as the one that is in the video which I include in the initial post.

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo Note saved: $*
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.
 
Old 04-10-2020, 01:04 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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.
 
Old 04-10-2020, 01:36 PM   #7
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
If you type into the command line exactly this:
Code:
echo $(date): abc
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 View Post
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:

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo Note saved: $*
and

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo $(date): $* >> ~/notes2.txt*
But in both cases only the first line is executed (and correctly), the second line is not.

Quote:
Originally Posted by rtmistler View Post
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

Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo Note saved: $*
And just with that the terminal shows the message.
 
Old 04-10-2020, 02:20 PM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,147
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
Code:
while :; do
    read -p "Write note then press enter: " note
    echo "$note" >> notes.txt
done
 
Old 04-10-2020, 03:07 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by Ghost-Order View Post
The output of $ echo $SHELL is bash: $: command not found
Do not add the leading $, that was simply the prompt character in the question.


Quote:
Originally Posted by Ghost-Order View Post
Code:
#!/bin/bash
#Simple note-taking script

echo $(date): $* >> ~/notes.txt
echo $(date): $* >> ~/notes2.txt*
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
 
Old 04-10-2020, 03:07 PM   #10
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-10-2020, 03:07 PM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Code:
#!/bin/bash
#Simple note-taking script

echo "$(date): $*" >> ~/notes.txt
echo "Note saved: $*"
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.
 
Old 04-10-2020, 03:10 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by Ghost-Order View Post
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!

Last edited by astrogeek; 04-10-2020 at 03:15 PM.
 
Old 04-10-2020, 03:21 PM   #13
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Ah! A kind of path confusion indeed! Thanks for the follow-up!
Yep, sorry for that.

Quote:
Originally Posted by michaelk View Post
Code:
#!/bin/bash
#Simple note-taking script

echo "$(date): $*" >> ~/notes.txt
echo "Note saved: $*"
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.
 
Old 04-15-2020, 02:47 AM   #14
slackartist
Member
 
Registered: Oct 2015
Location: Where the buffalo's roam
Distribution: Slackware Current Multilib
Posts: 161
Blog Entries: 20

Rep: Reputation: 26
fixed

Last edited by slackartist; 04-21-2020 at 01:26 AM.
 
Old 04-16-2020, 12:37 AM   #15
Ghost-Order
Member
 
Registered: Jan 2020
Distribution: Arch Linux
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by slackartist View Post
you could look at this too. i think i spent a couple years look into this and learning a lot in between but since you ask.

https://www.chiark.greenend.org.uk/~...m/aliases.html
dude youll need this if you want to compile notes correctly with the bash line variables
Hey, thank you for the info, I'll make sure to check it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recovering the notes of sticky notes dissident_goodchild Linux - Software 5 01-31-2020 12:12 AM
[SOLVED] what is the "sticky notes" option in XFCE pop-up notes? newbiesforever Linux - Software 1 12-11-2018 02:22 PM
LXer: Canonical Closes Ubuntu One Notes Service, Deletes People’s Notes LXer Syndicated Linux News 0 02-08-2013 09:10 PM
Notes for configure RedHat 5.0 Hand writed notes sandeeprhce5 Linux - Newbie 2 02-17-2010 01:16 PM
Any utility to write notes and search in notes? VicRic Linux - Newbie 2 08-29-2007 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration