Skip to content

sahilrajput03/learning-bash

Repository files navigation

Readme

Quick Links:

Running these scripts:

  • Use Code Runner (VsCode Extension) - Google Doc
  • Using bmon from the my_bin repository (or using bm)

Format .bash and .sh file on save in VsCode

Just install this extension: https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format and turn the default format in vsocde setting as 'null', so that appropriate formatters will be chosen for appropriate files.

Code Generated via AutoDocs

File - 0.sh

Simple echo example.

#!/bin/bash
# set -x
echo Hello, from example.

File - 1.sh

#!/bin/bash
echo Hello i am sahil.

File - 2.sh

#!/bin/bash
var=~/Desktop
cd $var
ls

File - 3.sh

#/usr/bin/env bash
echo $UID
echo "If you run the script via 'sudo' prefixed you would see '0' value because
# that denotes if the program is initiaed as root access or not."
echo
echo "If the user executing the script has root permissions by default then wihout
sudo the output would be '0'"

File - 4.sh

#!/bin/bash
ROOT_UID=0 # Only users with $UID 0 have root privileges.

if [ "$UID" -ne "$ROOT_UID" ]; then
  echo "Must be root to run this script."
  exit $E_NOTROOT
fi

echo Congrats you are root user.

File - 5.sh

#!/bin/bash
ROOT_UID=0
if [ "$UID" = "$ROOT_UID" ]; then
  echo "Yes, you have root permissions!"
fi

if [ "$UID" != "$ROOT_UID" ]; then
  echo "No, you dont have root permissions!"
fi

if [ "$UID" -ne "$ROOT_UID" ]; then # -ne is same as !=
  echo "No, you dont have root permissions! (WITH '-ne')"
fi

if [ "$UID" = 1000 ]; then # POSIX sh
  echo "You are a simple user with UID as '1000'."
fi

if [ "$UID" == 1000 ]; then # bash specific
  echo "You are a simple user with UID as '1000'."
fi

# A phenomenal usage:
# https://stackoverflow.com/a/40431336/10012446

File - 6.sh

#!/bin/bash

echo Read this amazing answer on stackoverflow: https://stackoverflow.com/a/40431336/10012446
echo "There is lots of way to compare value like string and numbers differenctly and using the C way i.e., == operator as well."

File - 7.sh

#!/bin/bash
#This file demonstrates simple string manipulation with variables.
echo -----
A="Sahil" 				#Note you should ALWAYS use quotes around text when assigning it to variable.
B="${A} Rajput"  	#This is how we add text to string in bash.

echo $A
echo $B

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages