Basics of Shell scripting

Basics of Shell scripting

and a little script that does something cool

How to set the file

  1. First create a .sh file.

  2. Start the first line with #! /bin/bash. This tells that you want to use bash to execute this script

  3. Then write the code according to syntax

  4. Then go into the shell and type chmod u+x "filename" to make the script executable.

  5. Execute the script using bash "filename"

  6. To add the script to usual commands of your shell, that is you can execute it no-matter where you are by directly typing a specific command, you have to create an alias for it. which is done as following

    • Press cd to go to root directory

    • Then type vim .bashrc to enter the bash file

    • Then add a line as following: alias command_name="bash full path of the script"

    • To make this easier you create a separate folder to store all the scripts.

    • Once you restart the terminal, all newly added alias commands can be executed from anywhere you are.

Syntax

  1. # is used to provide comments.

  2. To read data from the terminal and store it in a variable, use the following read variable_name To add a custom msg type read -p "Message" variable_name

  3. $@ represents the position of command arguments starting from 1. The command itself is argument 0.
    That is,

     #!/bin/bash
     for x in $@
     do
      echo "Entered arg is $x"
      done
    
  4. In a script $1 denotes first argument, $2 denotes 2nd argument.

  5. To reference a variable we must put $ before the variable name.

  6. A variable name should start with either an underscore or a letter. It can contain numbers too.

  7. use the echo "text" command to print text to the terminal.

  8. echo "text" > file.txt to overwrite a file. To append to the end of a file use >> instead. OR if you want other commands to save the output to a file, you do command > text.txt.

To take input we do command < source_file

The std input output and error are indexed as 0,1 and 2 respectively.

9. To print the output of a command or assign it to a variable we do this:

var = `command`
  1. We can use wc command to find the number of words in a file.

  2. To use conditionals and loops

    if [condition]; then
    statement
    elif [condition]; then
    statement 
    else
    default stuff
    fi
    

    -a means && -o means || -gt means > -lt means <. -eq means == -ne means !=

    While loop
     ```bash
     #!/bin/bash
     i=1
     while [[ $i -le 10 ]] ; do
     echo "$i"
     (( i += 1 ))
     done
    

    For loop

    #!/bin/bash
    
    for i in {1..5}
    do
    echo $i
    done
    

    We can also loop strings for X in red green blue

    Note: Remember to add spaces after the brackets, that is [[ condition ]] rather than [[condition]]. This small change caused me a lot of headache.

Debugging

Write set -x at the beginning of your code to enable debugging mode.

You could also use CRON to schedule tasks, though I haven't used it yet. This is mainly a Linux thing.

Short example

So very recently I was trying out the SSH connection between my android devices and my desktop.
Basically, you need to setup a username and password for your device which you want to access, then use those in the device you want to connect from.

So if you want to access your android device from your desktop, you setup sshd on the android device and use ssh on your desktop.

I have written a short bash script to automate this process, as follows, it also allows to setup sftp interactive mode:

#! /bin/bash
#! /bin/bash

read -p "1. Login 2.copy " choice
read -p "press 1 for tab, 2 for mobile " device

if [[ $choice -eq 1 ]]; then
    if [[ $device -eq 1 ]]; then
        echo "Connecting to Tablet"
        ssh username@ip-address -p 8022
    elif [[ $device -eq 2 ]]; then
        echo "Connecting to Mobile"
        ssh username@ip-address -p 8022
    else
        echo "Wrong Choice"
    fi
elif [[ $choice -eq 2 ]]; then
    if [[ $device -eq 1 ]]; then
        echo "Connecting to Tablet"
        sftp -P 8022 username@ip-address
    elif [[ $device -eq 2 ]]; then
        echo "Connecting to Mobile"
        sftp -P 8022 username@ip-address
    else
        echo "Wrong Choice"
    fi
fi

You can add as many devices as you want to this. This was turned into a system command using the steps mentioned in the very beginning.

There are some other steps like setting up a password in the remote device or setting up the ssh keys, but this simple script does the job thereafter.

Parting words

Once you get a hang of shell scripting, you can try out linux challenges such as Over the wire CTF.

I am a newbie in this myself, so I hope this little post helps you get started on this.

Resources

https://www.freecodecamp.org/news/how-to-search-files-effectively-in-linux/

https://missing.csail.mit.edu/2020/shell-tools/