User Tools

Site Tools


haas:fall2019:unix:ipkahints

Differences

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

Link to this comparison view

haas:fall2019:unix:ipkahints [2019/04/25 12:12] (current)
Line 1: Line 1:
 +======IPKA HINTS======
  
 +=====bc=====
 +
 +The **bc**(**1**) tool is very useful for performing math on numbers. We've used it extensively on various projects throughout the semester.
 +
 +====Usage Example 1====
 +
 +Simplest usage, displaying a number:
 +
 +<cli>
 +$ echo "​12"​ | bc -q
 +12
 +</​cli>​
 +
 +====Usage Example 2====
 +
 +Using a variable:
 +
 +<cli>
 +$ value="​12"​
 +$ echo "​${value}"​ | bc -q
 +12
 +</​cli>​
 +
 +====Usage Example 3====
 +
 +Changing the base:
 +
 +<cli>
 +$ value="​12"​
 +$ echo "​obase=2;​ ${value}"​ | bc -q
 +1100
 +</​cli>​
 +
 +====Usage Example 4====
 +
 +Variable-izing the base:
 +
 +<cli>
 +$ base="​2"​
 +$ value="​12"​
 +$ echo "​obase=${base};​ ${value}"​ | bc -q
 +1100
 +</​cli>​
 +
 +=====file=====
 +
 +The **file**(**1**) utility is useful for identifying file types, especially when the files themselves may not be properly named.
 +
 +====example====
 +
 +<cli>
 +$ file /etc/motd
 +/etc/motd: ASCII text
 +</​cli>​
 +
 +=====grep=====
 +
 +The **grep**(**1**) utility can be used to search through text files for patterns, of the literal or Regular Expression variety, by default outputting lines matching the search pattern. It is quite useful in command-line pipelines, to filter the output of a previous command'​s output.
 +
 +
 +====example 1====
 +
 +Here, we use grep to filter the output of **file**, to display only the lines of output pertaining to Ruby scripts:
 +
 +<cli>
 +$ file /​usr/​local/​bin/​* | grep '​Ruby'​
 +/​usr/​local/​bin/​autospec: ​                Ruby script, ASCII text executable
 +/​usr/​local/​bin/​bundle: ​                  Ruby script, ASCII text executable
 +/​usr/​local/​bin/​bundler: ​                 Ruby script, ASCII text executable
 +/​usr/​local/​bin/​erubis: ​                  Ruby script, ASCII text executable
 +/​usr/​local/​bin/​httparty: ​                Ruby script, ASCII text executable
 +/​usr/​local/​bin/​rackup: ​                  Ruby script, ASCII text executable
 +/​usr/​local/​bin/​rake: ​                    Ruby script, ASCII text executable
 +/​usr/​local/​bin/​rspec: ​                   Ruby script, ASCII text executable
 +/​usr/​local/​bin/​tilt: ​                    Ruby script, ASCII text executable
 +</​cli>​
 +
 +====example 2====
 +
 +Counting the resulting output by line can result in a nice '​present'​ or 'not present'​ status (true, false):
 +
 +<cli>
 +$ file /​usr/​local/​bin/​wcp | grep '​Ruby'​ | wc -l
 +0
 +$ file /​usr/​local/​bin/​bundle | grep '​Ruby'​ | wc -l
 +1
 +</​cli>​
 +
 +This could provide a nice means of making a decision on whether or not to perform some processing on a given file.
 +
 +=====ls=====
 +
 +The **ls**(**1**) command can be used to list all files in a given directory. The **-1** (one) argument will force a listing of one entry per line. Providing a path to **ls** will cause it to list the files in that directory versus the default behavior of the current directory.
 +
 +Providing an absolute path to commands when you run them disables any shell aliases that may be in place.
 +
 +====For example====
 +
 +<code bash>
 +/bin/ls -1 ${DATAPATH}
 +</​code>​
 +
 +Will list all the files currently located in ${DATAPATH},​ one entry per line.
 +
 +=====Command Expansions=====
 +
 +Command expansions are a form of UNIX quotes that allow you to capture the output of any enclosed commands, very useful for capturing into a variable or providing an in-line list of information to things that can utilize them.
 +
 +====Example 1====
 +If you are in the Public Directory'​s ipka/files/ directory:
 +
 +<code bash>
 +cat $(/bin/ls -1 ${DATAPATH})
 +</​code>​
 +
 +Will concatenate to STDOUT the contents of all the files.
 +
 +====Example 2====
 +And we can capture that expanded result (the listing of all the files) into a variable:
 +
 +<code bash>
 +value=$(/​bin/​ls -1 ${DATAPATH})
 +</​code>​
 +=====list-based for loop=====
 +
 +The numeric for loop is one variety of for loop. Bash also provides another, the "​list-based"​ for loop (also known as a "for each" loop). While it can still be used to process numbers, it can also take in non-numeric data, processing each item of the list in turn.
 +
 +====syntax====
 +The syntax is:
 +
 +<code bash>
 +for value in LIST; do
 +    # commands in loop
 +done
 +</​code>​
 +
 +=====selection=====
 +The **<​nowiki>​[</​nowiki>​**(**1**) utility allows for the evaluation of a logical or relational condition, resulting in a true or false value. Combined with an '​**if**'​ statement, this can be used to conditionally execute code based on the results of that evaluation.
 +
 +<code bash>
 +if [ 17 -lt 43 ]; then
 +    MESSAGE="​LESS THAN"
 +else
 +    MESSAGE="​NOT LESS THAN"
 +fi
 +echo "​${MESSAGE}"​
 +</​code>​
haas/fall2019/unix/ipkahints.txt · Last modified: 2019/04/25 12:12 (external edit)