Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Scripting. Linux Shell Scripts

First line headers

Bourne Shell (sh) #!/bin/sh Bourne Again Shell (bash) #!/bin/bash C-Shell (csh) #!/bin/csh Korn Shell (ksh) #!/bin/ksh Perl #!/usr/bin/perl PHP #!/usr/bin/php Python #!/usr/bin/python Python3 #!/usr/bin/env python3 Turbo Shell (tcsh) #!/usr/bin/tcsh TCL/Tk #!/usr/bin/wish -f True #!/bin/true # Do not execute the script and exit with a Success (0) exit status False #!/bin/false # Do not execute the script and exit with a Failure (1) exit status Unless you expressely need to code for a specific shell, your choice of header should be #!/bin/bash (that is, Bourne Again Shell (bash)) Set the executable flag: chmod +x FILE # declares the file as executable

# Parameters

if [ "$#" -eq 0 ] || [ -z "$1" ]; then echo "Missing parameters"; fi

# Variables

varn=1 # number (floating or integer) vars="hi" # string of characters echo $varn echo $vars

# Arrays

arr=(10 4 5 20 8) echo "Array Length: ${#arr[@]}"; echo -n "Highest number in array: "; printf "%d\n" "${arr[@]}" | sort -nr | head --lines=1 echo -n "Lowest number in array: "; printf "%d\n" "${arr[@]}" | sort -nr | tail --lines=1 msga[0]="Hello"; msga[1]="World"; msgb=("Hello" "World") msgc=([3]="Hello" [5]="World") echo "${msga[0]} ${msga[1]}" echo "${msgb[0]} ${msgb[1]}" echo "${msgc[3]} ${msgc[5]}" printf '%s\n' "${arr[@]}" # print the whole content of the array arr, separated by new lines printf '%s, ' "${arr[@]}" # print the whole content of the array arr, separated by commas and spaces (note that there will be a comma and a space after the last element in the array too) printf '%s, ' "${arr[@]}" | rev | cut -c 3- | rev # print the whole content of the array arr, separated by commas and spaces and removing the trailing comma and space if [ ${#array[@]} -ge 1 ]; then for i in $( seq $(( ${#array[@]} - 1 )) ); do array[$i]="$( echo ${array[$i]} | sed 's/[^0-9]*//g' ) "; done; fi # remove all characters but integer numbers (0-9) from all values in the array

# Functions

# Declare a function with function function helloworld { echo "Hello World"; } # Declare a function with round brackets hellow () { echo "Hello World"; } # Declare a function that accepts parameters helloany () { echo "Hello $1"; } # Return a value from a function function sum() { local r=0; if [ $# -ge 1 ]; then for i in "$@"; do local r=$(( $r+$i )); done echo -n $r; return 0; else echo -n $r; return 1; fi } # Call functions helloworld hellow helloany "everybody" echo $( sum 3 4 2 ); echo $( sum 5 5 ); echo $( sum 2 0 ); echo $( sum 6 ); echo $( sum );

# Branching

# Branching with if ... then ... elif ... else ... fi

# Check if a variable is set

if [ -z ${v1+x} ]; then echo "var is unset"; else echo "var is set. Value: $v1"; fi

# Check if a variable is empty

if [ -z "$v1" ]; then echo "The variable is empty"; fi if [ -n "$v1" ]; then echo "The variable is NOT empty"; fi if [ ! -z "$v1" ]; then echo "The variable is NOT empty"; fi if [ ! -n "$v1" ]; then echo "The variable is empty"; fi

# Compare boolean

if ( $v1 ); then echo "TRUE"; fi if ( ! $v1 ); then echo "NOT TRUE (that is, FALSE)"; fi

# Compare numbers

$v1=5; $v2=8; if [ $v1 -eq $v2 ]; then echo "$v1 is equal to $v2"; fi if [ $v1 -ne $v2 ]; then echo "$v1 is not equal to $v2"; fi if [ $v1 -gt $v2 ]; then echo "$v1 is greater than $v2"; fi if [ $v1 -ge $v2 ]; then echo "$v1 is greater than or equal to $v2"; fi if [ $v1 -lt $v2 ]; then echo "$v1 is less than $v2"; fi if [ $v1 -le $v2 ]; then echo "$v1 is less than or equal to $v2"; fi if (( "$v1" == "$v2" )); then echo "$v1 is equal to $v2"; fi if (( "$v1" != "$v2" )); then echo "$v1 is not equal to $v2"; fi if (( "$v1" > "$v2" )); then echo "$v1 is greater than $v2"; fi if (( "$v1" >= "$v2" )); then echo "$v1 is greater than or equal to $v2"; fi # >= and <= don't work with [[ ]] if (( "$v1" < "$v2" )); then echo "$v1 is less than $v2"; fi if (( "$v1" <= "$v2" )); then echo "$v1 is less than or equal to $v2"; fi # >= and <= don't work with [[ ]] if (( "$v1" == "$v2" )); then echo "$v1 is equal to $v2"; elif (( "$v1" < "$v2" )); then echo "$v1 is less than $v2"; else echo "$v1 is greater than $v2"; fi

# Compare strings

$v1="hello"; $v2="world"; if [[ "$v1" == "$v2" ]]; then echo "$v1 is equal to $v2"; fi if [[ "$v1" != "$v2" ]]; then echo "$v1 is not equal to $v2"; fi if [[ "$v1" == "$v2"* ]]; then echo "$v1 begins with $v2"; fi # Wildcards don't work with (( )) if [[ "$v1" == *"$v2" ]]; then echo "$v1 ends with $v2"; fi # Wildcards don't work with (( )) if [[ "$v1" == *"$v2"* ]]; then echo "$v1 contains $v2"; fi # Wildcards don't work with (( )) if [[ "$v1" == *"$v2"* ]] && [[ "$v1" != *"$v2" ]] && [[ "$v1" != "$v2"* ]]; then echo "$v1 contains with $v2, but not at the beginning or at the end."; fi # Wildcards don't work with (( )) if (echo "$v1" | grep -Eq $regex); then echo "$v1 matches $regex"; fi # This is the POSIX way to match Regular Expressions. if [[ "$v1" =~ $regex ]]; then echo "$v1 matches $regex"; fi # This is Bash specific to match Regular Expressions. If you get a "[[: not found" error in your script, remember to add #!/bin/bash at the top of your script. if [[ "$v1" > "$v2" ]]; then echo "$v1 is greater than $v2"; fi if [[ "$v1" < "$v2" ]]; then echo "$v1 is less than $v2"; fi if [[ "$v1" == "$v2" ]]; then echo "$v1 is equal to $v2"; else echo "$v1 is not equal to $v2"; fi

# Compare versions

v1="3.50.2"; v2="4.2.3"; if [[ "$v1" == "$v2" ]]; then echo "$v1 is equal to $v2"; fi if [[ "$(sort --version-sort <<< "$(printf '%s\n' "$v1" "$v2")" | head --lines=1)" = "$v1" ]]; then echo "$v1 is lower than or equal to $v2"; fi if [[ "$(sort --version-sort <<< "$(printf '%s\n' "$v1" "$v2")" | head --lines=1)" != "$v1" ]]; then echo "$v1 is higher than $v2"; fi if [[ "$v1" == "$v2" ]]; then echo "$v1 is equal to $v2"; elif [[ "$(sort --version-sort <<< "$(printf '%s\n' "$v1" "$v2")" | head --lines=1)" = "$v1" ]]; then echo "$v1 is lower than $v2"; else echo "$v1 is higher than $v2"; fi

# File System

if [ -e $v1 ]; then echo "$v1 exists as a File or a Directory"; fi if [ -f $v1 ]; then echo "$v1 is a File"; fi if [ -d $v1 ]; then echo "$v1 is a Directory"; fi if [ -L $v1 ]; then echo "$v1 is a SymLink"; fi if [ -s $v1 ]; then echo "$v1 file size is greater than 0"; fi if [ -N $v1 ]; then echo "$v1 was modified after its last read"; fi if [ -O $v1 ]; then echo "$v1 is owned by current user"; fi if [ -G $v1 ]; then echo "$v1 is owned by current user's group"; fi if [ -r $v1 ]; then echo "$v1 has Read permission"; fi if [ -w $v1 ]; then echo "$v1 has Write permission"; fi if [ -x $v1 ]; then echo "$v1 has Execute permission"; fi if [ $v1 -ef $v2 ]; then echo "$v1 and $v2 are hard links to the same file"; fi if [ $v1 -nt $v2 ]; then echo "$v1 is newer than $v2"; fi if [ $v1 -ot $v2 ]; then echo "$v1 is older than $v2"; fi

# Check for File or Directory

if [[ -d $v ]]; then echo "Directory" elif [[ -f $v ]]; then echo "File" else echo "Not found" fi

# Branching with case ... esac

# Config onekeyc=true; # Single Keypress for menu choice, rather than having to hit Enter echo -n "Enter choice "; if ( $onekeyc ); then read -r -n 1 -p "" choice # Key press echo ; else read -r -p "" choice # Type choice and hit Enter fi case $choice in "1") echo "1" ;; "2") echo "2" ;; "q"|"Q"|"x"|"X") echo "Q or X, upper or lower case" ;; *) echo "Default" ;; esac

# Looping

# Looping with for

for i in 1 2 3 4 5 do echo "N. $i" done echo for i in {1..5} do echo "N. $i" done echo for (( i=1; i<=5; ++i )) do echo "N. $i" done echo for (( i=1; i<=10; i=i+2 )) do echo "N. $i" done echo # Bash v4.0 and greater support step for i in {1..10..2} do echo "N. $i" done echo # Before than Bash v4.0 step could be achieved with the additional seq command for i in $(seq 1 2 10) do echo "N. $i" done echo # List all elements in given list i=0; for el in Hello World !; do i=$(( i + 1 )) echo "Element N. $i: $el" done echo # List all elements in given list in a variable str="Hello World !" i=0; for el in $str; do i=$(( i + 1 )) echo "Element N. $i: $el" done echo # List all elements in given array arr=("Hello" "World" "!") i=0; for el in "${arr[@]}"; do i=$(( i + 1 )) echo "Element N. $i: $el" done # List all files in PATH for file in PATH/* ; do echo $file done for (( ; ; )) do echo "infinite loop (kill the process, or terminate it with CTRL+C)" done

# Looping with while

i=1 while (( $i <= 5 )) do echo "N. $i" (( ++i )) done echo i=0 while (( $i <= 10 )) do i=$(( i + 2 )) if (( i == 4 )); then continue; fi echo "N. $i" if (( i >=8 )); then break; fi done echo while : do echo "infinite loop (kill the process, or terminate it with CTRL+C)" done

# Looping with until

i=1 until (( $i > 5 )) do echo "N. $i" (( ++i )) done echo i=0 until (( $i > 10 )) do i=$(( i + 2 )) if (( i == 4 )); then continue; fi echo "N. $i" if (( i >=8 )); then break; fi done echo until false : do echo "infinite loop (kill the process, or terminate it with CTRL+C)" done

Invoke Linux shell commands from PHP

PHP can perform calls to the Linux shell using the shell_exec() function: $output = shell_exec('ls -laF'); echo '<pre>'.$output.'</pre>'; or enclosing the Linux shell command between backticks: $output = `ls -laF`; echo '<pre>'.$output.'</pre>'; PHP also has a system() function that returns the value returned by the Linux shell command and the last line of output of such command: $last_line = system('ls -laF', $returned_value);




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 04:49 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer