GithubHelp home page GithubHelp logo

bashtime's Introduction

Arguments

Syntax: 
	./file.sh arg1 arg2 ....
Use:
	if [[ -d $1 ]] ---> here $1 is arg1

Variables

Syntax: 
	variableName="value"
Print
	echo $variableName

Conditionals

Syntax:
	if [ $index -lt 5 ]
	then
		echo $index
	else
		echo "Nothing"
	fi


Equal: -eq
Not equal: -ne
Less than or equal: -le
Less than: -lt
Greater than or equal: -ge
Greater than: -gt
Is null: -z

------> When comparing strings use "double quotes" to prevent errors:

For String: 
	echo "Please enter username: "
	read name
	echo $name
	if [ "$name" == "harsh" ];
	then
        echo "Welcom back harsh"
	else
        echo "Welcome not back"
	fi

For Numeric:
	test=3
	if [ $test -lt 44 ]
	then 
	        echo "Harsh"
	else
	        echo "Rahul"
	fi

	if [ $a -eq $b ]
	then
	        echo "You are root"
	elif [ $a -lt $b ]
	then
	        echo "You are god"
	else
	        echo "You are not root"
	fi

Note that while using logical operator use :

[[double brackets ]]
	if [[ $a -eq $b && $a -eq 3 ]]
	then
	        echo "a is 3"
	fi

Bash test:

  • File commands

    • "-a" FILE True if file exists
    • "-b" FILE true if a file is a block special
    • "-c" FILE true if a file is character special
    • "-d" FILE true if a file is a directory
    • "-e" FILE true if a file exists
    • "-f" FILE true if a file exists and regular file
    • "-g" FILE true if a file is a set-group-id
    • "-nt" FILE is newer than
    • "-ot" FILE is older than
    • "-r" FILE is readable
    • "-w" FILE can be modified
    • "-x" FILE can be executed
    • "-h" FILE exists and is a symbolic link (same as -L) extra
       -k FILE
              FILE exists and has its sticky bit set
    
       -L FILE
              FILE exists and is a symbolic link (same as -h)
    
       -N FILE
              FILE exists and has been modified since it was last read
    
       -O FILE
              FILE exists and is owned by the effective user ID
    
       -p FILE
              FILE exists and is a named pipe
    
       -r FILE
              FILE exists and read permission is granted
    
       -s FILE
              FILE exists and has a size greater than zero
    
       -S FILE
              FILE exists and is a socket
    
       -t FD  file descriptor FD is opened on a terminal
    
       -u FILE
              FILE exists and its set-user-ID bit is set
    
       -w FILE
              FILE exists and write permission is granted
    
       -x FILE
              FILE exists and execute (or search) permission is granted
    
    FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2
    
       	FILE1 -ot FILE2
              FILE1 is older than FILE2
    
         
    
  • Number commands

    • "-eq" equal
    • "-ne" not equal
    • "-lt" less than
    • "-le" less than or equal
    • "-gt" greater than
    • "-ge" greater then or equal
  • String commands

    • "=" equal
    • "!=" not equal
    • "-z" is empty
    • "-n" is not empty

EXPR

  • Add = +
  • Sub = -
  • Mul = *
  • Div = /
  • less then = <
  • equal = =
  • not equal = !=
  • substr = expr substr $var 2 3: exctract 3 char from 2 index
  • length = expr length $var: give length of var Use: var=expr $x + $y

Case:

Syntax:
	case EXPRESSION in

	  PATTERN_1)
	    STATEMENTS
	    ;;

	  PATTERN_2)
	    STATEMENTS
	    ;;

	  PATTERN_N)
	    STATEMENTS
	    ;;

	  *)
	    STATEMENTS
	    ;;
	esac

Loops:

Syntax:
- [FOR]()
	for((i=0;i<k;i++))
	do
		##
	done
	for a in $str
	do
		echo $a
	done
- [WHILE]()
	while [ $a -lt $k ]
	do
		echo $a
		((a++))
	done

bashtime's People

Contributors

harsh4999 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

mercor-projects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.