Advertisement
v1ral_ITS

more of my good notes

Sep 4th, 2018
1,730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 45.81 KB | None | 0 0
  1.      1  # /usr/bin/zsh
  2.      2 
  3.      3  Sun Sep  2 03:25:16 EDT 2018
  4.      4  NOTE:
  5.      5 
  6.      6  #!/bin/bash
  7.      7  ####################################
  8.      8  #                      #
  9.      9  # START                #
  10.     10  # g0ne_@p0stal                     #
  11.     11  # ImPerialTeKSolutions@outlook.com #
  12.     12  #                  #
  13.     13  ####################################
  14.     14 
  15.     15  #+ ITS_ URL Video DL: batch version 1.0
  16.     16  #* Batch list part of program
  17.     17 
  18.     18  # Dimensions
  19.     19  height=200
  20.     20  width=500
  21.     21 
  22.     22  # Numerical batch list's
  23.     23  yad --height=$height --width=$width --title "Numerical Answer batch#.list" --entry > /tmp/batch#.$$
  24.     24  _number=`cat /tmp/batch#.$$`
  25.     25 
  26.     26  # Batch list
  27.     27  batch_list=batch$_number.list
  28.     28 
  29.     29  # Main CMD Variables
  30.     30  cmd_1=$(zenity --width=$width --height=$height --title "Add URL's To Batch List" --text "v1ral_ITS Download, batch verson" --forms --add-entry "URL:" --forms --add-entry "URL:" --add-entry "URL:" --add-entry "URL:" --add-entry "URL:"  --add-entry "URL:"  --add-entry "URL:"  --add-entry "URL:"  --separator='|')
  31.     31 
  32.     32  # Start Main Dialog Program
  33.     33  #+ ITS_
  34.     34  echo "$cmd_1" | sed -e "s/|/\n/g" \
  35.     35  | tee -a /home/v1ral/$batch_list
  36.     36 
  37.     37  # Finish
  38.     38  /home/v1ral/bin/ITS &&  sign
  39.     39 
  40.     40  # Clean and exit
  41.     41  exit $?
  42.     42 
  43.     43  _________________________________________________
  44.     44 
  45.     45 
  46.     46  #!/bin/bash
  47.     47  ####################################
  48.     48  #                      #
  49.     49  # START                #
  50.     50  # g0ne_@p0stal                     #
  51.     51  # ImPerialTeKSolutions@outlook.com #
  52.     52  #                  #
  53.     53  ####################################
  54.     54 
  55.     55  #+ ITS_ URL Video DL: batch version 1.0
  56.     56  #* Batch download part of program
  57.     57 
  58.     58  needed_tool='youtube-dl yad kdialog ffmpeg'
  59.     59  require_tools ()
  60.     60  {
  61.     61      local NOT_AVAIL=""
  62.     62      for TOOL in $needed_tool; do
  63.     63          if [ "`which $TOOL 2> /dev/null`" == "" ]; then NOT_AVAIL="$NOT_AVAIL $TOOL"; fi
  64.     64      done
  65.     65      if [[ "$NOT_AVAIL" != "" ]]; then
  66.     66          echo "ERROR: The following required tool(s) cannot be found: $NOT_AVAIL"
  67.     67          exit 3
  68.     68      fi
  69.     69  }
  70.     70 
  71.     71  # Check If Package Downloaded
  72.     72  require_tools
  73.     73 
  74.     74  # Dimensions
  75.     75  height=200
  76.     76  width=500
  77.     77 
  78.     78  # Numerical batch list's
  79.     79  yad --height=$height --width=$width --title "Numerical Answer batch#.list" --entry > /tmp/batch#.$$
  80.     80  _number=`cat /tmp/batch#.$$`
  81.     81 
  82.     82  # Download Of Files Location
  83.     83  cd "$(yad --title "Change Directory To Download Destination" --file-selection --directory)"
  84.     84 
  85.     85  # Batch list
  86.     86  batch_list=batch$_number.list
  87.     87 
  88.     88  # Start Program Here
  89.     89  time youtube-dl --ignore-config --hls-prefer-native --add-metadata -i -c --yes-playlist --external-downloader aria2c -a $HOME/$batch_list  | pv -tpeba -s 100M
  90.     90  ding
  91.     91 
  92.     92  # Cleanup
  93.     93  rm $HOME/$batch_list
  94.     94 
  95.     95  # Exit
  96.     96  exit $?
  97.     97   
  98.     98   
  99.     99  ---------------------------------------------
  100.    100 
  101.    101 
  102.    102  Sun Sep  2 06:38:02 EDT 2018
  103.    103  NOTE:
  104.    104 
  105.    105  LINUX ESSENTIALS
  106.    106  [ Workshops ]       [ R_&_BioC ]        [ BioC-Seq ]        [ R_Programming ]       [ EMBOSS ]      [ Linux ]       [ Cluster ]
  107.    107  Author: Thomas Girke, UC Riverside
  108.    108  New version of this manual
  109.    109 
  110.    110  Index
  111.    111  INTRODUCTION
  112.    112  BASICS
  113.    113  UNIX HELP
  114.    114  FINDING THINGS
  115.    115  PERMISSIONS & OWNERSHIP
  116.    116  USEFUL COMMANDS
  117.    117  JOB/PROCESS MANAGEMENT
  118.    118  TEXT VIEWING
  119.    119  TEXT EDITORS
  120.    120  THE UNIX SHELL
  121.    121  SIMPLE SHELL ONE-LINER SCRIPTS
  122.    122  SIMPLE PERL ONE-LINER SCRIPTS
  123.    123  REMOTE COPY
  124.    124  ARCHIVING AND COMPRESSING
  125.    125  SIMPLE INSTALLS
  126.    126  DEVICES
  127.    127  ENVIRONMENT VARIABLES
  128.    128  EXERCISES
  129.    129  INTRODUCTION
  130.    130  Why UNIX?
  131.    131  Multitasking
  132.    132  Remote tasking ("real networking")
  133.    133  Multiuser
  134.    134  Access to shell, programming languages, databases, open-source projects
  135.    135  Better performance, less expensive (free), more up-to-date
  136.    136  Many more reasons
  137.    137  How to get access
  138.    138  Installation on local machine (not required!!!)
  139.    139  Get account on bioinfo.ucr.edu server:
  140.    140  email tgirke@citrus.ucr.edu
  141.    141  requirements for Windows and Mac OS X: http://faculty.ucr.edu/~tgirke/GCG.htm#General
  142.    142  UNIX variants
  143.    143  UNIX: Solaris, IRIX, HP-UX, Tru64-UNIX, Free's, LINUX, ...
  144.    144  LINUX distributions
  145.    145  RedHat, Debian, Mandrake, Caldera, Slackware, SuSE, ...
  146.    146 
  147.    147  BASICS
  148.    148  Syntax for this manual
  149.    149  Remember the UNIX/LINUX command line is case sensitive!
  150.    150  "$" indicates start of command
  151.    151  "#" indicates end of command and start of comment
  152.    152  The text in green or red monospace font represents the actual command. The "$" and "#" symbols are not part of it. The commands in red emphasize essential information for beginners.
  153.    153  "<...>" or "my_..." refers to variables and file names that need to be specified by the user. The arrows "<...>" need to be excluded, because they are generic UNIX redirection functions!
  154.    154  Login from Windows:
  155.    155  open PuTTY and select ssh
  156.    156  provide host name (IP) and session name
  157.    157 
  158.    158  $ user name: ...
  159.    159  $ password: ...
  160.    160  Setup for graphics emulation
  161.    161  WinSCP for file exchange
  162.    162  Login from Mac OS-X or LINUX
  163.    163  open terminal and type:
  164.    164 
  165.    165  $ ssh <user_name>@<host name(IP)>
  166.    166  $ user name: ...
  167.    167  $ password: ...
  168.    168  Changing password:
  169.    169  $ passwd # follow instructions
  170.    170  Orientation
  171.    171  $ pwd # present working directory
  172.    172  $ ls # content of pwd
  173.    173  $ ll # similar as ls, but provides additional info on files and directories
  174.    174  $ ll -a # includes hidden files (.name) as well
  175.    175  $ ll -R # lists subdirectories recursively
  176.    176  $ ll -t # lists files in chronological order
  177.    177  $ stat <file_name> # provides all attributes of a file
  178.    178  $ whoami # shows as who you are logged in
  179.    179  $ hostname # shows on which machine you are
  180.    180  Files and directories
  181.    181  $ mkdir <dir_name> # creates specified directory
  182.    182  $ cd <dir_name> # switches into specified directory
  183.    183  $ cd .. # moves one directory up
  184.    184  $ cd ../../ # moves two directories up (and so on)
  185.    185  $ cd # brings you to highest level of your home directory
  186.    186  $ rmdir <dir_name> # removes empty directory
  187.    187  $ rm <file_name> # removes file name
  188.    188  $ rm -r <dir_name> # removes directory including its content, but asks for confirmation, 'f' argument turns confirmation off
  189.    189  $ mv <name1> <name2> # renames directories or files
  190.    190  $ mv <name> <path> # moves file/directory as specified in path
  191.    191  $ cp <name> <path> # copy file/directory as specified in path (-r to include content in directories)
  192.    192  Copy and paste
  193.    193  Depends on local environment. Usually one of the following methods works:
  194.    194 
  195.    195  Copy: Ctrl&Shift&c or right/middle mouse click
  196.    196  Paste: Ctrl&Shift&p or right/middle mouse click
  197.    197  Handy shortcuts
  198.    198  $ . # refers to pwd
  199.    199  $ ~/ # refers to user's home directory
  200.    200  $ history # shows all commands you have used recently
  201.    201  $ !<number> # starts an old command by providing its ID number
  202.    202  $ up(down)_key # scrolls through command history
  203.    203  $ <incomplete path/file_name> TAB # completes path/file_name
  204.    204  $ <incomplete command> SHIFT&TAB # completes command
  205.    205  $ Ctrl a # cursor to beginning of command line
  206.    206  $ Ctrl e # cursor to end of command line
  207.    207  $ Ctrl d # delete character under cursor
  208.    208  $ Ctrl k # delete line from cursor, content goes into kill buffer
  209.    209  $ Ctrl y # paste content from Ctrl k
  210.    210 
  211.    211  UNIX HELP
  212.    212  $ man # general help
  213.    213  $ man wc # manual on program 'word count' wc
  214.    214  $ wc --help # short help on wc
  215.    215  $ info wc # more detailed information system (GNU)
  216.    216  $ apropos wc # retrieves pages where wc appears
  217.    217  Online help: SuperMan Pages, Linux Documentation Project (LDP)
  218.    218 
  219.    219  FINDING THINGS
  220.    220  Finding files, directories and applications
  221.    221  $ find -name "*pattern*" # searches for *pattern* in and below current directory
  222.    222  $ find /usr/local -name "*blast*" # finds file names *blast* in specfied directory
  223.    223  $ find /usr/local -iname "*blast*" # same as above, but case insensitive
  224.    224  additional useful arguments: -user <user name>, -group <group name>, -ctime <number of days ago changed>
  225.    225  $ find ~ -type f -mtime -2 # finds all files you have modified in the last two days
  226.    226  $ locate <pattern> # finds files and dirs that are written into update file
  227.    227  $ which <application_name> # location of application
  228.    228  $ whereis <application_name> # searches for executeables in set of directories
  229.    229  $ dpkg -l | grep mypattern # find Debian packages and refine search with grep pattern
  230.    230  Finding things in files
  231.    231  $ grep pattern file # provides lines in 'file' where pattern 'appears', if pattern is shell function use single-quotes: '>'
  232.    232  $ grep -H pattern # -H prints out file name in front of pattern
  233.    233  $ grep 'pattern' file | wc # pipes lines with pattern into word count wc (see chapter 8); wc arguments: -c: show only bytes, -w: show only words, -l: show only lines; help on regular expressions: $ man 7 regex or man perlre
  234.    234  $ find /home/my_dir -name '*.txt' | xargs grep -c ^.* # counts line numbers on many files and records each count along with individual file name; find and xargs are used to circumvent the Linux wildcard limit to apply this function on thousands of files.
  235.    235 
  236.    236  PERMISSIONS & OWNERSHIP
  237.    237  How does it work
  238.    238  $ ls -al # shows something like this for each file/dir: drwxrwxrwx
  239.    239  d: directory
  240.    240  rwx: read write execute
  241.    241  first triplet: user permissions (u)
  242.    242  second triplet: group permissions (g)
  243.    243  third triplet: world permissions (o)
  244.    244  To assign write and execute permissions to user and group:
  245.    245  $ chmod ug+rx my_file
  246.    246  To remove all permissions from all three user groups:
  247.    247  $ chmod ugo-rwx my_file
  248.    248  '+' causes the permissions selected to be added
  249.    249  '-' causes them to be removed
  250.    250  '=' causes them to be the only permissions that the file has.
  251.    251  Example for number system:
  252.    252  $ chmod +rx public_html/ or $ chmod 755 public_html/
  253.    253  Change ownership
  254.    254  $ chown <user> <file or dir> # changes user ownership
  255.    255  $ chgrp <group> <file or dir> # changes group ownership
  256.    256  $ chown <user>:<group> <file or dir> # changes user & group ownership
  257.    257 
  258.    258  USEFUL UNIX COMMANDS
  259.    259  $ df # disk space
  260.    260  $ free # memory info
  261.    261  $ uname -a # shows tech info about machine
  262.    262  $ bc # command-line calculator (to exit type 'quit')
  263.    263  $ wget ftp://ftp.ncbi.nih.... # file download from web
  264.    264  $ /sbin/ifconfig # give IP and other network info
  265.    265  $ ln -s original_filename new_filename # creates symbolic link to file or directory
  266.    266  $ du -sh # displays disk space usage of current directory
  267.    267  $ du -sh * # displays disk space usage of individual files/directories
  268.    268  $ du -s * | sort -nr # shows disk space used by different directories/files sorted by size
  269.    269 
  270.    270  JOB/PROCESS MANAGEMENT
  271.    271  $ who # shows who is logged into system
  272.    272  $ w # shows which users are logged into system and what they are doing
  273.    273  $ ps # shows processes running by user
  274.    274  $ ps -e # shows all processes on system; try also '-a' and '-x' arguments
  275.    275  $ ps aux | grep <user_name> # shows all processes of one user
  276.    276  $ top # view top consumers of memory and CPU
  277.    277  $ mtop # displays multicomputer/CPU processes
  278.    278  $ Ctrl z <enter> bg or fg <enter> # suspends a process to bring into back- or foreground
  279.    279  $ Ctrl c # stops an initiated process
  280.    280  $ kill <ps_ID> # Kills specified job; if this doesn't do it, add -9 as argument. Also, type <%1> then <CTRL C>.
  281.    281  $ renice -n <priority_value> # change priority value, which range from 1-19, the higher the value the lower the priority, default is 10
  282.    282 
  283.    283  TEXT VIEWING
  284.    284  $ less <my_file> # more versatile text viewer than 'more', 'G' moves to end of text, 'g' to beginning, '/' find forward, '?' find backwards
  285.    285  $ more <my_file> # views text, use space bar to browse, hit 'q' to exit
  286.    286  $ cat <my_file> # concatenates files and prints content to standard output
  287.    287 
  288.    288  TEXT EDITORS
  289.    289  VI and VIM
  290.    290  Non-graphical (terminal-based) editor. Vi is guaranteed to be available on any system. Vim is the improved version of vi.
  291.    291  EMACS
  292.    292  Window-based editor. You still need to know keystroke commands to use it. Installed on all Linux distributions and on most other Unix systems.
  293.    293  XEMACS
  294.    294  More sophisticated version of emacs, but usually not installed by default. All common commands are available from menus. Very powerful editor, with built-in syntax checking, Web-browsing, news-reading, manual-page browsing, etc.
  295.    295  PICO
  296.    296  Simple terminal-based editor available on most versions of Unix. Uses keystroke commands, but they are listed in logical fashion at bottom of screen.
  297.    297  VIM MANUAL (essentials marked in red)
  298.    298  BASICS
  299.    299  $ vim my_file_name # open/create file with vim
  300.    300  $ i # INSERT MODE
  301.    301  $ ESC # NORMAL (NON-EDITING) MODE
  302.    302  $ : # commands start with ':'
  303.    303  $ :w # save command; if you are in editing mode you have to hit ESC first!!
  304.    304  $ :q # quit file, don't save
  305.    305  $ :q! # exits WITHOUT saving any changes you have made
  306.    306  $ :wq # save and quit
  307.    307  $ R # replace MODE
  308.    308  $ r # replace only one character under cursor
  309.    309  $ q: # history of commands (from NORMAL MODE!), to reexecute one of them, select and hit enter!
  310.    310  $ :w new_filename # saves into new file
  311.    311  $ :#,#w new_filename # saves specific lines (#,#) to new file
  312.    312  $ :# go to specified line number
  313.    313  HELP
  314.    314  $ Useful list of vim commands: Vim Commands Cheat Sheet, VimCard, Vim Basics
  315.    315  $ vimtutor # open vim tutorial from shell
  316.    316  $ :help # opens help within vim, hit :q to get back to your file
  317.    317  $ :help <topic> # opens help on specified topic
  318.    318  $ |help_topic| CTRL-] # when you are in help this command opens help topic specified between |...|, CTRL-t brings you back to last topic
  319.    319  $ :help <topic> CTRL-D # gives list of help topics that contain key word
  320.    320  $ : <up-down keys> # like in shell you get recent commands!!!!
  321.    321  MOVING AROUND IN FILE
  322.    322  $ $ # moves cursor to end of line
  323.    323  $ A # same as $, but switches to insert mode
  324.    324  $ 0 (zero) # moves cursor to beginning of line
  325.    325  $ CTRL-g # shows at status line filename and the line you are on
  326.    326  $ SHIFT-G # brings you to bottom of file, type line number (isn't displayed) then SHIFT-G # brings you to specified line#
  327.    327  DISPLAY
  328.    328  WRAPPING AND LINE NUMBERS
  329.    329  $ :set nowrap # no word wrapping, :set wrap # back to wrapping
  330.    330  $ :set number # shows line numbers, :set nonumber # back to no-number mode
  331.    331  WORKING WITH MANY FILES & SPLITTING WINDOWS
  332.    332  $ vim *.txt # opens many files at once; ':n' switches between files
  333.    333  $ :wall or :qall # write or quit all open files
  334.    334  $ vim -o *.txt # opens many files at once and displays them with horizontal split, '-O' does vertical split
  335.    335  $ :args *.txt # places all the relevant files in the argument list $ :all # splits all files in the argument list (buffer) horizontally $ CTRL-w # switch between windows
  336.    336  $ :split # shows same file in two windows
  337.    337  $ :split <file-to-open> # opens second file in new window
  338.    338  $ :vsplit # splits windows vertically, very useful for tables, ":set scrollbind" let's you scroll all open windows symultaneously
  339.    339  $ :close # closes current window
  340.    340  $ :only # closes all windows except current one
  341.    341  SPELL CHECKING & Dictionary
  342.    342  $ aspell -c <file> # shell command
  343.    343  $ aspell -l <my_file> # shell command
  344.    344  $ :! dict <word> # meaning of word
  345.    345  $ :! wn 'word' -over # synonyms of word
  346.    346  PRINTING FILE
  347.    347  $ :ha # prints entire file
  348.    348  $ :#,#ha # prints specified lines: #,#
  349.    349  MERGING/INSERTING FILES
  350.    350  $ :r <filename> # inserts content of specified file after cursor
  351.    351  UNDO/REDO
  352.    352  $ u # undo last command
  353.    353  $ U # undo all changes on current line
  354.    354  $ CTRL-R # redo one change which was undone
  355.    355  DELETION/CUT (switch to NORMAL mode)
  356.    356  $ x # deletes what is under cursor
  357.    357  $ dw # deletes from curser to end of word including the space
  358.    358  $ de # deletes from curser to end of word NOT including the space
  359.    359  $ cw # deletes rest of word and lets you then insert, hit ESC to continue with NORMAL mode
  360.    360  $ c$ # deletes rest of line and lets you then insert, hit ESC to continue with with NORMAL mode
  361.    361  $ d$ # deletes from cursor to the end of the line
  362.    362  $ dd # deletes entire line
  363.    363  $ 2dd # deletes next two lines, continues: 3dd, 4dd and so on.
  364.    364  PUT (PASTE)
  365.    365  $ p # uses what was deleted/cut and pastes it behind cursor
  366.    366  COPY & PASTE
  367.    367  $ yy # copies line, for copying several lines do 2yy, 3yy and so on
  368.    368  $ p # pastes clipboard behind cursor
  369.    369  SEARCH IN FILE (most regular expressions work)
  370.    370  $ /my_pattern # searches for my_pattern downwards, type n for next match
  371.    371  $ ?my_pattern # seraches for my_pattern upwards, type n for next match
  372.    372  $ :set ic # switches to ignore case search (case insensitive)
  373.    373  $ :set hls # switches to highlight search (highlights search hits)
  374.    374  REPLACE WITH REGULAR EXPRESSIONS (great intro: A Tao of Regular Expressions)
  375.    375  $ :s/old_pat/new_pat/ # replaces first occurence in a line
  376.    376  $ :s/old_pat/new_pat/g # replaces all occurence in a line
  377.    377  $ :s/old_pat/new_pat/gc # add 'c' to ask for confirmation
  378.    378  $ :#,#s/old_pat/new_pat/g # replaces all occurence between line numbers: #,#
  379.    379  $ :%s/old_pat/new_pat/g # replaces all occurence in file
  380.    380  $ :%s/\(pattern1\)\(pattern2\)/\1test\2/g # regular expression to insert, you need here '\' in front of parentheses (<# Perl)
  381.    381  $ :%s/\(pattern.*\)/\1 my_tag/g # appends something to line containing pattern (<# .+ from Perl is .* in VIM)
  382.    382  $ :%s/\(pattern\)\(.*\)/\1/g # removes everything in lines after pattern
  383.    383  $ :%s/\(At\dg\d\d\d\d\d\.\d\)\(.*\)/\1\t\2/g # inserts tabs between At1g12345.1 and Description
  384.    384  $ :%s/\n/new_pattern/g #Replaces return signs
  385.    385  $ :%s/pattern/\r/g #Replace pattern with return signs!!
  386.    386  $ :%s/\(\n\)/\1\1/g # insert additional return signs
  387.    387  $ :%s/\(^At\dg\d\d\d\d\d.\d\t.\{-}\t.\{-}\t.\{-}\t.\{-}\t\).\{-}\t/\1/g # replaces content between 5th and 6th tab (5th column), '{-}' turns off 'greedy' behavior
  388.    388  $ :#,#s/\( \{-} \|\.\|\n\)/\1/g # performs simple word count in specified range of text
  389.    389  $ :%s/\(E\{6,\}\)/<font color="green">\1<\/font>/g # highlight pattern in html colors, here highlighting of >= 6 occurences of Es
  390.    390  $ :%s/\([A-Z]\)/\l\1/g # change uppercase to lowercase, '%s/\([A-Z]\)/\u\1/g' does the opposite
  391.    391  $ :g/my_pattern/ s/\([A-Z]\)/\l\1/g | copy $ # uses 'global' command to apply replace function only on those lines that match a certain pattern. The 'copy $' command after the pipe '|' prints all matching lines at the end of the file.
  392.    392  $ :args *.txt | all | argdo %s/\old_pat/new_pat/ge | update # Command 'args' places all relevant files in the argument list (buffer); 'all' displays each file in separate split window; command 'argdo' applies replacement to all files in argument list (buffer); flag 'e' is necessary to avoid stop at error messages for files with no matches; command 'update' saves all changes to files that were updated.
  393.    393  MATCHING PARENTHESES SEARCH
  394.    394  - place curser on (, [ or { and type % # curser moves to matching parentheses
  395.    395  HTML EDITING
  396.    396  -Convert text file to html format:
  397.    397  $ :runtime! syntax/2html.vim # run this command with open file in Vim
  398.    398  SHELL COMMAND IN VIM
  399.    399  $ :!<SHELL_COMMAND> <ENTER> # executes any shell command, hit <enter> to return
  400.    400  $ :sh # switches window to shell, 'exit' switches back to vim
  401.    401  USING VIM AS TABLE EDITOR
  402.    402  $ v # starts visual mode for selecting characters
  403.    403  $ V # starts visual mode for selecting lines
  404.    404  $ CTRL-V # starts visual mode for selecting blocks (use CTRL-q in gVim under Windows). This allows column-wise selections and operations like inserting and deleting columns. To restrict substitude commands to a column, one can select it and switch to the command-line by typing ':'. After this the substitution sytax for a selected block looks like this: '<,'>s///.
  405.    405  $ :set scrollbind # starts simultaneous scrolling of 'vsplitted' files. To set to horizontal binding of files, use command ':set scrollopt=hor' (after first one). Run all these commands before the ':split' command.
  406.    406  $ :AlignCtrl I= \t then :%Align # This allows to align tables by column separators (here '\t') when the Align utility from Charles Campbell's is installed.
  407.    407  To sort table rows by selected lines or block, perform the visual select and then hit F3 key. The rest is interactive. To enable this function one has to include in the .vimrc file from Gerald Lai the Vim sort script.
  408.    408  MODIFY VIM SETTINGS (in file .vimrc)
  409.    409  - see last chapter of vimtutor (start from shell)
  410.    410  - useful .vimrc sample
  411.    411  - when vim starts to respond very slowly then one may need to delete the .viminf* files in home directory
  412.    412 
  413.    413  THE UNIX SHELL
  414.    414  When you log into UNIX/LINUX the system starts a program called SHELL. It provides you with a working environment and interface to the operating system. Usually there are many different shell programs installed.
  415.    415  $ finger <user_name> # shows which shell you are using
  416.    416  $ chsh -l # gives list of shell programs available on your system (does not work on all UNIX variants)
  417.    417  $ <shell_name> # switches to different shell
  418.    418  STDIN, STDOUT, STDERR, REDIRECTORS, OPERATORS & WILDCARDS (more on this @ LINUX HOWTOs)
  419.    419  By default, many UNIX commands read from standard input (STDIN) and send their output to standard out (STDOUT). You can redirect them by using the following commands:
  420.    420  $ file* # * is wildcard to specify many files
  421.    421  $ ls > file # prints ls output into specified file
  422.    422  $ command < my_file # uses file after '<' as STDIN
  423.    423  $ command >> my_file # appends output of one command to file
  424.    424  $ command | tee my_file # writes STDOUT to file and prints it to screen; alternative way to do this:
  425.    425  $ command > my_file; cat my_file
  426.    426  $ command > /dev/null # turns off progress info of applications by redirecting their output to /dev/null
  427.    427  $ grep my_pattern my_file | wc # Pipes (|) output of 'grep' into 'wc'
  428.    428  $ grep my_pattern my_non_existing_file 2 > my_stderr # prints STDERR to file
  429.    429  Useful shell commands
  430.    430  $ cat <file1> <file2> > <cat.out> # concatenate files in output file 'cat.out'
  431.    431  $ paste <file1> <file2> > <paste.out> # merges lines of files and separates them by tabs (useful for tables)
  432.    432  $ cmp <file1> <file2> # tells you whether two files are identical
  433.    433  $ diff <fileA> <fileB> # finds differences between two files
  434.    434  $ head -<number> <file> # prints first lines of a file
  435.    435  $ tail -<number> <file> # prints last lines of a file
  436.    436  $ split -l <number> <file> # splits lines of file into many smaller ones
  437.    437  $ csplit -f out fasta_batch "%^>%" "/^>/" "{*}" # splits fasta batch file into many files at '>'
  438.    438  $ sort <file> # sorts single file, many files and can merge (-m) them, -b ignores leading white space, ...
  439.    439  $ sort -k 2,2 -k 3,3n input_file > output_file # sorts in table column 2 alphabetically and column 3 numerically, '-k' for column, '-n' for numeric
  440.    440  $ sort input_file | uniq > output_file # uniq command removes duplicates and creates file/table with unique lines/fields
  441.    441  $ join -1 1 -2 1 <table1> <table2> # joins two tables based on specified column numbers (-1 file1, 1: col1; -2: file2, col2). It assumes that join fields are sorted. If that is not the case, use the next command:
  442.    442  $ sort table1 > table1a; sort table2 > table2a; join -a 1 -t "`echo -e '\t'`" table1a table2a > table3 # '-a <table>' prints all lines of specified table! Default prints only all lines the two tables have in common. '-t "`echo -e '\t'`" ->' forces join to use tabs as field separator in its output. Default is space(s)!!!
  443.    443  $ cat my_table | cut -d , -f1-3 # cut command prints only specified sections of a table, -d specifies here comma as column separator (tab is default), -f specifies column numbers.
  444.    444  $ grep and egrep # see chapter 4
  445.    445 
  446.    446  SIMPLE SHELL ONE-LINER SCRIPTS
  447.    447  Useful One-Liners (script download)
  448.    448  $ for i in *.input; do mv $i ${i/name\.old/name\.new}; done # renames file name.old to name.new
  449.    449  - To test things first, insert 'echo' between 'do mv' (above).
  450.    450  $ for i in *.input; do ./application $i; done # runs application in loops on many input files
  451.    451  $ for i in *.input; do fastacmd -d /data/../database_name -i $i > $i.out; done # runs fastacmd in loops on many *.input files and creates *.out files
  452.    452  $ for i in *.pep; do target99 -db /usr/../database_name -seed $i -out $i; done # runs SAM's target99 on many input files
  453.    453  $ for j in 0 1 2 3 4 5 6 7 8 9; do grep -iH <my_pattern> *$j.seq; done # searches in > 10,000 files for pattern and prints occurences together with file names.
  454.    454  $ for i in *.pep; do echo -e "$i\n\n17\n33\n\n\n" | ./tmpred $i > $i.out; done # example of how to run an interactive application (tmpred) that asks for file name input/output
  455.    455  $ for i in *.fasta1; do blast2 -p blastp -i $i -j ${i/_*fasta1/_*fasta2} >> my_out_file; done # runs BLAST2 for all *.fasa1/*.fasta2 file pairs in the order specified by file names and writes results into one file. This example uses two variables in a for loop. The content of the second variable gets specified in each loop by a replace function.
  456.    456  $ for i in *.fasta; do for j in *.fasta; do blast2 -p blastp -F F -i $i -j $j >> my_out_file; done; done; # runs BLAST2 in all-against-all mode and writes results into one file; '-F F' turns low-complexity filter off
  457.    457  How to write a script
  458.    458  - create file which contains in first line:
  459.    459  #!/bin/bash
  460.    460  - place shell commands in file
  461.    461  - run <chmod +x my_shell_script> to make it executable
  462.    462  - run shell script like this: ./my_shell_script
  463.    463  - when you place it into /usr/local/bin you only type its name from any user account
  464.    464 
  465.    465  SIMPLE PERL ONE-LINER SCRIPTS
  466.    466  Useful One-Liners
  467.    467  $ perl -p -i -w -e 's/pattern1/pattern2/g' input_file # replace something (e.g. return signs) in file using regular expressions; use $1 to backreference to pattern placed in parentheses
  468.    468  '-p' lets perl know to write program; '-i.bak' creates backup file *.bak, only -i doesn't; '-w' turns on warnings; '-e' executeable code follows
  469.    469  $ perl -ne 'print if (/my_pattern1/ ? ($c=1) : (--$c > 0)) ; print if (/my_pattern2/ ? ($d = 1) : (--$d > 0))' my_infile > my_outfile # parses lines that contain pattern1 and pattern2
  470.    470  following lines after pattern can be specified in '$c=1' and '$d=1'; for OR function use this syntax: '/(pattern1|pattern2)/'
  471.    471 
  472.    472  REMOTE COPY: WGET, SCP and NCFTP
  473.    473  WGET (file download from the www)
  474.    474  $ wget ftp://ftp.ncbi.nih.... # file download from www; add option '-r' to download entire directories
  475.    475  SCP (secure copy between machines)
  476.    476  General syntax
  477.    477  $ scp source target # Use form 'userid@machine_name' if your local and remote user ids are differnt. If they are the same you can use only 'machine_name'.
  478.    478 
  479.    479  Examples
  480.    480  Copy file from Server to Local Machine (type from local machine prompt):
  481.    481  $ scp user@remote_host:file.name . # '.' copies to pwd, you can specify here any directory, use wildcards to copy many files at once.
  482.    482  Copy file from Local Machine to Server:
  483.    483  $ scp file.name user@remote_host:~/dir/newfile.name
  484.    484  Copy entire directory from Server to Local Machine (type from local machine prompt):
  485.    485  $ scp -r user@remote_host:directory/ ~/dir
  486.    486  Copy entire directory from Local Machine to Server (type from local machine prompt):
  487.    487  $ scp -r directory/ user@remote_host:directory/
  488.    488  Copy between two remote hosts (e.g. from bioinfo to cache):
  489.    489  similar as above, just be logged in one of the remote hosts:
  490.    490  $ scp -r directory/ user@remote_host:directory/
  491.    491  NICE FTP
  492.    492  $ open ncftp
  493.    493  $ ncftp> open ftp.ncbi.nih.gov
  494.    494  $ ncftp> cd /blast/executables
  495.    495  $ ncftp> get blast.linux.tar.Z (skip extension: @)
  496.    496  $ ncftp> bye
  497.    497 
  498.    498  ARCHIVING AND COMPRESSING
  499.    499  Archiving and compressing
  500.    500  $ tar -cvf my_file.tar mydir/ # Builds tar archive of files or directories. For directories, execute command in parent directory. Don't use absolute path.
  501.    501  $ tar -czvf my_file.tgz mydir/ # Builds tar archive with compression of files or directories. For directories, execute command in parent directory. Don't use absolute path.
  502.    502  Viewing Archives
  503.    503  $ tar -tvf my_file.tar
  504.    504  $ tar -tzvf my_file.tgz
  505.    505  Extracting
  506.    506  $ tar -xvf my_file.tar
  507.    507  $ tar -xzvf my_file.tgz
  508.    508  $ gunzip my_file.tar.gz # or unzip my_file.zip, uncompress my_file.Z, or bunzip2 for file.tar.bz2
  509.    509  $ find -name '*.zip' | xargs -n 1 unzip # this command usually works for unziping many files that were compressed under Windows
  510.    510  try also:
  511.    511  $ tar zxf blast.linux.tar.Z
  512.    512  $ tar xvzf file.tgz
  513.    513  options:
  514.    514  f: use archive file
  515.    515  p: preserve permissions
  516.    516  v: list files processed
  517.    517  x: exclude files listed in FILE
  518.    518  z: filter the archive through gzip
  519.    519 
  520.    520  SIMPLE INSTALLS
  521.    521  Systems-wide installations
  522.    522  Installations for systems-wide usage are the responsibility of system administrator
  523.    523  To find out if an application is installed, type:
  524.    524  $ which <application_name>
  525.    525  $ whereis <application_name> # searches for executeables in set of directories, doesn't depend on your path
  526.    526  Most applications are installed in /usr/local/bin or /usr/bin. You need root permissions to write to these directories.
  527.    527  Perl scripts go into /usr/local/bin, Perl modules (*.pm) into /usr/local/share/perl/5.8.0/. To copy executables in one batch, use command: cp `find -perm -111 -type f` /usr/local/bin
  528.    528  Applications in user accounts
  529.    529  Create a new directory, download application into this directory, unpack it (see chapter 13) and follow package-specific installation instructions.
  530.    530  Usually you can then already run this application when you specify its location e.g.: /home/user/my_app/blastall.
  531.    531  If you want you can add this directory to your PATH by typing from this directory:
  532.    532  $ PATH=.:$PATH; export PATH # this allows you to run application by providing only its name; when you do echo $PATH you will see .: added to PATH.
  533.    533  Intstallation of RPMs
  534.    534  $ rpm -i application_name.rpm
  535.    535  To check which version of RPM package is installed, type:
  536.    536  $ rpm --query <package_name>
  537.    537  Help and upgrade files for RPMs can be found at http://rpmfind.net/.
  538.    538  Installation of Debian packages
  539.    539  Check whether your application is available at: http://www.debian.org/intro/about, then you type (no download):
  540.    540  $ apt-cache search phylip #searches for application "phylip" from command line
  541.    541  $ apt-cache show phylip #provides description of program
  542.    542  $ apt-get install phylip # example for phylip install, manuals can be found in /usr/doc/phylip/, use zless or lynx to read documentation (don't unzip).
  543.    543  $ apt-get update # do once a month do update Debian packages
  544.    544  $ apt-get upgrade -u # to upgrade after update from above
  545.    545  $ dpkg -i # install data package from local package file (e.g. after download)
  546.    546  $ aptitude # Debian package manageing interface (Ctrl-t opens menues)
  547.    547  $ aptitude search vim # search for packages on system and in Debian depositories
  548.    548 
  549.    549  DEVICES
  550.    550  Mount/unmount usb/floppy/cdrom
  551.    551  $ mount /media/usb
  552.    552  $ umount /media/usb
  553.    553  $ mount /media/cdrom
  554.    554  $ eject /media/cdrom
  555.    555  $ mount /media/floppy
  556.    556 
  557.    557  ENVIRONMENT VARIABLES
  558.    558  $ xhost user@host # adds X permissions for user on server.
  559.    559  $ echo DISPLAY # shows current display settings
  560.    560  $ export (setenv) DISPLAY=<local_IP>:0 # change environment variable
  561.    561  $ unsetenv DISPLAY # removes display variable
  562.    562  $ printenv # prints all environment variables
  563.    563  $ $PATH # list of directories that the shell will search when you type a command
  564.    564  - You can edit your default DISPLAY setting for your account by adding it to file .bash_profile.
  565.    565 
  566.    566  EXERCISES
  567.    567  Exercise 1
  568.    568  Download proteome of Halobacterium spec. from ftp://ftp.ncbi.nih.gov/genbank/genomes/Bacteria/Halobacterium_sp/AE004437.faa (use wget or web browser for download)
  569.    569  How many predicted proteins are there?
  570.    570  $ grep '>' AE004437.faa | wc
  571.    571  How many proteins contain the pattern "WxHxxH[1-2]"?
  572.    572  $ egrep 'W.H..H{1,2}' AE004437.faa | wc
  573.    573  Use the find function (/) in 'less' to fish out the proteins containing this pattern or more elegantly do it with awk:
  574.    574  $ awk --posix -v RS='>' '/W.H..(H){1,2}/ { print ">" $0;}' AE004437.faa | less
  575.    575  Create a BLASTable database with formatdb
  576.    576  $ formatdb -i AE004437.faa -p T -o T
  577.    577  '-p F' for nucleotide and '-p T' for protein databases
  578.    578  Generate list of sequence IDs for above pattern match result and retrieve its sequences with fastacmd from formatted database
  579.    579  $ fastacmd -d AE004437.faa -i my_IDs > seq
  580.    580  Generate several lists of sequence IDs from various pattern match results and retrieve their sequences in one step using the fastacmd in for loop
  581.    581  $ for i in *.my_ids; do fastacmd -d AE004437.faa -i $i > $i.out; done
  582.    582  Run blastall with a few proteins against newly created database or against Halobacterium or UniProt database (/data/UNIPROT/blast/uniprot)
  583.    583  $ blastall -p blastp -i input.file -d AE004437.faa -o blastp.out -e 1e-6 -v 10 -b 10 &
  584.    584  Parse blastall output into Excel spread sheet:
  585.    585  a) using biocore parser
  586.    586  $ blastParse -c <hits> -i <blast.out> -o <blast.parse>
  587.    587  b) using BioPerl parser
  588.    588  $ bioblastParse.pl blast.out
  589.    589  Run HMMPFAM search with above proteins against Pfam database
  590.    590  $ hmmpfam -E 0.1 --acc -A0 /data/PFAM/Pfam_ls input.file > output.pfam
  591.    591  Parse result with BioPerl parser
  592.    592  $ hmmSummary output.pfam > hmm.summary
  593.    593  Exercise 2
  594.    594  Split sample fasta batch file with csplit (use sequence file from exercise 1).
  595.    595  Concatenate single fasta files from (1) to one batch file.
  596.    596  BLAST two related sequences, retrieve the result in table format and use join to identify common hit IDs in the two tables.
  597.    597  Exercise 3
  598.    598  write a shell script that executes several BLAST searches at once:
  599.    599  #!/bin/sh
  600.    600  blastall -p blastp -d /.../my_database -i /.../my_input -o my_out -e 1e-6 -v 10 -b 10 &
  601.    601  blastall -p blastp -d /.../my_database -i /.../my_input -o my_out -e 1e-6 -v 10 -b 10 &
  602.    602  Exercise 4
  603.    603  Create multiple alignment with ClustalW (e.g. use sequences with 'W.H..HH' pattern)
  604.    604  $ clustalw my_fasta_batch
  605.    605  Exercise 5
  606.    606  Reformat alignment into PHYILIP format using 'seqret' from EMBOSS
  607.    607  $ seqret clustal::my_align.aln phylip::my_align.phylip
  608.    608  Exercise 6
  609.    609  Create neighbor-joining tree with PHYLIP
  610.    610  $ cp my_align.phylip infile
  611.    611  $ phylip protdist # creates distance matrix
  612.    612  $ cp outfile infile
  613.    613  $ phylip neighbor # use default settings
  614.    614  $ cp outtree intree
  615.    615  $ phylip retree # displays tree and can use midpoint method for defining root of tree, my typical command sequence is: 'N' 'Y' 'M' 'W' 'R' 'R' 'X'
  616.    616  $ cp outtree my_tree.dnd
  617.    617  View your tree in TreeBrowse or open it in TreeView
  618.    618   
  619.    619  ---------------------------------------------
  620.    620 
  621.    621  prompt () {
  622.    622      local -a prompt_opts theme_active
  623.    623      zstyle -g theme_active :prompt-theme cleanup || {
  624.    624          [[ -o promptbang ]] && prompt_opts+=(bang)
  625.    625          [[ -o promptcr ]] && prompt_opts+=(cr)
  626.    626          [[ -o promptpercent ]] && prompt_opts+=(percent)
  627.    627          [[ -o promptsp ]] && prompt_opts+=(sp)
  628.    628          [[ -o promptsubst ]] && prompt_opts+=(subst)
  629.    629          zstyle -e :prompt-theme cleanup 'zstyle -d :prompt-theme cleanup;' 'prompt_default_setup;' ${PS1+PS1="${(q)PS1}"} ${PS2+PS2="${(q)PS2}"} ${PS3+PS3="${(q)PS3}"} ${PS4+PS4="${(q)PS4}"} ${RPS1+RPS1="${(q)RPS1}"} ${RPS2+RPS2="${(q)RPS2}"} ${RPROMPT+RPROMPT="${(q)RPROMPT}"} ${RPROMPT2+RPROMPT2="${(q)RPROMPT2}"} ${PSVAR+PSVAR="${(q)PSVAR}"} "precmd_functions=(${(q)precmd_functions[@]})" "preexec_functions=(${(q)preexec_functions[@]})" "prompt_opts=( ${prompt_opts[*]} )" 'reply=(yes)'
  630.    630      }
  631.    631      set_prompt "$@"
  632.    632      (( ${#prompt_opts} )) && setopt noprompt{bang,cr,percent,sp,subst} "prompt${^prompt_opts[@]}"
  633.    633      true
  634.    634  }
  635.    635  clippaste () {
  636.    636      emulate -L zsh
  637.    637      if [[ $OSTYPE == darwin* ]]
  638.    638      then
  639.    639          pbpaste
  640.    640      elif [[ $OSTYPE == cygwin* ]]
  641.    641      then
  642.    642          cat /dev/clipboard
  643.    643      else
  644.    644          if (( $+commands[xclip] ))
  645.    645          then
  646.    646              xclip -out -selection clipboard
  647.    647          elif (( $+commands[xsel] ))
  648.    648          then
  649.    649              xsel --clipboard --output
  650.    650          else
  651.    651              print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  652.    652              return 1
  653.    653          fi
  654.    654      fi
  655.    655  }
  656.    656 
  657.    657  Sun Sep  2 12:50:36 EDT 2018
  658.    658  NOTE:
  659.    659 
  660.    660  In the example below we will print the numbers 1 through to 10:
  661.    661 
  662.    662  while_loop.sh
  663.    663  #!/bin/bash
  664.    664  # Basic while loop
  665.    665  counter=1
  666.    666  while [ $counter -le 10 ]
  667.    667  do
  668.    668  echo $counter
  669.    669  ((counter++))
  670.    670  done
  671.    671  echo All done
  672.    672 
  673.    673 
  674.    674  Let's break it down:
  675.    675 
  676.    676   
  677.    677   
  678.    678  ---------------------------------------------
  679.    679 
  680.    680 
  681.    681  For Loops
  682.    682  The for loop is a little bit different to the previous two loops. What it does is say for each of the items in a given list, perform the given set of commands. It has the following syntax.
  683.    683 
  684.    684  for var in <list>
  685.    685  do
  686.    686  <commands>
  687.    687  done
  688.    688 
  689.    689  The for loop will take each item in the list (in order, one after the other), assign that item as the value of the variable var, execute the commands between do and done then go back to the top, grab the next item in the list and repeat over.
  690.    690 
  691.    691  The list is defined as a series of strings, separated by spaces.
  692.    692 
  693.    693  Here is a simple example to illustrate:
  694.    694 
  695.    695  for_loop.sh
  696.    696  #!/bin/bash
  697.    697  # Basic for loop
  698.    698  names='Stan Kyle Cartman'
  699.    699  for name in $names
  700.    700  do
  701.    701  echo $name
  702.    702  done
  703.    703  echo All done
  704.    704  Let's break it down:
  705.    705 
  706.    706  Line 4 - Create a simple list which is a series of names.
  707.    707  Line 6 - For each of the items in the list $names assign the item to the variable $name and do the following commands.
  708.    708  Line 8 - echo the name to the screen just to show that the mechanism works. We can have as many commands here as we like.
  709.    709  Line 11 - echo another command to show that the bash script continued execution as normal after all the items in the list were processed.
  710.    710  ./for_loop.sh
  711.    711  Stan
  712.    712  Kyle
  713.    713  Cartman
  714.    714  All done
  715.    715  Ranges
  716.    716  We can also process a series of numbers
  717.    717 
  718.    718  for_loop_series.sh
  719.    719  #!/bin/bash
  720.    720  # Basic range in for loop
  721.    721  for value in {1..5}
  722.    722  do
  723.    723  echo $value
  724.    724  done
  725.    725  echo All done
  726.    726  Line 4 - It's important when specifying a range like this that there are no spaces present between the curly brackts { }. If there are then it will not be seen as a range but as a list of items.
  727.    727  ./for_loop_series.sh
  728.    728  1
  729.    729  2
  730.    730  3
  731.    731  4
  732.    732  5
  733.    733  All done
  734.    734  When specifying a range you may specify any number you like for both the starting value and ending value. The first value may also be larger than the second in which case it will count down.
  735.    735 
  736.    736  It is also possible to specify a value to increase or decrease by each time. You do this by adding another two dots ( .. ) and the value to step by.
  737.    737 
  738.    738  for_loop_stepping.sh
  739.    739  #!/bin/bash
  740.    740  # Basic range with steps for loop
  741.    741  for value in {10..0..2}
  742.    742  do
  743.    743  echo $value
  744.    744  done
  745.    745  echo All done
  746.    746  ./for_loop.sh
  747.    747  10
  748.    748  8
  749.    749  6
  750.    750  4
  751.    751  2
  752.    752  0
  753.    753  All done
  754.    754  One of the more useful applications of for loops is in the processing of a set of files. To do this we may use wildcards. Let's say we want to convert a series of .html files over to .php files.
  755.    755 
  756.    756  convert_html_to_php.sh
  757.    757  #!/bin/bash
  758.    758  # Make a php copy of any html files
  759.    759  for value in $1/*.html
  760.    760  do
  761.    761  cp $value $1/$( basename -s .html $value ).php
  762.    762  done
  763.    763  Controlling Loops: Break and Continue
  764.    764  Most of the time your loops are going to through in a smooth and ordely manner. Sometimes however we may need to intervene and alter their running slightly. There are two statements we may issue to do this.
  765.    765 
  766.    766  Break
  767.    767  The break statement tells Bash to leave the loop straight away. It may be that there is a normal situation that should cause the loop to end but there are also exceptional situations in which it should end as well. For instance, maybe we are copying files but if the free disk space get's below a certain level we should stop copying.
  768.    768 
  769.    769  copy_files.sh
  770.    770  #!/bin/bash
  771.    771  # Make a backup set of files
  772.    772  for value in $1/*
  773.    773  do
  774.    774  used=$( df $1 | tail -1 | awk '{ print $5 }' | sed 's/%//' )
  775.    775  if [ $used -gt 90 ]
  776.    776  then
  777.    777  echo Low disk space 1>&2
  778.    778  break
  779.    779  fi
  780.    780  cp $value $1/backup/
  781.    781  done
  782.    782  Continue
  783.    783  The continue statement tells Bash to stop running through this iteration of the loop and begin the next iteration. Sometimes there are circumstances that stop us from going any further. For instance, maybe we are using the loop to process a series of files but if we happen upon a file which we don't have the read permission for we should not try to process it.
  784.    784 
  785.    785  copy_check.sh
  786.    786  #!/bin/bash
  787.    787  # Make a backup set of files
  788.    788  for value in $1/*
  789.    789  do
  790.    790  if [ ! -r $value ]
  791.    791  then
  792.    792  echo $value not readable 1>&2
  793.    793  continue
  794.    794  fi
  795.    795  cp $value $1/backup/
  796.    796  done
  797.    797  Select
  798.    798  The select mechanism allows you to create a simple menu system. It has the following format:
  799.    799 
  800.    800  select var in <list>
  801.    801  do
  802.    802  <commands>
  803.    803  done
  804.    804 
  805.    805  When invoked it will take all the items in list (similar to other loops this is a space separated set of items) and present them on the screen with a number before each item. A prompt will be printed after this allowing the user to select a number. When they select a number and hit enter the corresponding item will be assigned to the variable var and the commands between do and done are run. Once finished a prompt will be displayed again so the user may select another option.
  806.    806 
  807.    807  A few points to note:
  808.    808 
  809.    809  No error checking is done. If the user enters something other than a number or a number not corresponding to an item then var becomes null (empty)
  810.    810  If the user hits enter without entering any data then the list of options will be displayed again.
  811.    811  The loop will end when an EOF signal is entered or the break statement is issued.
  812.    812  You may change the system variable PS3 to change the prompt that is displayed.
  813.    813  Here is a simple example to illustrate it's usage:
  814.    814 
  815.    815  select_example.sh
  816.    816  #!/bin/bash
  817.    817  # A simple menu system
  818.    818  names='Kyle Cartman Stan Quit'
  819.    819  PS3='Select character: '
  820.    820  select name in $names
  821.    821  do
  822.    822  if [ $name == 'Quit' ]
  823.    823  then
  824.    824  break
  825.    825  fi
  826.    826  echo Hello $name
  827.    827  done
  828.    828  echo Bye
  829.    829  Let's break it down:
  830.    830 
  831.    831  Line 4 - Set up a variable with the list of characters and a last option which we may select to quit. Note that the items are separated by a space.
  832.    832  Line 6 - Change the value of the system variable PS3 so that the prompt is set to something a little more descriptive. (By default it is #?)
  833.    833  Lines 10 - 13 - If the last option, 'Quit', is selected then break out of the select loop.
  834.    834  Line 14 - Print out a message just to demonstrate the mechanism has worked. You may have as many commands here as you like.
  835.    835  Line 17 - Print a message just to show that the script has continued as normal after the select loop.
  836.    836  And now let's run the Bash script:
  837.    837 
  838.    838  ./select_example.sh
  839.    839  1) Kyle     3) Stan
  840.    840  2) Cartman  4) Quit
  841.    841  Select character: 2
  842.    842  Hello Cartman
  843.    843  Select Character: 1
  844.    844  Hello Kyle
  845.    845  Select character: 4
  846.    846  Bye
  847.    847  Summary
  848.    848  while do done
  849.    849  Perform a set of commands while a test is true.
  850.    850  until do done
  851.    851  Perform a set of commands until a test is true.
  852.    852  for do done
  853.    853  Perform a set of commands for each item in a list.
  854.    854  break
  855.    855  Exit the currently running loop.
  856.    856  continue
  857.    857  Stop this iteration of the loop and begin the next iteration.
  858.    858  select do done
  859.    859  Display a simple menu system for selecting items from a list.
  860.    860  Clarity
  861.    861  There are several Bash loop mechanisms. Pick the one which makes your code the easiest to follow.
  862.    862  Planning
  863.    863  Now that your scripts are getting a little more complex you will probably want to spend a little bit of time thinking about how you structure them before diving in.
  864.    864 
  865.    865  Tue Sep  4 20:48:34 EDT 2018
  866.    866  NOTE: dd compression on the fly
  867.    867 
  868.    868  sudo bash -c "dd if=/dev/sda2 | gzip > /media/disk/sda2-backup-10august09.gz"
  869.    869  sudo dd if=/dev/sda2 | bzip2 --best > /media/disk/$(date +%Y%m%d_%H%M%S)_sda2-backup.bz2
  870.    870  lzma -z9ev
  871.    871   
  872.    872  ---------------------------------------------
  873.   873
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement