onsa
1/19/2017 - 11:16 AM

General Linux administration info

General Linux administration info

#!/bin/bash

declare -i INTEGER

VARIABLENAME="value"
let VARIABLENAME="value"

# round number
let a=44234.21
printf %.0f a         # the number after %. determines the number of decimals

# -e allows backslash escapes
# \c keeps cursor in same line
echo -e "... \c "

# single quotes suppress special characters
echo '$1'     # literally $1 is printed, not an argument's value
#!/bin/bash

tr 'abcde' 'fghij'      # a => f, b => g, c => h, d => i, e => j
> filename    # truncates file to zero length orcreates zero-length file

>>  filename  # creates the file if not present, otherwise appends to it

2>1           # redirects stderr to stdout
2>&1          # redirects stderr to wherever stdout is pointing
#!/bin/bash

# first the inner command is executed, i.e. the first parameter is echoed
# then that value is used for renaming a file
mv filename `echo $1`
#!/bin/bash

for (( i=0;i<$LIMIT;i++)); do
  [some command]
done

COUNT=6
while [ $COUNT -gt 0 ]; do
	echo Value of count is: $COUNT
	let COUNT=COUNT-1
done

until [ $COUNT -gt 5 ]; do
  echo Value of count is: $COUNT
  let COUNT=COUNT+1
done 
#!/bin/bash

read variablename       # if no variable name is given, value is stored in $REPLY
echo $variablename

read several words
echo $several $words

select variablename in 'this' 'that'
do
  echo $variablename
  break
done
#!/bin/bash

function functionname {
  local VARIABLENAME="local variable"
  echo $1
}

functionname 'parameter'
#!/bin/bash

# bash convert binary number 10001
result=2#10001
echo $result

# bash convert octal number 16
result=8#16
echo $result

# bash convert hex number 0xE6A
result=16#E6A
echo $result 
#!/bin/bash

directory="./BashScripting"

# bash check if directory exists
if [ -d $directory ]; then        # space between brackets necessary
	echo "Directory exists"
else 
	echo "Directory does not exists"
fi

case $variablename in
    1) echo 'a';;
    2) echo "b";;
    3) echo "c";;
    4) echo "d";;
esac 

# arithmetic comparison
# -lt 	<
# -gt 	>
# -le 	<=
# -ge 	>=
# -eq 	==
# -ne 	!=

# string comparison
# = 	equal
# != 	not equal
# < 	less then
# > 	greater then
# -n s1 	string s1 is not empty
# -z s1 	string s1 is empty

# file testin
# -b filename 	Block special file
# -c filename 	Special character file
# -d directoryname 	Check for directory existence
# -e filename 	Check for file existence
# -f filename 	Check for regular file existence not a directory
# -G filename 	Check if file exists and is owned by effective group ID.
# -g filename 	true if file exists and is set-group-id.
# -k filename 	Sticky bit
# -L filename 	Symbolic link
# -O filename 	True if file exists and is owned by the effective user id.
# -r filename 	Check if file is a readable
# -S filename 	Check if file is socket
# -s filename 	Check if file is nonzero size
# -u filename 	Check if file set-ser-id bit is set
# -w filename 	Check if file is writable
# -x filename 	Check if file is executable
# bash parsing preference (only first found is run)

  ~/.bash_profile
  ~/.bash_login
  ~/.profile
#!/bin/bash

# -a reads into an array
read -a arrayname

declare -a ARRAYNAME
ARRAYNAME=('first element', 'second element', third fourth)
# number of elements
echo ${#ARRAYNAME[@]}
#!/bin/bash

echo $1 $2 $3
# cast arguments into an array
argumentarray=("$@")
echo $@   OR  ${args[0]} ${args[1]} ${args[2]}