comparison src/learn_bash.html.luan @ 48:889e3c2d2699

learn_bash work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Jan 2024 02:34:44 -0700
parents 84dd3edd03e9
children 3057adc065f3
comparison
equal deleted inserted replaced
47:84dd3edd03e9 48:889e3c2d2699
653 653
654 <p><code>sleep 3</code> sleeps for 3 seconds, meaning it does nothing for 3 seconds. I waited 3 seconds for this command to finish. Then I ran <code>sleep 30</code> which would sleep for 30 seconds, but I lost my patience and pressed control+c which interrupts the program and breaks out of it. You can try control+c if you ever get stuck waiting for a command to finish.</p> 654 <p><code>sleep 3</code> sleeps for 3 seconds, meaning it does nothing for 3 seconds. I waited 3 seconds for this command to finish. Then I ran <code>sleep 30</code> which would sleep for 30 seconds, but I lost my patience and pressed control+c which interrupts the program and breaks out of it. You can try control+c if you ever get stuck waiting for a command to finish.</p>
655 <% 655 <%
656 end 656 end
657 } 657 }
658 find = {
659 title = [[The "find" Command]]
660 content = function()
661 %>
662 <code block>
663 ~/learn $ find .
664 .
665 ./file3
666 ./file2
667 ./file1
668 ./dir1
669 ./dir1/d1file
670 ~/learn $ find . -name 'file*'
671 ./file3
672 ./file2
673 ./file1
674 ~/learn $ find . -name '*file'
675 ./dir1/d1file
676 ~/learn $ find . -name 'd*'
677 ./dir1
678 ./dir1/d1file
679 ~/learn $ find . -name '*1' -or -name '*2'
680 ./file2
681 ./file1
682 ./dir1
683 </code>
684
685 <p><code>find</code> recursively searches for files in a directory tree. Note that in this case the <code>*</code> wildcard matching is not being done by Bash, it is being done by <code>find</code>. <code>find</code> has many options for searching for files and acting on them, see <code>man find</code>.</p>
686 <%
687 end
688 }
689 io = {
690 title = [[Input and Output]]
691 content = function()
692 %>
693 <code block>
694 ~/learn $ echo 'this is a test' >test.txt
695 ~/learn $ ls -F
696 dir1/ file1 file2 file3 test.txt
697 ~/learn $ cat test.txt
698 this is a test
699 ~/learn $ echo 'this is another test' >test.txt
700 ~/learn $ cat test.txt
701 this is another test
702 ~/learn $ echo 'another line' >>test.txt
703 ~/learn $ cat test.txt
704 this is another test
705 another line
706 ~/learn $ ls >ls.txt
707 ~/learn $ cat ls.txt
708 dir1
709 file1
710 file2
711 file3
712 ls.txt
713 test.txt
714 ~/learn $ ls -d f* q* >ls.txt
715 ls: q*: No such file or directory
716 ~/learn $ cat ls.txt
717 file1
718 file2
719 file3
720 ~/learn $ ls -d f* q* 2>ls.txt
721 file1 file2 file3
722 ~/learn $ cat ls.txt
723 ls: q*: No such file or directory
724 ~/learn $ ls -d f* q* | tee ls.txt
725 ls: q*: No such file or directory
726 file1
727 file2
728 file3
729 ~/learn $ cat ls.txt
730 file1
731 file2
732 file3
733 ~/learn $ ls -d f* q* 2>&1 | tee ls.txt
734 ls: q*: No such file or directory
735 file1
736 file2
737 file3
738 ~/learn $ cat ls.txt
739 ls: q*: No such file or directory
740 file1
741 file2
742 file3
743 </code>
744
745 <p>All programs have standard input, standard output, and standard error. Programs write normal output to standard output and error messages to standard error. By default, standard output and standard error go to the terminal, but this can be changed. <code>>file</code> sends standard output to <code>file</code>. <code>>>file</code> appends standard output to <code>file</code>. <code>2>file</code> sends standard error to <code>file</code>. <code>2>&1</code> sends standard error to standard output. <code>|</code> sends standard output of the previous command to standard input of the following command. We haven't used standard input before, but <code>tee file</code> reads standard input and then writes it to both standard output and to <code>file</code>. And for completeness, <code>&lt;file</code> reads standard input from <code>file</code> even though I haven't shown an example of this.</p>
746
747 <code block>
748 ~/learn $ find . -type f | wc -l
749 6
750 </code>
751
752 <p>There are 6 files in <code>learn</code>. Use <code>man</code> to figure out how this works.</p>
753 <%
754 end
755 }
756 subst = {
757 title = [[Command Substitution]]
758 content = function()
759 %>
760 <code block>
761 ~/learn $ echo I am in $(pwd)
762 I am in /Users/fschmidt/learn
763 ~/learn $ echo this directory contains: $(ls)
764 this directory contains: dir1 file1 file2 file3 ls.txt test.txt
765 ~/learn $ echo this directory contains $(ls | wc -l) files
766 this directory contains 6 files
767 </code>
768
769 <p><code>cmd $(commands)</code> will use the output of <code>commands</code> as argument text for <code>cmd</code>.</p>
770
771 <code block>
772 ~/learn $ cat $(find . -type f) | wc -c
773 86
774 </code>
775
776 <p>The files in <code>learn</code> contain a total of 86 bytes. Use <code>man</code> to figure out how this works.</p>
777 <%
778 end
779 }
658 later = { 780 later = {
659 title = [[placeholder]] 781 title = [[placeholder]]
660 content = function() 782 content = function()
661 %> 783 %>
662 <p>later</p> 784 <p>later</p>
702 <title>Reactionary Bash Tutorial</title> 824 <title>Reactionary Bash Tutorial</title>
703 </head> 825 </head>
704 <body> 826 <body>
705 <% header() %> 827 <% header() %>
706 <div content> 828 <div content>
707 <h1><a href="learn.html">Reactionary Bash Tutorial</a></h1> 829 <h1><a href="learn_bash.html">Reactionary Bash Tutorial</a></h1>
708 <hr> 830 <hr>
709 <h2>Contents</h2> 831 <h2>Contents</h2>
710 <div toc> 832 <div toc>
711 <% show_toc(content) %> 833 <% show_toc(content) %>
712 </div> 834 </div>