If Statements in Bash

Basic If Statements

#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]
then
echo Hey that\'s a large number.
pwd
fi
date

If Else

#!/bin/bash
# else example
if [ $# -eq 1 ]
then
nl $1
else
nl /dev/stdin
fi

If Elif Else

#!/bin/bash
# elif statements
if [ $1 -ge 18 ]
then
echo You may go to the party.
elif [ $2 == 'yes' ]
then
echo You may go to the party but be back before midnight.
else
echo You may not go to the party.
fi

Boolean Operations

#!/bin/bash
# and example
if [ -r $1 ] && [ -s $1 ]
then
echo This file is useful.
fi
#!/bin/bash
# or example
if [ $USER == 'bob' ] || [ $USER == 'andy' ]
then
ls -alh
else
ls
fi

Multiple logical operators, ((A || B) && C)

use double brackets

if [[ ($A -eq 0 || $B -ne 0) && $C -eq 0 ]]; then …

References
https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php
https://unix.stackexchange.com/questions/290146/multiple-logical-operators-a-b-c-and-syntax-error-near-unexpected-t

User Input in Bash

Ask the User for Input

#!/bin/bash
# Ask the user for their name
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname

More variables

#!/bin/bash
# Demonstrate how read actually works
echo What cars do you like?
read car1 car2 car3
echo Your first car was: $car1
echo Your second car was: $car2
echo Your third car was: $car3

More with Read

#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
echo Thankyou $uservar we now have your login details

-p allows you to specify a prompt and -s makes the input silent

References
https://ryanstutorials.net/bash-scripting-tutorial/bash-input.php

Start and Stop Softether from command line on linux

extute ip route show and compare route table before and after connecting to the vpn server to get all required addresses

start.sh

#!/bin/sh

[ "$UID" -eq 0 ] || exec sudo "$0" "$@"

vpnclient start
sleep 2s
vpncmd localhost /client /CMD AccountConnect [ConnectionName]
dhclient [VPN_Adapter]
ip route add [VPN_Server_IP]/32 via [Gateway_Address] dev [Network_Device_Name]
ip route del default via [Gateway_Address] dev [Network_Device_Name]
#!/bin/sh

[ "$UID" -eq 0 ] || exec sudo "$0" "$@"

vpnclient start
sleep 2s
vpncmd localhost /client /CMD AccountConnect GE
dhclient vpn_vpn
ip route add 145.245.93.56/32 via 192.168.1.1 dev wlp3s0
ip route del default via 192.168.1.1 dev wlp3s0

stop.sh

#!/bin/sh

[ "$UID" -eq 0 ] || exec sudo "$0" "$@"

vpncmd localhost /client /CMD AccountDisconnect GE
vpnclient stop
ip route del default via 192.168.30.1 dev vpn_vpn
ip route del 145.245.93.56 via 192.168.1.1 dev wlp3s0 
ip route del 192.168.30.0/24 dev vpn_vpn proto kernel scope link src
ip route add default via 192.168.1.1 dev wlp3s0 proto dhcp metric 600
echo 'nameserver 8.8.8.8' > /etc/resolv.conf
echo 'nameserver 8.8.4.4' >> /etc/resolv.conf

References
https://pupli.net/2016/09/19/how-to-setup-softether-in-ubuntu/

Write data to text file using bash/shell scripting

You can redirect the output of a command to a file:

cat file > copy_file

or append to it

cat file >> copy_file

If you want to write directly the command is echo ‘text’

echo 'Hello World' > file

Or

# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt

# possibility 2:
echo "line 1
line 2" >> greetings.txt

# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT

 

References
https://stackoverflow.com/questions/11162406/open-and-write-data-to-text-file-using-bash-shell-scripting
https://unix.stackexchange.com/questions/77277/how-to-append-multiple-lines-to-a-file