How to set the file
First create a
.sh
file.Start the first line with
#! /bin/bash
. This tells that you want to use bash to execute this scriptThen write the code according to syntax
Then go into the shell and type
chmod u+x "filename"
to make the script executable.Execute the script using
bash "filename"
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 directoryThen type
vim .bashrc
to enter the bash fileThen 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
#
is used to provide comments.To read data from the terminal and store it in a variable, use the following
read variable_name
To add a custom msg typeread -p "Message" variable_name
$@
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
In a script
$1
denotes first argument,$2
denotes 2nd argument.To reference a variable we must put
$
before the variable name.A variable name should start with either an underscore or a letter. It can contain numbers too.
use the
echo "text"
command to print text to the terminal.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 docommand > 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`
We can use
wc
command to find the number of words in a file.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/