Skip to content

Commit

Permalink
Merge pull request #19 from GovTechSG/add-file-check
Browse files Browse the repository at this point in the history
added config file existence check with red warning
  • Loading branch information
Salihan04 authored Jun 27, 2019
2 parents 06ae50b + f074cf4 commit f86f31c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions slist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
###################################################################################################

list_path=/tmp/serverslist.lst
config_file=~/.ssh/config

# Coloring
red=$'\e[1;31m'
end=$'\e[0m'

usage() { echo "Usage: $0 [-p <filer word>]" 1>&2; exit 1; }

Expand All @@ -26,7 +31,8 @@ exit;


main() {
list=$(< ~/.ssh/config grep "Host " | awk '{print $2}')
check_config_file_exists
list=$(< $config_file grep "Host " | awk '{print $2}')
rm -f $list_path
num=1
colour=34
Expand Down Expand Up @@ -80,7 +86,7 @@ main() {
}

filter() {
list=$(< ~/.ssh/config grep "Host " | awk '{print $2}')
list=$(< $config_file grep "Host " | awk '{print $2}')
rm -f $list_path
num=1
colour=34
Expand Down Expand Up @@ -138,7 +144,7 @@ filter() {

list() {
colour=34
< ~/.ssh/config grep Host | while read -r line;
< $config_file grep Host | while read -r line;
do
if [[ $line != *"*"* ]]; then
if [[ $line == *"Host "* ]]; then
Expand All @@ -158,7 +164,7 @@ list() {
}

flist() {
< ~/.ssh/config grep Host | while read -r line;
< $config_file grep Host | while read -r line;
do
if [[ $line == *"Host "* ]]; then
replace_string=$(sed 's/Host/Server:/g' <<< "$line")
Expand All @@ -171,6 +177,16 @@ flist() {
exit;
}

check_config_file_exists(){
if [ -f "$config_file" ]; then
echo "$config_file exist"
else

printf "%s\n" "${red} $config_file <-- file does not exist - Please create one. ${end} "
exit;
fi
}

if [ $# -eq 0 ]; then
clear
main
Expand Down

0 comments on commit f86f31c

Please sign in to comment.