Advertisement
Justman10000

Bash Coding

Jun 26th, 2023 (edited)
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.15 KB | None | 0 0
  1. # Below is all the code to do certain things
  2. # Note that placeholders are used here and this is a guide! Do NOT use this as a script (it will not work)!
  3. # Note also that this is a guide for Bash, so ONLY for Linux (maybe also for MacOS, also based on GNU like Linux)
  4.  
  5. # Basics
  6. ## Restart system
  7. reboot
  8. ## Login as user
  9. su userName
  10. ### Run code as other user
  11. sudo -u userName code_to_execute
  12. ## Remove file/directory
  13. rm file
  14. rm directory
  15. ### If the directory not empty
  16. rm -r directory
  17. ## Copy
  18. cp fromHere to
  19. ### Copy directory with their subdirectorys and files
  20. cp -r fromHere to
  21. ## Download files, archives, binarys etc
  22. wget url/file.name
  23. wget url/archive.name
  24. ## Run code as process
  25. code_to_execute &
  26.  
  27. # Advanced
  28. ## Declare variables
  29. variable=example
  30. ### Declare variables with code to be executed
  31. variable=$(code_to_execute)
  32. ## Fetch variable values
  33. $variable
  34. ### If you want print a value of a variable in a file
  35. ${variable}
  36. ### For terminal declared variables (rather known as arguments)
  37. $1
  38. $2
  39. $3
  40.  
  41. ## Dedect OS
  42. uname
  43. ## Dedect kernel
  44. uname -r
  45. ## Dedect hostname
  46. uname -n
  47. ## Dedect Processor
  48. uname -p
  49. ## Dedect Architecture
  50. uname -m
  51.  
  52. ## Math
  53. ### Greater than
  54. if [ 10 -gt 5 ]; then
  55.     echo "The number is greater than 5"
  56. fi
  57.  
  58. ### Smaller than
  59. if [ 5 -lt 7 ]; then
  60.     echo "The number is less than 7"
  61. fi
  62.  
  63. ### Equal
  64. if [ 5 -eq 5 ]; then
  65.     echo "The number is equal to 5"
  66. fi
  67.  
  68. ## Check for permissions and ownerships
  69. ls -lha
  70.  
  71. ## Fetch configured timezone of the system
  72. cat /etc/timezone
  73.  
  74. ## Write paragraph content in a file
  75. cat << EOF > file.txt
  76. ## Content
  77. EOF
  78.  
  79. ## Checks the existence of a PID
  80. if ( kill -0 PID ); then
  81.     # Code to execute
  82. fi
  83.  
  84. ## Checks for the existence of a specific file
  85. if [ -f fileName ]; then
  86.     # Code to execute
  87. fi
  88.  
  89. ## Checks for the existence of a specific directory
  90. if [ -d directory ]; then
  91.     # Code to execute
  92. fi
  93.  
  94. ## Checks, if a directory is writable
  95. if [ -w directory]; then
  96.     # Code to execute
  97. fi
  98.  
  99. ## Checks, if a file is executeable
  100. if [ -x file]; then
  101.     # Code to execute
  102. fi
  103.  
  104. ### You can use this to merge in a one line
  105. [ ! -d directory ] && mkdir directory
  106.  
  107. ### To check whether a directory is empty
  108. if [[ -z "$(find /pfad/zum/verzeichnis -mindepth 1)" ]]; then
  109.     echo "The directory is empty"
  110. else
  111.     echo "The directory is not empty"
  112. fi
  113.  
  114. ## Use functions (handy when you want to use a whole chain of code multiple times, or simply for the overview)
  115. ### Create functions
  116. myfunction() {
  117.     # Code_to_execute
  118. }
  119.  
  120. ### Call functions
  121. myfunction
  122.  
  123. ## Give the id of the latest runned process
  124. echo $!
  125. ### Save the id of the latest runned process
  126. echo $! > process.pid
  127.  
  128. ## Replace PID
  129. journalctl _PID=PID
  130.  
  131. ## Get all user given arguments
  132. echo $@
  133. ### Fetch, if a package exists
  134. checkPackages() {
  135.     for cmd in "$@" ; do
  136.         if command -v "$cmd" ; then
  137.             $cmd --version
  138.             return
  139.         fi
  140.     done
  141.  
  142.     echo "Not found!"
  143. }
  144.  
  145. checkPackages thePackage
  146.  
  147. ## Deleting pushed commits in git
  148. ### Starting interactive mode
  149. git rebase -i
  150. ### Delete commit locally
  151. git reset HEAD~
  152. ### Delete commit globally (in the repository)
  153. git push origin main --force --tags
  154.  
  155. # Experts
  156. ## Return the first executing process
  157. ps -p 1 -o comm=
  158.  
  159. ### Check, is used systemd or SysV-Init
  160. if [ "$(ps -p 1 -o comm=)" == "systemd" ]; then
  161.     systemctl enable app
  162.     systemctl start app
  163. else
  164.     service app enable
  165.     service app start
  166. fi
  167.  
  168. ## Set memory swap
  169. fallocate -l 4G /swapfile
  170. chmod 600 /swapfile
  171. mkswap /swapfile
  172. swapon /swapfile
  173.  
  174. ## Let's print "!" 10 times
  175. c=1
  176. while [[ "$c" -le 10 ]]; do
  177.     echo -n "!"
  178.     sleep 1
  179.     c=$(($c+1))
  180. done
  181.  
  182. ## Using getops
  183. while getopts ":a:b:c:d:" opt; do
  184.   case ${opt} in
  185.     a)
  186.       a=$OPTARG
  187.     ;;
  188.  
  189.     b)
  190.       b=$OPTARG
  191.     ;;
  192.  
  193.     c)
  194.       c=$OPTARG
  195.     ;;
  196.  
  197.     d)
  198.       d=$OPTARG
  199.     ;;
  200.  
  201.     *)
  202.       echo "Option -$OPTARG requires an argument." 1>&2
  203.       exit 1
  204.     ;;
  205.   esac
  206. done
  207.  
  208. shift $((OPTIND -1))
  209.  
  210. echo $a
  211. echo $b
  212. echo $c
  213. echo $d
  214.  
  215. ## Using while with shift
  216. while (( "$#" )); do
  217.   case "$1" in
  218.     -h | --host)
  219.       host="$2"
  220.       shift 2
  221.       ;;
  222.     -p | --port)
  223.       port="$2"
  224.       shift 2
  225.       ;;
  226.     -u | --username)
  227.       username="$2"
  228.       shift 2
  229.       ;;
  230.     -pass | --password)
  231.       password="$2"
  232.       shift 2
  233.       ;;
  234.     *)
  235.       echo "Unknown parameter passed: $1"
  236.       exit 1
  237.   esac
  238. done
  239.  
  240. echo "Host: $host"
  241. echo "Port: $port"
  242. echo "Username: $username"
  243. echo "Password: $password"
  244.  
  245. ## If this error comes:
  246. ## Unable to monitor directories for changes because iNotify max watches exceeded, add this to the end of the /etc/sysctl.conf file:
  247. fs.inotify.max_user_watches=99999
  248. ### Then run:
  249. sudo sysctl -p
  250. cat /proc/sys/fs/inotify/max_user_watches
  251.  
  252. ## Generate random strings (also usable as passwords)
  253. tr -dc A-Za-z0-9_ < /dev/urandom | head -c 32 | xargs
  254.  
  255. ## Display the IP of the mashine (or computer)
  256. ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -1
  257.  
  258. ## Delete everything in the file file.txt after a :
  259. grep -E ".*:.*" file.txt | sed -i 's/:[^:]*//g' file.txt
  260.  
  261. ## Replace test with test2 in example.txt
  262. sed -i "s|test|test2|" example.txt
  263.  
  264. ## Check for memory (in KiloByte)
  265. awk '/MemTotal/ {print $2}' /proc/meminfo
  266.  
  267. ## Format your echo texts coloured
  268. echo -e "\e[31mThis text is red\e[0m"
  269.  
  270. ### All colors
  271. #### Black: \e[30m
  272. #### Red: \e[31m
  273. #### Green: \e[32m
  274. #### Yellow: \e[33m
  275. #### Blue: \e[34m
  276. #### Magenta: \e[35m
  277. #### Cyan: \e[36m
  278. #### White: \e[37m
  279.  
  280. ### Or
  281. getColoured() {
  282.     tput setaf 1
  283.     echo "$@"
  284.     tput sgr0
  285. }
  286.  
  287. getColoured test
  288.  
  289. #### All colors (Put after "tput setaf")
  290. ##### Light red: 1
  291. ##### Green: 2
  292. ##### Yellow: 3
  293. ##### Blue: 4
  294. ##### Purple: 5
  295. ##### Light blue: 6
  296. ##### White: 7
  297. ##### Grey: 8
  298. ##### Red: 9
  299.  
  300. ## Running code based on selected option
  301. options=(
  302.     "Option 1"
  303.     "Option 2"
  304. #   ...
  305. )
  306.  
  307. select opt in "${options[@]}"
  308. do
  309.     case $opt in
  310.         "Option 1")
  311.             # Code for option 1
  312.         ;;
  313.  
  314.         "Option 2")
  315.             # Code for option 2
  316.         ;;
  317.  
  318. #       ...
  319.     esac
  320. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement