Script

Filter
2016-06-03

Information om minnet

Linux
Kommando för att se vilka minnen som sitter i en linux-burk
Lista minnen:
lshw -C memory

Se vilka minnesplatser som är upptagna:
dmidecode -t 17
2016-06-03

Förloppsindikator

Linux
Enkelt kommandoprogram för att se ett förlopp
Installera programmmet pv.
Exempel på användning med kommandot dd för att se ett kopieringsförlopp med hjälp av pipes:

dd bs=4M if=infile | pv | dd of=outfile
2015-11-19

Ändra på flera mappar och filer på en gång

Bash
Spara tid med smarta kommandon i bash
2
Ex 1 - Ta bort alla .bak-filer i mappen /home/myName, rekursivt:
find /home/myName/ -type f -name "*.bak" -print | xargs rm -rf

Ex2 - Byt alla filändelser .html till .htm i aktuell mapp
find . -type f -name *.html -print | xargs rename .html .htm *.html

Ex3 - Ta bort alla mappar med namnet ".metadata"
find / -type d -name ".metadata" -print | xargs rm -rf

Ex4 - Byt ut alla mellanslag i filnamnet på alla jpg-bilder till _ (underline)
find . -type f -name '* *' | rename ' ' '_' *.jpg

Ex5 - Ta bort paranteser i filnamn
find . -type f -name '(' | rename '(' '' *
find . -type f -name ')' | rename ')' '' *
2015-01-07

Shellshock

Bash
Kolla om din bash är sårbar
# Kör på en rad i bash:

env 'VAR='() { :;}; echo Bash is vulnerable!' 'FUNCTION()=() { :;}; echo Bash is vulnerable!' bash -c "echo Bash Test"


# Om apache -> stäng av mod_cgi
2014-11-18

AWK Tutorial

Programming
Learning AWK with some examples
# Example file1:
# Beth   4.00   0
# Dan    3.75   0
# Kathy  4.00  10
# Mark   5.00  20
# Mary   5.50  22
# Susie  4.25  18

# Program form: pattern { action }

# Awk tests every input line against the pattern,
# and when a pattern matches, performs the corresponding action

# # Are comments, from the #-char to end of line

# Command form: awk "program" inputFiles
# Ex1: Command line: awk "$3 == 0 { print $1 }" file1 file2

# Ex2 in awkFile: $3 == 0 { print $1 }
# Ex2: Command line: awk -f awkFile file1 file2


# ### ACTION examples ###############################################
# Print whole file:
# { print }

# Print whole file also:
# { print $O }

# Print field 1 and product of field 2 and 3:
# { print $1, $2 * $3 }

# If field 3 is 0 print field 1:
# $3 == 0 { print $1 }

# NF: Number of fields (in the line):
# { print NF, $1, $NF }

# NR: Number of read lines (so far):
# { print NR, $0 }

# Print text:
# { print "Total pay for", $1, "is", $2 * $3 }

# Printf-formatted number (float) with 2 decimals:
# { printf("Total pay for %s is %.2f kr\n", $1, $2 * $3) }

# Left aligned 8 chars long, dollar sign, 6 chars wide with 2 decimals:
# { printf("%-8s $%6.2f\n", $1, $2 * $3) }


# ### SELECTION examples ############################################
# Selection by compare, print each row that match:
# $2 >= 5

# Selection by computing:
# $2 * $3 > 50 { printf("$%.2f for %s\n", $2 * $3, $1) }

# Select text (prints rows where first field equals Suzie):
# $1 == "Susie"

# Regular expressions (prints rows where Susie exists anywhere):
# /Susie/


# ### LOCICAL OPERATORS examples ####################################
# Print lines where field 2 is at least 4 - OR - field 3 is at least 20:
# $2 >= 4 || $3 >= 20

# Same as above, another way:
# !($2 < 4 && $3 < 20)


# ### DATA VALIDATION example #######################################
# Five tests on data (no errors, no output):
# NF != 3 { print $0, "number of fields is not equal to 3" }
# $2 < 4.35 { print $0, "rate is below minimum wage:", $2 }
# $2 > 10 { print $0, "rate exceeds $10 per hour" }
# $3 < 0 { print $0, "negative hours worked" }
# $3 > 60 { print $0, "too many hours worked" }


# ### BEGIN and END #################################################
# Print a head line before, empty line, the file, empty line and a footer:
# BEGIN { print "NAME RATE HOURS"; print "" }
#       { print }
# END   { print ""; print "That's it!"; }


# ### COMPUTING WITH VARIABLES examples #############################
# Calculate variable and print result:
# $3 > 15 { emp = emp + 1 }
# END     { print emp, "employees worked more than 15 hours" }

# Prints number of lines read (one employee per line):
# END { print NR, "employees" }

# Calculate average value (file must be mote than one line):
# { pay = pay + $2 * $3 }
# END { print NR, "employees"
#       print "Total pay is", pay
#       print "Average pay is", pay/NR }

# Find max value and print numeric and string variables:
# $2 > maxrate { maxrate = $2; maxemp = $1 }
# END { print "Highest hourly rate:", maxrate, "for", maxemp }

# Concatenate first field in each row and print the string:
# { names = names $1 " " }
# END { print names }

# Print last line:
# { last = $0 }
# END { print last }

# Print length of string:
# { print $1, length($1) }

# Count lines, words and characters, print concatenated result:
# { nc = nc + length($0) + 1
#   nw = nw + NF }
# END { print NR, "lines," , nw, "words,", nc, "characters" }


# ### CONTROL-FLOW STATEMENTS examples ##############################
# If-else statement:
# $2 > 4 { n = n + 1; pay = pay + $2 * $3 }
# END {
#     if (n > 0)
#         print n, "employees.",
#         "Total pay is", pay,
#         "Average pay is", pay/n
#     else
#         print "No employees are paid more than $6/hour"
# }

# While statement:
# Compute compound interest.
# Formula: Value = amount * (1 + rate) raised to years
# Usage: awk -f awk.txt (Program starts and displays an empty line)
# Input: amount interest years (3 values)
# Output: Compounded value at the end of each year
# { i = 1
#     while (i <= $3) {
#         printf("\t%.2f\n", $1 * (1 + $2) ^ i)
#         i = i + 1
#     }
# }

# For statement:
# Input: amount rate years
# Output: Compounded value at the end of each year
# { for (i = 1; i <= $3; i = i + 1)
#     printf("\t%.2f\n", $1 * (1 + $2) ^ i)
# }


# ### ARRAYS examples ###############################################
# Reverse - print input in reverse order by line:
# { line[NR] = $0 } # remember each input line
# END { i = NR # print lines in reverse order
#     while (i > 0) {
#         print "line", i, line[i]
#         i = i - 1
#     }
# }

# Same as above with for statement:
# { line[NR] = $0 }
# END { for (i = NR; i > 0; i = i - 1)
#     print "line", i, line[i]
# }


# ### USEFUL ONELINERS ##############################################
# Print the total number of input lines:
# END { print NR }

# Print the third input line:
# NR == 3

# Print the last field of every input line:
# { print $NF }

# Print the last field of the last input line:
# { field = $NF}
# END { print field }

# Print every input line with more than four fields:
# NF > 4

# Print every input line in which the last field is more than 4:
# $NF > 4

# Print the total number of fields in all input lines:
# { nf = nf + NF }
# END { print nf }

# Print the total number of lines that contain Beth:
# /Beth/ { nlines = nlines + 1 }
# END { print nlines }

# Print the line that contains the largest second field:
# $2 > max { max = $2; maxline = $0 }
# END { print maxline }

# Print every line that has at least one field:
# NF > 0

# Print every line longer than 12 characters:
# length($0) > 12

# Print the number of fields in every line followed by the line itself:
# { print NF, $0 }

# Print the first two fields, in opposite order, of every line:
# { print $2, $1 }

# Exchange the first two fields of every line and then print the line:
# { temp = $1; $1 = $2; $2 = temp; print }

# Print every line with the first field replaced by the line number:
# { $1 = NR; print }

# Print every line after erasing the second field:
# { $2 = ""; print }

# Print in reverse order the fields of every line:
# {
#     for (i = NF; i > 0; i = i - 1) {
#         printf("%-8s ", $i)
#     }
#     printf("\n")
# }

# Print the sums of the fields of every line:
# { sum = 0
#     for (i = 1; i <= NF; i = i + 1) {
#         sum = sum + $i
#     }
#     print sum
# }

# Add up all fields in all lines and print the sum:
# {
#     for (i = 1; i <= NF; i = i + 1)
#         sum = sum + $i
# }
# END { print sum }

# Print every line after replacing each field by its absolute value:
# {
#     for (i = 1; i <= NF; i = i + 1)
#         if ($i < 0)
#             $i = -$i
#     print
# }
🙂