Advertisement
Justman10000

Bash Coding

Jun 26th, 2023 (edited)
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.05 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. ## Let's print "!" 10 times
  169. c=1
  170. while [[ "$c" -le 10 ]]; do
  171.     echo -n "!"
  172.     sleep 1
  173.     c=$(($c+1))
  174. done
  175.  
  176. ## Using getops
  177. while getopts ":a:b:c:d:" opt; do
  178.   case ${opt} in
  179.     a)
  180.       a=$OPTARG
  181.     ;;
  182.  
  183.     b)
  184.       b=$OPTARG
  185.     ;;
  186.  
  187.     c)
  188.       c=$OPTARG
  189.     ;;
  190.  
  191.     d)
  192.       d=$OPTARG
  193.     ;;
  194.  
  195.     *)
  196.       echo "Option -$OPTARG requires an argument." 1>&2
  197.       exit 1
  198.     ;;
  199.   esac
  200. done
  201.  
  202. shift $((OPTIND -1))
  203.  
  204. echo $a
  205. echo $b
  206. echo $c
  207. echo $d
  208.  
  209. ## Using while with shift
  210. while (( "$#" )); do
  211.   case "$1" in
  212.     -h | --host)
  213.       host="$2"
  214.       shift 2
  215.       ;;
  216.     -p | --port)
  217.       port="$2"
  218.       shift 2
  219.       ;;
  220.     -u | --username)
  221.       username="$2"
  222.       shift 2
  223.       ;;
  224.     -pass | --password)
  225.       password="$2"
  226.       shift 2
  227.       ;;
  228.     *)
  229.       echo "Unknown parameter passed: $1"
  230.       exit 1
  231.   esac
  232. done
  233.  
  234. echo "Host: $host"
  235. echo "Port: $port"
  236. echo "Username: $username"
  237. echo "Password: $password"
  238.  
  239. ## If this error comes:
  240. ## Unable to monitor directories for changes because iNotify max watches exceeded, add this to the end of the /etc/sysctl.conf file:
  241. fs.inotify.max_user_watches=99999
  242. ### Then run:
  243. sudo sysctl -p
  244. cat /proc/sys/fs/inotify/max_user_watches
  245.  
  246. ## Generate random strings (also usable as passwords)
  247. tr -dc A-Za-z0-9_ < /dev/urandom | head -c 32 | xargs
  248.  
  249. ## Display the IP of the mashine (or computer)
  250. ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -1
  251.  
  252. ## Delete everything in the file file.txt after a :
  253. grep -E ".*:.*" file.txt | sed -i 's/:[^:]*//g' file.txt
  254.  
  255. ## Replace test with test2 in example.txt
  256. sed -i "s|test|test2|" example.txt
  257.  
  258. ## Check for memory (in KiloByte)
  259. awk '/MemTotal/ {print $2}' /proc/meminfo
  260.  
  261. ## Format your echo texts coloured
  262. echo -e "\e[31mThis text is red\e[0m"
  263.  
  264. ### All colors
  265. #### Black: \e[30m
  266. #### Red: \e[31m
  267. #### Green: \e[32m
  268. #### Yellow: \e[33m
  269. #### Blue: \e[34m
  270. #### Magenta: \e[35m
  271. #### Cyan: \e[36m
  272. #### White: \e[37m
  273.  
  274. ### Or
  275. getColoured() {
  276.     tput setaf 1
  277.     echo "$@"
  278.     tput sgr0
  279. }
  280.  
  281. getColoured test
  282.  
  283. #### All colors (Put after "tput setaf")
  284. ##### Light red: 1
  285. ##### Green: 2
  286. ##### Yellow: 3
  287. ##### Blue: 4
  288. ##### Purple: 5
  289. ##### Light blue: 6
  290. ##### White: 7
  291. ##### Grey: 8
  292. ##### Red: 9
  293.  
  294. ## Running code based on selected option
  295. options=(
  296.     "Option 1"
  297.     "Option 2"
  298. #   ...
  299. )
  300.  
  301. select opt in "${options[@]}"
  302. do
  303.     case $opt in
  304.         "Option 1")
  305.             # Code for option 1
  306.         ;;
  307.  
  308.         "Option 2")
  309.             # Code for option 2
  310.         ;;
  311.  
  312. #       ...
  313.     esac
  314. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement