Bash Script giving "Command Not found"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script giving "Command Not found"
# 1  
Old 10-03-2013
Bash Script giving "Command Not found"

Hello Geeks,

Greetings...I have the following script:

Code:
#!/usr/bin/bash
#Script to generate number of active PDP context & calculate PDP activation #failurefrom EPG-M
#Script written by Gbenga Adigun
#September 12, 2013

username="xxxxxx"
password="xxxxxxxxx"

HOSTS=(
		ggsn01
		ggsn02
	  );
	  
##Attempted,Completed & Active PDP Skeleton Template

pdp_command(){
    for a in ${HOSTS[@]}; do
	"${a}pdp"=`sshpass -p "$password" ssh -q "$username"@"$a" "show services epg pgw statistics apn web.net" | grep "Active PDP contexts" | awk '{print $4}'`
    "${a}a1"=`sshpass -p "$password" ssh -q "$username"@"$a" "show services epg pgw statistics apn web.net" | grep "Attempted PDP activations" | awk '{print $4}' | sed '$s/,$//'`
    "${a}c1"=`sshpass -p "$password" ssh -q "$username"@"$a" "show services epg pgw statistics apn web.net" | grep "Attempted PDP activations" | awk '{print $6}'`
	 
	echo `date` "Active PDP Contexts for the past 15 mins is:" $a'pdp'>> /var/ipworks/logs/active_pdp/`date | awk '{ print $2"-" $3"-" $6 }'`"$a_active_pdp".txt
	
	echo $a'a1'
	echo
	echo $a'c1'
	done
}

pdp_command



But problem is when bash calls pdp_command function, i usually get command not found for the variable substitution.


Code:
root###bash -x pdp_stats.sh 
+ username=xxxx
+ password=xxxxxxxx
+ HOSTS=(ggsn01 ggsn02)
+ pdp_command
+ for a in '${HOSTS[@]}'
++ sshpass -p xxxssh -q xxx@ggsn01 'show services epg pgw statistics apn web.net'
++ grep 'Active PDP contexts'
++ awk '{print $4}'
+ ggsn01pdp=
pdp_stats.sh: line 18: ggsn01pdp=: command not found
++ sshpass -p xxxx ssh -q xxx@ggsn01 'show services epg pgw statistics apn web.net'
++ grep 'Attempted PDP activations'
++ awk '{print $4}'
++ sed '$s/,$//'
+ ggsn01a1=
pdp_stats.sh: line 19: ggsn01a1=: command not found



Any help will be appreciated!

Moderator's Comments:
Mod Comment Please use code tags for your data and code
# 2  
Old 10-03-2013
Code:
"${a}a1"=...

Variables do not work this way.

Always avoid dynamic variable names if you can, they tend to be huge security holes and cause more headaches than they solve.
# 3  
Old 10-03-2013
Please, any idea how to go about this? Not a seasoned programmer Smilie
# 4  
Old 10-03-2013
Redesign in a way which doesn't need arbitrary runtime-defined variable names. Try arrays:

Code:
for ((N=0; N<${#HOSTS[@]}; N++))
do
        AA1[$N]="$(ssh username@${HOSTS[$N]} ... )"
done

While you're at it, get rid of sshpass too -- using it exposes your passwords to the system at large, there's reasons ordinary ssh is so picky. You can use keys to arrange passwordless logins instead.

Also, you can get everything you want in one ssh call instead of three, which is important because ssh calls can be slow:

Code:
# $$ is your shell's PID, which amounts to a convenient random number here
ssh username@${HOSTNAME[$N]} show services epg pgw statistics apn web.net > /tmp/$$ # Run once and save

VAR1="$(awk '/Active PDP contexts/ {print $4}' /tmp/$$)"
VAR2="$(awk '/Attempted PDP activations/ {print $4, $6}' /tmp/$$)"
rm /tmp/$$

$( ) is the modern equivalent of backticks ` `.
# 5  
Old 10-03-2013
Would associative array be a solution? pdp[ggsn01], pdp[ggsn02], a1[ggsn01], ...
# 6  
Old 10-03-2013
Quote:
Originally Posted by RudiC
Would associative array be a solution? pdp[ggsn01], pdp[ggsn02], a1[ggsn01], ...
Only available in some bash and some ksh, so I don't reccomend their use, it'll break when you carry the script anywhere else.
# 7  
Old 10-03-2013
Always could fallback to 'default' indexed arrays...

Code:
ARRAY1=($@)
ARRAY2=( 1 2 3 4 5 .. )
ARRAY3=('some@email.com' 'http://or.internet.com')

# Examples
echo ${ARRAY1[0]}
ARG1
echo ${ARRAY2[2]}
3
echo "${ARRAY3[1]}"
http://or.internet.com

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX and Linux Applications

Problem on SQLplus command ""bash: sqlplus: command not found""

Hi all, i face an error related to my server ""it's running server"" when i use sqlplus command $ sqlplus bash: sqlplus: command not found the data base is up and running i just need to access the sqlplus to import the dump file as a daily backup. i already check the directory... (4 Replies)
Discussion started by: clerck
4 Replies

3. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

4. Shell Programming and Scripting

Bash script fails with "function: not found" error

Hello everyone, I am having problems figuring this out. This script below is supposed to create a list of file names with their "md5sum", in a file "lib-list.txt" When I run it "sh component-list.sh " I get this:component-list.sh: 4: component-list.sh: function: not found component-list.sh:... (4 Replies)
Discussion started by: joemb
4 Replies

5. Shell Programming and Scripting

"Command not found" doing a while loop in bash/shell

i=0 numberofproducts=${#urls} #gets number of entries in array called "urls" numberofproductsminusone=`expr $numberofproducts - 1` #-subtract by one while do wget ${urls} i=$(( $i + 1 )) sleep 10 done I'm getting an error ./scrape: line 22: [0: command not found that... (3 Replies)
Discussion started by: phpchick
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Post Here to Contact Site Administrators and Moderators

What's happens with my thread about "-bash: ELF: command not found "?

Hi, Today, I've submitted a new tread in "Shell Programming and Scripting" forum, with title "-bash: ELF: command not found ". However, this thread has disappear. Can somebody give me an explanation? Regards. (3 Replies)
Discussion started by: Sonia_
3 Replies

8. Shell Programming and Scripting

How to distinguish between "command not found" and "command with no result"

system() call imeplemented in solaris is such a way that: Command not found - return code 1 Command executed successfully without Output - return code 1 how to distinguish between these two based on return code in a c - file? Can you help on this ? (5 Replies)
Discussion started by: iitmadhu
5 Replies

9. Shell Programming and Scripting

"-bash: sqlldr: command not found"

hi all, here i am trying to run one control file. but getting "-bash: sqlldr: command not found" error :confused: the code is given below. sqlldr $db_username/$db_password@$db_sid control=$loading_path/load_to_table.ctl log=loading.log can anybody help me in fixing the issue? ... (1 Reply)
Discussion started by: vinayakatj56
1 Replies

10. Shell Programming and Scripting

In ksh shell command - Print "-ABC" is giving error

Hi Guys, while executing the following command : print "-ABC" is giving following error : ksh: print: bad option(s) I cannot use echo for some other reasons, so any other option ? (2 Replies)
Discussion started by: sagarjani
2 Replies
Login or Register to Ask a Question