Paul Sutton

Write

Bash Scripting 20 – Write date and data a file

So following on from the previous scripts, it may be useful to record both the date and some data associated with that date to a file.

I have found out how to do this, but also tidied my script up, removed some unneeded code (quite a lot) and this should work.

echo "date" "data" # for reference, not written to file 
for ((i = 0 ; i < 10 ; i++)); do
    echo $(date), $((RANDOM % 20)) # echo to screen (stdout)
    echo $(date), $((RANDOM % 20)) > log3.csv
done

Produces

date data
Fri 18 Apr 12:48:16 BST 2025, 0
Fri 18 Apr 12:48:16 BST 2025, 15
Fri 18 Apr 12:48:16 BST 2025, 13
Fri 18 Apr 12:48:16 BST 2025, 6
Fri 18 Apr 12:48:16 BST 2025, 6
Fri 18 Apr 12:48:16 BST 2025, 13
Fri 18 Apr 12:48:16 BST 2025, 18
Fri 18 Apr 12:48:16 BST 2025, 5
Fri 18 Apr 12:48:16 BST 2025, 3
Fri 18 Apr 12:48:16 BST 2025, 14

Which is fine, but we can refine the date output so it is shorter with

date "+DATE: %D TIME: %T"
  date "+%D"   display date no label
  date "+%T"  display tine no label 

If we want this in a csv file it may help to add a comma

date "+DATE: %D, TIME: %T"

Remove the labels DATE: & TIME:

date "+ %D, %T"

Produces

date "+ %D, %T"
 04/20/25, 14:58:19

My final entry, in the script run by cron, at least as far as time and date is concerned

echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather.csv

As this sends the output to weather.csv, If we remove the redirect it will go to stout (standard output) we can just run the following on the terminal 'as is'

echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false)

This you should end up with

04/29/25, 15:44:14, Weather in Paignton: 16 °C - UVI: 5.91 - Wind: 0.89 m/s NNE - Humidity: 75% - Pressure: 1024 hPa

I have just copied and pasted the line in to the terminal and removed the >> weather.csv as that appends the csv file with the data.

As we all have different use cases, this solution is fine for me, I can adjust to suit further. This is far more useful for a CSV file, especially if you want to load the data in to a spreadsheet.

See fourth link below for full details. * Sources of help

https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script
https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash
https://linuxhandbook.com/bash-loops/
https://stackoverflow.com/questions/25242626/bash-redirect-output-to-log-file-with-day-date-in-the-name
https://phoenixnap.com/kb/linux-date-command

Tags

#Bash,#Bashscripting,#BashScripting,#Files,#Write,#Data,#Time,#Data


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 13 – Linux permission table

As previously mentioned, scripts need to be given permission to run. This table should help with the process of working out what permission to set on a file or script.

I think the usual default is 755 which gives the user read, write, execute. Both the group and other users of the system get read and write.

For the most part, you can set execute permission on a script with

chmod +x scriptname.sh
READ WRITE EXECUTE
USER GROUP OTHER USER GROUP OTHER USER GROUP OTHER
400 40 4 200 20 2 100 10 1

The markdown code for the above is

|         | **READ** |         |         | **WRITE** |         |         | **EXECUTE** |         |
|:-------:|:--------:|:-------:|:-------:|:---------:|:-------:|:-------:|:-----------:|:-------:|
|  _USER_ |  _GROUP_ | _OTHER_ |  _USER_ |  _GROUP_  | _OTHER_ |  _USER_ |   _GROUP_   | _OTHER_ |
| **400** |  **40**  |  **4**  | **200** |   **20**  |  **2**  | **100** |    **10**   |  **1**  |

Which may be useful. I used Tables Generator to help create this.

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#Bashscripting,#Files,#Permissions.#chmod,#Read,#Write, #Execute


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Index

Donate using Liberapay

A #AI #AbsorbtionSpectra #Abuse #Academy #Activism #Adenine #Afghanistan #Africa #Alphablocks #AMES #AminoAcid #AmnestyInternational #AMOC #Analytics #apg #Api #Apt #Aptitude #AralBalkan #Archaeology #Arduino #ARM #Artemis #arXiv #Assembler #AstroBiology #AstroChemistry #Astronify #Astronomy #Astrophysics #Atlantic #AtomicStructure #Atmosphere B #BlueGhost #bash #BASH #BASHShell #BashScripting #Bashscripting #BatchProcessing #Beamer #BepiColombo #better #BibTeX #BigBluButton #BigOil #BioChemistry #Biology #BioMass #Blender #Blog #Bonfire #Bookworm #Bookwyrm #Browser #Bullseye #Buster C #Castopod #Charity #CheatSheet #Chat #Chemistry #Chrome #Chromium #Climate #Code #CodeClub #Coding #Commons #Conditions #Conference #Console #Cornwall #Corona #CoronaVirus #Cosmology #Covid19 #cpd #CPD #Creative #CreativeCommons #CreativeEducation #CriticalThinking #CrudeOil #Cryptpad #Crystal #CTAN #CyberSecurity #Cytosine #Commands D #DarkMatter #Data #DBS #dcglug #dclug #Debconf #Debian #Decentralised #Decentralized #DeepLearning #Derived #Detox #Development #Devon #Diaspora #Digital #DiodeZone #Discussions #disroot #Disroot #dna #DNA #Docker #Documentation #Donate #Donation #Draft #DRM #Drupal #DosBox E #Editing #Education #EdX #Electronics #Elements #Element #emacs #Email #EmissionSpectra #Employment #Energy #EnergySavingWeek #Engine #Engine #ESAof #Ethics #Ethiopia #EventManagement #Events #EveryonesInvited #Exoplanet #Exploration #Euclid #Editor #Endof10 #endof10 #FalconsEye #Federated #Fediverse #Firefox #Flockingbird #Football #FootBall #Fosdem #FOSSandCrafts #FossileFuels #Foundation #Framablog #FramaBlog #Framework #FreeBSD #FreeBSD #Freedom #FreeSoftware #FreeSoftwareDirectory #Friendica #Friendica #FSF #FSFE #Funkwhale #Fusion #FutureLearn #ffmpeg G #Galaxy #Galculator #GameEngine #Games #Games #Gamma #GDPR #GettingStarted #Ghostreply #Gimp #Git #Gitlab #GitLab #gm #GNOME #GNU #GnuSocial #GoAccess #GoatCounter #GoDot #gold #Gopher #GPL #GraphicsMagick #Greek #Guanine #GUI H #Hack #HackerPublicRadio #Hacking #Hardware #Hexchat #HomeChemistry #HomeChemistry1 #HomeChemistry10 #HomeChemistry11 #HomeChemistry12 #HomeChemistry13 #HomeChemistry14 #HomeChemistry15 #HomeChemistry16 #HomeChemistry17 #HomeChemistry18 #HomeChemistry2 #HomeChemistry3 #HomeChemistry4 #HomeChemistry5 #HomeChemistry6 #HomeChemistry7 #HomeChemistry8 #HomeChemistry9 #Hosting #HPR #htop #Hubble #Hubzilla #HumanRights #Hypothesis #Help I #Image #ImageManipulation #Index #InfoGraphic #information #Inkscape #Invidious #IRC J #JamesWebb #Jit.si #Jitsi #JoeEditor #JoesOwnEditor #jpl #JPL #Jupyter #JupyterNotebook #JWST K #Kanban #kbin #KCSIE #KDE #KeepingChildrenSafeinEducation, #Kenya #kstars #KeyBinding L #LaTeX #Law #Learning #Lecture #Legal #Legislation #Lemmy #LGPL #LiberaPay #Libre #LibreAdventure #LibreLounge #Librem #Libreoffice #LibreOffice #LibreOfficeCalc #LibreOfficeDraw #LibreOfficeGettingStarted #LibreOfficeImpres #LibreOfficeWriter #LibrePlanet #Linux #LinuxMint #Lua #Luanti #Luanti #LXDE #Lynx #LPI #LinuxFoundation M #Magnesium #Management #Manganese #Map #Mapscii #Mars #Mastodon #Materials #Matomo #Matrix #Maya #Meeting #Meetings #mercury #Mercury #Meta #Micro.blog #mining #Misskey #mobile #Mobile #Mobilizon #Mobilizon #MolarSolutionCalculator #Moon N #NaCl #Nano #NationsLeague #Nebula #NetHack #network #NewSkillsAcademy #Nextcloud #NFL #NGINX #Nuclear #NuclearFusion #Nucleobases #NASA O #Ocean #Oil #OilProducts #Online #Online #OnlineSafetyBill #Open #OpenData #OpenLearn #OpenStreetMap #OpenUniversity #Orbitals #OU #Overleaf #Owncast #OwnCloud P #Package #Parker #PaigntonLibrarySTEMGroup #Pandas #Paper #ParticlePhysics #Particles #Password #Payment #Paypal #PDF #PeerTube #PeriodicTable #Phonics #Photo #Photograph #Photographs #Photos #Physics #pinebook #pinephone #PixelFed #Planet #Plausible #Pleroma #Plume #Podcast #PowderToy #Privacy #Production #Products #Programming #ProtoSchool #Public #Purism #Python #Python3 Q #Quark #Quarks R #RadioAstronomy #Reading #Recovery #RedBubble #RedCabbage #Research #Rights #RISC #RISCV #rna #RNA #RocksAndDiamonds #Rookie #RookieCamp #Rust S #Safeguarding #SaferInternetDay #Safety #Salt #Schools #Science #Science #ScienceDaily #Scismic #Scratch #Scratch2 #Scratch3 #SDTJ #Seagl #Security #Simulator #Sitejs #Skymaps #smallweb #Soccer #Social #SocialHome #SocialHub #SodiumChloride #Solarus #Solid #SouthDevonTechJam #Space #Stars #STEAM #Steam #Stellarium #Stickers #Stripe #stsci #Symmetry #Synaptic #Satellite #SuperTuxKart #ssh T #Tinkerers #Tailings #Talk #Teaching #TeachingAssistant #Techlearningcollective #Telescope #Terminal #Terms #TeX #TextEditor #TheOpenUniversity #Theory #TheOU #Thesis #Thunar #Thunderbird #Thymine #Tilde #Toot #Top #Topic #Torbay #TorbayTrojans #Transit #Translation #Trojans #Trunk #Tuxiversity U #Ulytsheavy #Umami #UN #UnitedKingdom #UnitedNations #UniverseOfLearning #Uracil #Use #users V #Vaccine #Virgo #VLC #VokoScreen #Volunteer #Volunteering #VultureNethack #vultureseye #vfsync W #Warming #wayland #weatherinfo #Website #WhiteVinegar #wicd #wireless #Wordpress #Work #WorldCup #WorldSpaceWeek #Wormhole #Write.as #Write freely #Writing X #Xchat #XenonLamp #XFCE #XFCE4 #XMPP #xorg #Xournal #xray Y #YearOfTheFediverse #YH4F #YouthHacking4Freedom #YouthHackingForFreedom Z #Zoo #ForeverChemicals #PFAS #TFA

NUMBERS

#0AD

Write your own compiler

Another post found on Mastodon. This was posted by Dr. Brian Callahan.

Links

Tags

#Write,#Own,#Compiler,#Programming,#Blog,#coding #code #program #programming #compiler #compile #compilers


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay