Coder Social home page Coder Social logo

bashscripting's Introduction

Bash Scripting

Printint

  • echo <txt> print text with a new line. add -n to make it without new line.
  • printf <txt> print whithout a new line by default.
  • When defining a variable, do not make spaces before or after.
x="shady" => OK
x ="shady" => NOK
x = "shady" => NOK

Taking an input form user

echo "Enter your name"
read name
echo "Welcome again ${name}"

To not show the input value

read -s name

To print a message whilr taking an input

read -p "Your namea again: " name2
echo "Welcome again ${name2}"

We can compine two commands together

read -sp "Enter password: " password
echo "Welcome again ${password}"

For loop

# set -x and set +x : for debugging
set -x 
for i in {1..10}
do
echo ${i}
done
set +x

This will loop for each word in the string not each char.

str="bash script course"
for i in ${str}
do
echo ${i}
done

Read an output of a command

# var=ls #var = "ls"
var=$(ls) # This mean to execute the command the get the output
#same as var=`ls`
echo ${var}

Get the files in this dir

files=$(ls)
for i in ${files}
do
    echo ${i}
done

&& and || in bash

(&&) means if this command is done successfully then print echo. And if the command failed, don't print.

ls && echo success
lsls && echo success

The opisite is ||

ls || echo success
lsls || echo success

test in bash

If the condition is true then execute commande after && and ignore commands after ||.

You can also compare integers but you need to use different flags like -eq. type man test to see.

You can also check for a file if it exist or a dir and somethongs like that just see the manual.

test "shady" = "shady" || echo apple || echo tomato
test "shady" = "shady" || echo apple && echo tomato
test "shady" = "shady" && echo apple || echo tomato
test "shady" = "shady" && echo apple && echo tomato

If in bash

# These three spaces must be written
  # #                #
if [ -f "./README.md" ]
then
    echo "file is here!"
else
    echo "File is not here"
fi

It's tes equivelant

test -f "README.md" && echo "File is here!" || echo "File is not here"

Argumants

echo $# # The number of args
echo $* # args as string
echo $@ # args as list
echo $0 # psth to this script
echo $1 # first arg
echo $$ # process id
echo $? # status of the last line as a number

Another example

args=$@
count=$#
echo $args
for i in ${args}
do
    echo ${i}
done

Dealing with delimiters

str="shady;Nabil;Mohamed"
# Setting ';' as a delimiter
IFS=';'
read -a NAME <<< ${str}
for i in ${NAME[@]}
do
    echo ${i}
done

Functions

add_list()
{
    echo "Processing list: $@"
    result=$((0))
    for i in $@
    do
        result=$(($result + ${i}))
    done
    return $result;
}
add_list $@
echo $?

Important note

When using "$@", the arguments are preserved as separate entities, even if they contain spaces. When using $@ without double quotes, the arguments are split based on whitespace, resulting in unexpected behavior when arguments contain spaces.

Both are the same when not use any "" in the input.

echo "Using \"\$@\":"
for arg in "$@"; do
    echo "$arg"
done

echo "Using \$@:"
for arg in $@; do
    echo "$arg"
done

Arrays

array=("shady" "nabil" "mohamed")
# Print array elements
echo ${array[@]}
# Print the number of elements in the array
echo ${#array[@]}
# the array here is 1-based
subArray=(${array[@]:1:2})
echo ${subArray[@]}

strings VS Arrays

# Strings are zero based.
nmStr="Shady Nabil Mohamed"
# 5 is the number of chars wanted to be printed
sbStr=${nmStr:6:5}
echo ${sbStr}

select

select lang in cpp python java result
do
    echo $lang
    case $lang in
    "cpp")
        echo "Good choise"
        ;;
    *)
        break
        ;;
    esac
done

readonly with vars and functions

x=10
echo $x
x=20
echo $x
# readonly x
x=30 # Error

fun()
{
    echo "First call"
}

fun
readonly -f fun # Error
fun()
{
    echo "second call"
}

fun

bashscripting's People

Contributors

shadynabil8 avatar

Watchers

 avatar

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.