General Web Resources


Table of Contents

Linux Shell Scripting with Bash 
Script basics 
Advanced Bash-Scripting Guide 

Linux Shell Scripting with Bash 

http://www.ieaa.org/~adrian/wiki/uploads/Main/LinuxShellScriptingWithBash.html

Extremely helpful site.

[...]

Script basics 

  • Suspend a script and wait until a signal: suspend
  • Reading keyboard input and put into $VAR: read VAR

    • With prompt: read -p "Enter a value: " VAR
    • By default, read understands backslash escaping of characters. To read as raw (i.e. backspace is a character), use: read -r VAR
    • With 5 sec timeout for reading input: read -t 5 VAR
    • With a limit of 10 characters max (no need Enter if reached): read -n 10 VAR
    • If no variables is given, by default read will store the value into $REPLY

      [...]

Process substitution 

  • Example: grep -f <(ls -1) /var/log/filelist.log
  • Example: cat textfile > >(wc -l)
  • <(list) will substitute on-the-fly with a FIFO, which can be read to obtain the output of list
  • >(list) will substitute on-the-fly with a FIFO, which can be written to, and list will read the content as if it is from console input

File descriptors 

  • exec 3< filename; read LINE <&3; Open filename as input and assign to file descriptor number 3
  • 3< filename; read LINE <&3; Open filename as input and assign to file descriptor number 3. But once assigned, 3 cannot be reassigned nor the file can be reopened.
  • exec 4<&3 Copy an input file descriptor. If 4 is omitted, 0 is assumed (as for input file descriptor)
  • exec 4<&3- Copy input file descriptor 3 to file descriptor 4, then close file descriptor 3.
  • exec 4>&3 Copy output file descriptor
  • exec 4>log.txt ; command >&4 Use of output file descriptor
  • exec 3<>filename Open a file for both input and output. But if you output something at the middle, the content pending for input will be overwritten. Think of a file position pointer concept.
  • exec 3<&- Close input file descriptor
  • exec 4>&- Close output file descriptor
  • /dev/fd/n Refer to file descriptor n

    [...]