Easily find issues by searching: #<Issue ID>
Example: #1832
Easily find members by searching in: <username>, <first name> and <last name>.
Example: Search smith, will return results smith and adamsmith
These utilities emit a sequence of integers, with a user-selectable increment.
The default separator character between each integer is a newline, but this can be changed with the -s option.
bash$ seq 5 1 2 3 4 5 bash$ seq -s : 5 1:2:3:4:5
Both jot
and seq
come in handy in a for loop.
Example 16-54. Using seq to generate loop arguments
#!/bin/bash # Using "seq" echo for a in `seq 80` # or for a in $( seq 80 ) # Same as for a in 1 2 3 4 5 ... 80 (saves much typing!). # May also use 'jot' (if present on system). do echo -n "$a " done # 1 2 3 4 5 ... 80 # Example of using the output of a command to generate # the [list] in a "for" loop. echo; echo COUNT=80 # Yes, 'seq' also accepts a replaceable parameter. for a in `seq $COUNT` # or for a in $( seq $COUNT ) do echo -n "$a " done # 1 2 3 4 5 ... 80 echo; echo BEGIN=75 END=80 for a in `seq $BEGIN $END` # Giving "seq" two arguments starts the count at the first one, #+ and continues until it reaches the second. do echo -n "$a " done # 75 76 77 78 79 80 echo; echo BEGIN=45 INTERVAL=5 END=80 for a in `seq $BEGIN $INTERVAL $END` # Giving "seq" three arguments starts the count at the first one, #+ uses the second for a step interval, #+ and continues until it reaches the third. do echo -n "$a " done # 45 50 55 60 65 70 75 80 echo; echo exit 0
A simpler example:
# Create a set of 10 files, #+ named file.1, file.2 . . . file.10. COUNT=10 PREFIX=file for filename in `seq $COUNT` do touch $PREFIX.$filename # Or, can do other operations, #+ such as rm, grep, etc. done
Example 16-55. Letter Count"
#!/bin/bash # letter-count.sh: Counting letter occurrences in a text file. # Written by Stefano Palmeri. # Used in ABS Guide with permission. # Slightly modified by document author. MINARGS=2 # Script requires at least two arguments. E_BADARGS=65 FILE=$1 let LETTERS=$#-1 # How many letters specified (as command-line args). # (Subtract 1 from number of command-line args.) show_help(){ echo echo Usage: `basename $0` file letters echo Note: `basename $0` arguments are case sensitive. echo Example: `basename $0` foobar.txt G n U L i N U x. echo } # Checks number of arguments. if [ $# -lt $MINARGS ]; then echo echo "Not enough arguments." echo show_help exit $E_BADARGS fi # Checks if file exists. if [ ! -f $FILE ]; then echo "File \"$FILE\" does not exist." exit $E_BADARGS fi # Counts letter occurrences . for n in `seq $LETTERS`; do shift if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # Checks arg. echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # Counting. else echo "$1 is not a single char." fi done exit $? # This script has exactly the same functionality as letter-count2.sh, #+ but executes faster. # Why?
Somewhat more capable than seq
, jot
is a classic UNIX utility that is not normally included in a standard Linux distro. However, the source rpm
is available for download from the MIT repository.
Unlike seq
, jot
can generate a sequence of random numbers, using the -r
option.
bash$ jot -r 3 999 1069 1272 1428
The getopt
command parses command-line options preceded by a dash. This external command corresponds to the getopts Bash builtin. Using getopt
permits handling long options by means of the -l flag, and this also allows parameter reshuffling.
Example 16-56. Using getopt to parse command-line options
#!/bin/bash # Using getopt # Try the following when invoking this script: # sh ex33a.sh -a # sh ex33a.sh -abc # sh ex33a.sh -a -b -c # sh ex33a.sh -d # sh ex33a.sh -dXYZ # sh ex33a.sh -d XYZ # sh ex33a.sh -abcd # sh ex33a.sh -abcdZ # sh ex33a.sh -z # sh ex33a.sh a # Explain the results of each of the above. E_OPTERR=65 if [ "$#" -eq 0 ] then # Script needs at least one command-line argument. echo "Usage $0 -[options a,b,c]" exit $E_OPTERR fi set -- `getopt "abcd:" "$@"` # Sets positional parameters to command-line arguments. # What happens if you use "$*" instead of "$@"? while [ ! -z "$1" ] do case "$1" in -a) echo "Option \"a\"";; -b) echo "Option \"b\"";; -c) echo "Option \"c\"";; -d) echo "Option \"d\" $2";; *) break;; esac shift done # It is usually better to use the 'getopts' builtin in a script. # See "ex33.sh." exit 0
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017