[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
howtos:general_admin:cli_constructs_and_useful_info [2012/10/21 08:37 (UTC)] – [Basic and Extended Regex] htexmexhhowtos:general_admin:cli_constructs_and_useful_info [2013/04/02 15:19 (UTC)] (current) – [Constructs] htexmexh
Line 9: Line 9:
 ls -1 /var/log/packages | rev | cut -d - -f 1-3 --complement | rev ls -1 /var/log/packages | rev | cut -d - -f 1-3 --complement | rev
 </code> </code>
-Or if you wanted to get the full patch of a file, minus the suffix.+Or if you wanted to get the full path of a file, minus the suffix.
 <code bash> <code bash>
 echo /proc/config.gz | rev | cut -d. -f1 --complement | rev echo /proc/config.gz | rev | cut -d. -f1 --complement | rev
Line 78: Line 78:
 </code> </code>
 Note that there are no quotes around ''$(seq)'' because otherwise it would quote the entire expanded number sequence and that wouldn't work right. Note that there are no quotes around ''$(seq)'' because otherwise it would quote the entire expanded number sequence and that wouldn't work right.
 +== External Links ===
 +  * http://www.commandlinefu.com/commands/browse
 ==== Quoting ==== ==== Quoting ====
 Quoting may seem complicated, and reasons for it obscure, but there is a purpose to it and it is not that complicated.  Quoting may seem complicated, and reasons for it obscure, but there is a purpose to it and it is not that complicated. 
Line 95: Line 97:
 bash-4.2$  bash-4.2$ 
 </code> </code>
-Clearly you need to quote a file with spaces. You could use single quotes here, because no variable were inside the quotes. You should not quote in this case:+Clearly you need to quote a file with spaces. You could use single quotes here, because no variables were inside the quotes. You should not quote in this case:
 <code bash> <code bash>
 bash-4.2$ for i in $(seq 1 10); do printf "$i "; done; echo;   bash-4.2$ for i in $(seq 1 10); do printf "$i "; done; echo;  
Line 144: Line 146:
 </code> </code>
 Here the shell expands ''*'' before find sees it. You should single quote input to ''awk'', ''find'', ''sed'', and ''grep'', as each of these uses special characters that overlap with the shells', and thus they must be protected from shell expansion. Here the shell expands ''*'' before find sees it. You should single quote input to ''awk'', ''find'', ''sed'', and ''grep'', as each of these uses special characters that overlap with the shells', and thus they must be protected from shell expansion.
-==== Basic and Extended Regex ====+=== External Links === 
 +  * http://www.grymoire.com/Unix/Quote.html 
 +==== Regular expressions ====
 === Basic === === Basic ===
   * ''.'' matches any single character.   * ''.'' matches any single character.
Line 163: Line 167:
     * ''[Yy]'' matches Y or y.     * ''[Yy]'' matches Y or y.
     * ''[a-z0-9]'' includes a range, and in this case matches a through z and 0 through 9.     * ''[a-z0-9]'' includes a range, and in this case matches a through z and 0 through 9.
-    * ''[^a-z]'' negates the ranges, so in this case it matches anything but a through z.+    * ''[^a-z]'' negates the range, so in this case it matches anything but a through z.
   * ''^'' matches the beginning of a line. Example: ''^a'' matches an a at the beginning of a line.   * ''^'' matches the beginning of a line. Example: ''^a'' matches an a at the beginning of a line.
   * ''$'' matches the end of a line. Example: ''a$'' matches an a at the end of a line.   * ''$'' matches the end of a line. Example: ''a$'' matches an a at the end of a line.
Line 170: Line 174:
     * Example: ''\<[tT]he\>'' matches the word ''the'' or ''The''.     * Example: ''\<[tT]he\>'' matches the word ''the'' or ''The''.
   * ''*'' matches any number of the previous character or nothing = no character. Example: ''[0-9]*'' which will match any number of numbers. ''.*'' matches any number of anything.   * ''*'' matches any number of the previous character or nothing = no character. Example: ''[0-9]*'' which will match any number of numbers. ''.*'' matches any number of anything.
-  * ''+'' matches any number of the previous character, like ''*'', but there must be at least one to match, so it will not match nothing or no character. +=== Extended regular expressions ===
-=== Extended ===+
 The following must be supported by the program for them to work. For example for grep you must run ''egrep'' or ''grep -E''. The following must be supported by the program for them to work. For example for grep you must run ''egrep'' or ''grep -E''.
 +  * ''+'' matches any number of the previous character, like ''*'', but there must be at least one to match, so it will not match nothing or no character.
 +  * ''?'' makes the previous character optional (it can be missing), and is matched at most once.
   * ''(|)'' acts like an OR statement. Example: ''(it|her|this)'' matches any of those words.   * ''(|)'' acts like an OR statement. Example: ''(it|her|this)'' matches any of those words.
   * ''a{3}'' matches ''aaa'' = 3 a's.   * ''a{3}'' matches ''aaa'' = 3 a's.
Line 178: Line 183:
   * ''{0,}'' = ''*''   * ''{0,}'' = ''*''
   * ''{1,}'' = ''+''   * ''{1,}'' = ''+''
 +  * ''{,1}'' = ''?''
 +=== External Links ===
 +  * http://www.grymoire.com/Unix/Regular.html
 +  * http://www.regular-expressions.info/
 +==== Useful commands and info ====
 +=== stat ===
 +Stat is the most accurate way to determine:
 +  * File size in bytes: <code bash>stat -c '%s' file.txt</code>
 +  * File permissions in octal: <code bash>stat -c '%a' file.txt</code>
 +=== awk variable defaults ===
 +An important point is that awk variables are set to zero by default. This may cause problems in some situtations. Example:
 +<code bash>
 +echo -ne '-321\n-14\n-1\n-34\n-4\n' | awk 'BEGIN{max=""}{if ($1 > max) max=$1; if ($1 < min) min=$1;}END{print min"\t"max}'
 +</code>
 +This works properly because max is set to an empty string and thus has a lower value than any number. Try removing the BEGIN clause and see what happens. Also note that adding ''min=""'' to the BEGIN clause fails as well.
 +=== no data directory test ===
 +You can use this to test if a directory contains no data. For example, it will say ''0'' if the directory only contains empty files and directories = no data.
 +<code bash>
 +du -s directory
 +</code>
 +=== cmp ===
 +This can compare two files byte by byte, and can be more useful than checksums. For example, after you burn a CD/DVD, you can run:
 +<code bash>
 +cmp slackware.iso /dev/sr0
 +</code>
 +It should say the following if the disk burned correctly:
 +<code bash>
 +cmp: EOF on slackware.iso
 +</code>
 +=== shell math ===
 +Remember that shell utilities like ''let'' and ''expr'' only do integer math. For floating point use either ''bc'' or ''awk''.
 +=== shell GUI ===
 +There are numerous programs that allow you to create GUIs from a shell script.
 +  * [[http://xdialog.free.fr/|Xdialog is fully dialog compatible.]]
 +  * [[https://sites.google.com/site/easybashgui/home|A function library that increases shell GUI portability.]]
 +  * [[http://code.google.com/p/gtkdialog/|Gtkdialog has advanced features for customized GUIs.]]
 +=== External Links ===
 +  * [[http://www.grymoire.com/Unix/|Guide to awk, bash, sed, find, and more]]
 +  * [[http://www.tldp.org/LDP/abs/html/index.html|Advanced bash guide]]
 +  * [[http://www.catonmat.net/projects/cheat-sheets/|Cheat sheets on Linux CLI programs, the main site is also useful]]
 +  * [[http://www.zzee.com/solutions/linux-permissions.shtml|Guide to file permissions]]
 ====== Sources ====== ====== Sources ======
 <!-- If you are copying information from another source, then specify that source --> <!-- If you are copying information from another source, then specify that source -->
Line 184: Line 230:
 <!-- * Originally written by [[wiki:user:xxx | User X]] --> <!-- * Originally written by [[wiki:user:xxx | User X]] -->
 <!-- * Contributions by [[wiki:user:yyy | User Y]] --> <!-- * Contributions by [[wiki:user:yyy | User Y]] -->
 +  * I quoted ''man comm'' 
 +  * I used ''man grep'' for the regex section. 
 +  * Written by [[wiki:user:htexmexh|H_TeXMeX_H]]
 <!-- Please do not modify anything below, except adding new tags.--> <!-- Please do not modify anything below, except adding new tags.-->
 <!-- You must remove the tag-word "template" below before saving your new page --> <!-- You must remove the tag-word "template" below before saving your new page -->
-{{tag>howtos software author_htexmexh work_in_progress}}+{{tag>howtos software author_htexmexh}}
 howtos:general_admin:cli_constructs_and_useful_info ()