/home/bill/System_maintenance/1_bash notes.txt www.BillHowell.ca 07Jan2018 initial ***************** 15Jan2018 Cool - work frequency analysis and many other overall text processing utilities great summary http://www.tldp.org/LDP/abs/html/textproc.html #!/bin/bash # wf.sh: Crude word frequency analysis on a text file. # This is a more efficient version of the "wf2.sh" script. # Check for input file on command-line. ARGS=1 E_BADARGS=85 E_NOFILE=86 if [ $# -ne "$ARGS" ] # Correct number of arguments passed to script? then echo "Usage: `basename $0` filename" exit $E_BADARGS fi if [ ! -f "$1" ] # Check if file exists. then echo "File \"$1\" does not exist." exit $E_NOFILE fi look The command look works like grep, but does a lookup on a "dictionary," a sorted word list. By default, look searches for a match in /usr/dict/words, but a different dictionary file may be specified. Example 16-20. Checking words in a list for validity #!/bin/bash # lookup: Does a dictionary lookup on each word in a data file. file=words.data # Data file from which to read words to test. echo echo "Testing file $file" echo while [ "$word" != end ] # Last word in data file. do # ^^^ read word # From data file, because of redirection at end of loop. look $word > /dev/null # Don't want to display lines in dictionary file. # Searches for words in the file /usr/share/dict/words #+ (usually a link to linux.words). lookup=$? # Exit status of 'look' command. if [ "$lookup" -eq 0 ] then echo "\"$word\" is valid." else echo "\"$word\" is invalid." fi done <"$file" # Redirects stdin to $file, so "reads" come from there. echo exit 0 # ---------------------------------------------------------------- # Code below line will not execute because of "exit" command above. # Stephane Chazelas proposes the following, more concise alternative: while read word && [[ $word != end ]] do if look "$word" > /dev/null then echo "\"$word\" is valid." else echo "\"$word\" is invalid." fi done <"$file" exit 0 +-----+ 15Jan2018 my stab at the cull #do if look -bdf "$line" "$1" > /dev/null # grepper="$( echo "$line" | tr \\n \\0 | xargs -0 grep -e --ignore-case "$badder" )" # if [[ "$badder" = "$liner" ]] # then echo "\"$line\" is valid." # else echo "\"$line\" is invalid." # fi # tester=$( echo "$line" | grep "$badder" ) # fart="$( echo "george" | grep "eo" )" # -e | tr \\n \\0 echo "line = $line" echo "badder = $badder" echo "grepper = $grepper" ######################################################## # main () sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\ /g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr # ========================= # Frequency of occurrence # Filter out periods and commas, and #+ change space between words to linefeed, #+ then shift characters to lowercase, and #+ finally prefix occurrence count and sort numerically. # Arun Giridhar suggests modifying the above to: # . . . | sort | uniq -c | sort +1 [-f] | sort +0 -nr # This adds a secondary sort key, so instances of #+ equal occurrence are sorted alphabetically. # As he explains it: # "This is effectively a radix sort, first on the #+ least significant column #+ (word or string, optionally case-insensitive) #+ and last on the most significant column (frequency)." # # As Frank Wang explains, the above is equivalent to #+ . . . | sort | uniq -c | sort +0 -nr #+ and the following also works: #+ . . . | sort | uniq -c | sort -k1nr -k ######################################################## exit 0 # Exercises: # --------- # 1) Add 'sed' commands to filter out other punctuation, #+ such as semicolons. # 2) Modify the script to also filter out multiple spaces and #+ other whitespace. ***************** 07Jan2018 search "Linux and bash convert strings to numbers" strings == comparator integers -eq -lt -gt a=expr'$a/1' ***************** 07Jan2018 search "Linux and bash arguments to a call" https://stackoverflow.com/questions/16988427/calling-one-bash-script-from-another-script-passing-it-arguments-with-quotes-and >> good for one of my problems, but I didn't paste here ***************** 07Jan2018 search "Linux and bash if statement" https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/ November 25, 2012 anthony Linux The syntax of an if statement (a short explanation) The basic syntax of an if … then statement is like this: if ; then fi The condition is, depending on its type, surrounded by certain brackets, eg. [ ]. You can read about the different types further on in the tutorial. You can add commands to be executed when the condition is false using the else keyword, and use the elif (elseif) keyword to execute commands on another condition if the primary condition is false. The else keyword always comes last. Example: if [ -r somefile ]; then content=$(cat somefile) elif [ -f somefile ]; then echo “The file ‘somefile’ exists but is not readable to the script.” else echo “The file ‘somefile’ does not exist.” fi >> Many good details on if statements (too large to paste here...) MAJOR PROBLEM!!! Do NOT use "==" in if conditions for strings, use a single "=" if [ "$2" == "pcmanfm" ]; then -> this does NOT work!! if [ "$2" = "pcmanfm" ]; then -> this works ****************************** 07Jan2018 search "Linux and bash assign a value to a function output" +-----+ https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash I am working on a simple scripting project for work that involves the use of Bash. I have a pretty simple script that is something like the following: #!/bin/bash VAR1="$1" VAR2="$2" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF variable, I am able to get output. I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen? edited Feb 16 '17 at 6:04, codeforester asked Jan 10 '11 at 20:58, John +--+ In addition to the backticks, you can use $(), which I find easier to read, and allows for nesting. OUTPUT="$(ls -1)" echo "${OUTPUT}" Quoting (") does matter to preserve multi-line values. edited Oct 17 '14 at 13:50, user2350426 answered Jan 10 '11 at 21:04, Andy Lester I know three ways to do: 1) Functions are suitable for such tasks: func (){ ls -l } Invoke it by saying func 2) Also another suitable solution could be eval: var="ls -l" eval $var 3) The third one is using variables directly: var=$(ls -l) OR var=`ls -l` you can get output of third solution in good way: echo "$var" and also in nasty way: echo $var edited May 17 '14 at 16:22, BoltClock answered Feb 13 '14 at 7:31, MLSC +--+ Some bash tricks I use to set variables from commands First simple old and compatible way myPi=`echo '4*a(1)' | bc -l` echo $myPi 3.14159265358979323844 Mostly compatible, second way As nesting could become heavy, parenthesis was implemented for this myPi=$(bc -l <<<'4*a(1)') Nested sample: SysStarted=$(date -d "$(ps ho lstart 1)" +%s) echo $SysStarted 1480656334 reading more than one variable (with bashisms) df -k / Filesystem 1K-blocks Used Available Use% Mounted on /dev/dm-0 999320 529020 401488 57% / If I just want Used value: array=($(df -k /)) you could see array variable: declare -p array declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [ 4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]= "401488" [11]="57%" [12]="/")' Then: echo ${array[9]} 529020 But I prefer this: { read foo ; read filesystem size used avail prct mountpoint ; } < <(df -k /) echo $used 529020 1st read foo will just skip header line (variable $foo will contain something like Filesystem 1K-blocks Used Available Use% Mounted on) About sudo cmd | grep ... | cut ... shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7) echo $shell /bin/bash (Please avoid useless cat! So this is just 1 fork less: shell=$(grep $USER