Home > Software design >  How effectlively to call a bash function to another in a program
How effectlively to call a bash function to another in a program

Time:02-04

I have below Bash script where I am trying to Create functions for creating colors for output text in the script, I am trying to call these function into another one but somehow its not working.

I looked around and googled, but didn't get the anything concrete to my situation, Can someone please help we to get, what i am doing wrong?

Script:

#!/bin/bash
read -rsp $'Please Enter password below: ' SSHPASS
echo -n  ""
export SSHPASS

RED=$'\033[0;31m'; GREEN=$'\033[32m'; YELLOW=$'\033[1;33m'; BLUE=$'\033[0;34m'; ENDCOLOR=$'\033[0m'
#
function warning() {
  printf "${RED}[WARNING] %s${ENDCOLOR}\n" "$1" >&2
}

function information() {
  printf "${YELLOW}[INFO] %s${ENDCOLOR}\n" "$1"
}

function ok() {
  printf "${GREEN}[INFO] %s${ENDCOLOR}\n" "$1"
}

function hostclr() {
  printf "${BLUE}[INFO] %s${ENDCOLOR}\n" "$1"
}

function remote_connect() {
   target_host=$1

   sshpass -e ssh -q "${target_host}"  "true" -o StrictHostKeyChecking=no -o ConnectTimeout=60 2>>/dev/null
   if [[ $? -eq 0 ]]

   then
     SMBconf=$(sshpass -e ssh -q -t "$target_host" "grep -i vfs /etc/samba/smb.conf")

     if [[ $SMBconf == "" ]];then
         printf "%-20s s %5s 0s\n" hostclr "$target_host"  ok "NO VFS Stack found(not Vulnerable!)"

     elif [[ $SMBconf != "" ]];then
         printf "%-20s s %5s 0s\n"  hostclr "$target_host"  warning "Its Vulnerable!"

     else
         printf "%-20s s  %5s 0s\n" hostclr "$target_host" "Unable to get the ssh connection"
     fi
fi
}  2>/dev/null
export -f remote_connect
< samba_new_Servers xargs -P10 -n1 -d'\n' bash -c 'remote_connect "$@"' --
unset SSHPASS

Result: enter image description here

Edit:

I am using functions hostclr, ok etc

CodePudding user response:

export -f remote_connect

You exported only one function. You have to export all that you use.

export RED GREEN YELLOW etc...
export -f remote_connect hostclr ok information etc...

CodePudding user response:

You still have to use command substitution to get the output of ok et al. to use as an argument to printf.

printf "%-20s s %5s 0s\n" "$(hostclr "$target_host")"  "$(ok "NO VFS Stack found(not Vulnerable!)")"
  •  Tags:  
  • Related