{"id":508,"date":"2020-11-28T17:36:10","date_gmt":"2020-11-28T17:36:10","guid":{"rendered":"https:\/\/blog.thomarite.uk\/?p=508"},"modified":"2020-11-28T17:36:29","modified_gmt":"2020-11-28T17:36:29","slug":"bash-cookbook-p1","status":"publish","type":"post","link":"https:\/\/blog.thomarite.uk\/index.php\/2020\/11\/28\/bash-cookbook-p1\/","title":{"rendered":"Bash-Cookbook-P1"},"content":{"rendered":"\n<p>I have reading this <a href=\"https:\/\/smile.amazon.co.uk\/bash-Cookbook-2e-Carl-Albing\/dp\/1491975334\/\">book<\/a> (1st edit &#8211; quite old) in the last months after breakfast. So I am taking some notes. I think they are things useful and I should use them. So try to write, understand, remember and use.<\/p>\n\n\n\n<p>I am using bash 5.1.0(1)-rc3<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ bash --version\nGNU bash, version 5.1.0(1)-rc3 (x86_64-pc-linux-gnu)<\/pre>\n\n\n\n<p><strong>1- Quoting<\/strong><\/p>\n\n\n\n<p><strong>Shell quoting<\/strong>: Enclose  a string in single quotes unless it contains elements that you want the shell to interpolate<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\n$ echo John has $100 note?!\nJohn has 00 note?!\n$ echo \"John has $100 note?!\"\nJohn has 00 note?!\n$ echo 'John has $100 note?!'\nJohn has $100 note?!<\/pre>\n\n\n\n<p>You can&#8217;t embed a single quote inside single quotes even with a backslash. Nothing is interpolated inside single quotes. Workaround is using double quotes with escapes or, escaping a single quote outside of surrounding single quotes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo 'John doesn't have $100 notes'\n^C\n$\n$ echo \"John doesn't have $100 notes\"\nJohn doesn't have 00 notes\n$\n$ echo \"John doesn't have \\$100 notes\"\nJohn doesn't have $100 notes\n$\n$ echo 'John doesn'\\''t have $100 notes'\nJohn doesn't have $100 notes\n$<\/pre>\n\n\n\n<p><strong>2- Standard Output<\/strong>\/<strong>Input<\/strong><\/p>\n\n\n\n<p><strong>Redirect output from &#8220;ls&#8221;<\/strong>: It can be confusing when redirecting output from &#8220;ls&#8221; to a file and then read it as you dont see  the expected format. Using &#8220;-C&#8221; you ensure the redirection will be based on &#8220;Colummns&#8221;. By default (-1), the output will be based on lines.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls -ltr\ntotal 92\n-rw-r--r-- 1 tomas tomas 72533 Jul 27 2016 jabber.py\n-rw-r--r-- 1 tomas tomas 16087 Jul 27 2016 anotify.py\ndrwxr-xr-x 2 tomas tomas 4096 Dec 10 2017 autoload\n$ ls\nanotify.py autoload jabber.py\n$ ls > \/tmp\/test.txt\n$ cat \/tmp\/test.txt    --> You would expect just one line like \"ls\"????\nanotify.py\nautoload\njabber.py\n$\n$ ls -C > \/tmp\/test2.txt\n$ cat \/tmp\/test2.txt   --> Yes, this is the same output of standard \"ls\"\nanotify.py autoload jabber.py\n$ ls -C\nanotify.py autoload jabber.py\n$ ls -1\nanotify.py\nautoload\njabber.py\n$<\/pre>\n\n\n\n<p>Redirect output and error to different files:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls -ltr \/boot\/* <strong>><\/strong> \/tmp\/test-out.txt <strong>2><\/strong> \/tmp\/test-err.txt\n$\n$ cat \/tmp\/test-out.txt\n...\n-rw-r--r-- 1 root root 73210069 Nov 28 11:10 \/boot\/initrd.img-5.9.0-1-amd64\n-rw-r--r-- 1 root root 234724 Nov 28 11:10 \/boot\/config-5.9.0-1-amd64\n\/boot\/grub:\ntotal 2379\n...\n-rw-r--r-- 1 root root 2394102 Nov 14 18:12 unicode.pf2\n-r--r--r-- 1 root root 8362 Nov 28 10:53 grub.cfg\n$\n$ cat \/tmp\/test-err.txt\nls: cannot open directory '\/boot\/efi': Permission denied\nls: cannot open directory '\/boot\/lost+found': Permission denied\n$<\/pre>\n\n\n\n<p>Redirect output and error to same file, use &#8220;<strong>>&amp;<\/strong>&#8220;. Other option is &#8220;$ program > outfile <strong>2>&amp;1<\/strong>&#8220;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls -ltr \/boot\/* <strong>>&amp;<\/strong> \/tmp\/test-both.txt\n$\n$ cat \/tmp\/test-both.txt\n...\n-rw-r--r-- 1 root root 73210069 Nov 28 11:10 \/boot\/initrd.img-5.9.0-1-amd64\n-rw-r--r-- 1 root root 234724 Nov 28 11:10 \/boot\/config-5.9.0-1-amd64\nls: cannot open directory '\/boot\/efi': Permission denied\nls: cannot open directory '\/boot\/lost+found': Permission denied\n\/boot\/grub:\ntotal 2379\n...\n-rw-r--r-- 1 root root 2394102 Nov 14 18:12 unicode.pf2\n-r--r--r-- 1 root root 8362 Nov 28 10:53 grub.cfg\n$\n$ ls -ltr \/boot\/* > \/tmp\/test3.txt\nls: cannot open directory '\/boot\/efi': Permission denied\nls: cannot open directory '\/boot\/lost+found': Permission denied\n$<\/pre>\n\n\n\n<p>Grouping output from several commands: Use <strong>()<\/strong> for grouping them<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp\/aaa\/bbb$ (ls -ltr; pwd; cd ..; ls -ltr; pwd) > \/tmp\/all.txt\n:\/tmp\/aaa\/bbb$\n:\/tmp\/aaa\/bbb$ cat \/tmp\/all.txt\ntotal 0\n-rw-r--r-- 1 tomas tomas 0 Nov 28 16:28 b.txt\n\/tmp\/aaa\/bbb\ntotal 4\ndrwxr-xr-x 2 tomas tomas 4096 Nov 28 16:28 bbb\n-rw-r--r-- 1 tomas tomas 0 Nov 28 16:28 aaa.txt\n\/tmp\/aaa\n:\/tmp\/aaa\/bbb$<\/pre>\n\n\n\n<p>&#8220;<strong>tee<\/strong>&#8220;: read from standard input and write to standard output and files:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp\/bbb$ ls -ltr\ntotal 32\n-rw-r--r-- 1 tomas tomas 30 Nov 28 15:29 test.txt\n-rw-r--r-- 1 tomas tomas 32 Nov 28 15:30 test2.txt\n-rw-r--r-- 1 tomas tomas 58 Nov 28 15:38 error.txt\n-rw-r--r-- 1 tomas tomas 935 Nov 28 15:57 test-out.txt\n-rw-r--r-- 1 tomas tomas 121 Nov 28 15:57 test-err.txt\n-rw-r--r-- 1 tomas tomas 1056 Nov 28 15:59 test-both.txt\n-rw-r--r-- 1 tomas tomas 935 Nov 28 16:00 test3.txt\n-rw-r--r-- 1 tomas tomas 1 Nov 28 16:47 all.txt\n-rw-r--r-- 1 tomas tomas 0 Nov 28 16:52 a.a\n-rw-r--r-- 1 tomas tomas 0 Nov 28 16:52 a.aa\n-rw-r--r-- 1 tomas tomas 0 Nov 28 16:52 a.aaa\n:\/tmp\/bbb$\n:\/tmp\/bbb$\n:\/tmp\/bbb$ find . -name '*.txt'\n.\/all.txt\n.\/test.txt\n.\/test3.txt\n.\/test2.txt\n.\/test-out.txt\n.\/error.txt\n.\/test-both.txt\n.\/test-err.txt\n:\/tmp\/bbb$\n<em>:\/tmp\/bbb$ find . -name '<\/em>.txt' | tee \/tmp\/tee.txt\n.\/all.txt\n.\/test.txt\n.\/test3.txt\n.\/test2.txt\n.\/test-out.txt\n.\/error.txt\n.\/test-both.txt\n.\/test-err.txt\n:\/tmp\/bbb$\n:\/tmp\/bbb$ cat \/tmp\/tee.txt\n.\/all.txt\n.\/test.txt\n.\/test3.txt\n.\/test2.txt\n.\/test-out.txt\n.\/error.txt\n.\/test-both.txt\n.\/test-err.txt\n:\/tmp\/bbb$<\/pre>\n\n\n\n<p>Here-Doc: use <strong>\\<\/strong>EOF to turn off shell scripting features inside here-doc.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp\/aaa$ bash here.txt bb\nbb $2\n:\/tmp\/aaa$ bash here.txt aa\naa $1\n:\/tmp\/aaa$\n:\/tmp\/aaa$ cat here.txt\nhere-doc example\ngrep $1 &lt;&lt;\\<strong>EOF<\/strong>\nname note\naa $1\nbb $2\ncc $3\nEOF\n:\/tmp\/aaa$<\/pre>\n\n\n\n<p>&#8220;$?&#8221;return non-zero if the last command fails.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp$ pwd\n\/tmp\n:\/tmp$ echo $?\n0<\/pre>\n\n\n\n<p> &#8220;&amp;&amp;&#8221; run next program if the preceding program worked (logic <strong>and<\/strong>)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp$ .\/test &amp;&amp; date\nbash: .\/test: No such file or directory\n:\/tmp$\n:\/tmp$ pwd &amp;&amp; date\n\/tmp\nSat 28 Nov 17:20:50 GMT 2020\n:\/tmp$<\/pre>\n\n\n\n<p>&#8220;||&#8221; (logic <em><strong>or<\/strong><\/em>) <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp\/aaa$ .\/test || ( printf \"%b\" \"Failed.\\n\")\nbash: .\/test: No such file or directory\nFailed.\n:\/tmp\/aaa$<\/pre>\n\n\n\n<p><strong>nohup<\/strong>: run job in background and exit shell before finishing job.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ nohup .\/long-script &amp;<\/pre>\n\n\n\n<p>for loop:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:\/tmp\/aaa$ for FILE in bbb\/*\ndo\nif [ -f $FILE ]\nthen\ncat $FILE\nfi\ndone\na file\nb file\nc file\n:\/tmp\/aaa$ ls -ltr bbb\/\ntotal 12\n-rw-r--r-- 1 tomas tomas 7 Nov 28 17:30 a.txt\n-rw-r--r-- 1 tomas tomas 7 Nov 28 17:30 b.txt\n-rw-r--r-- 1 tomas tomas 7 Nov 28 17:31 c.txt<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have reading this book (1st edit &#8211; quite old) in the last months after breakfast. So I am taking some notes. I think they are things useful and I should use them. So try to write, understand, remember and use. I am using bash 5.1.0(1)-rc3 $ bash &#8211;version GNU bash, version 5.1.0(1)-rc3 (x86_64-pc-linux-gnu) 1- &hellip; <a href=\"https:\/\/blog.thomarite.uk\/index.php\/2020\/11\/28\/bash-cookbook-p1\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash-Cookbook-P1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-unix"],"_links":{"self":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":1,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}