LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2009, 08:42 PM   #1
pbxuser911
LQ Newbie
 
Registered: Oct 2009
Location: USA
Posts: 12

Rep: Reputation: 0
help combining 2 shell scripts


im new to linux
we have 2 scripts i can run, bith work seperately, but i want to combine it

can anyone help me? this is for our VOIP PBX, what it does, it shows me the passwords for the extension accounts, and one shows me the DID;s aka the alias

this one shows password:
#!/bin/bash
# Show the passwords of all users:

function get_xml()
{
gawk -v tag=$1 'BEGIN{regex="<" tag ">([^<]*)</" tag ">";}{ match($0, regex, m); for(i = 1;; i++) { if(!(i in m)) break; printf("%s\n",m[i]);}}' $2
}

for user in users/*.xml
do
name=${user:6} # only the name
idx=${name%.xml} # only the number
type=$(get_xml type $user)
if [ "$type" == extensions ]; then
id=$(get_xml id $user)
primary=$(get_xml alias $user)
domain=$(get_xml domain user_alias/$primary.xml)
for d in domain_alias/*.xml
do
dom=$(get_xml domain $d)
if [ -z "$dom" -a "$dom" == "$domain" ]; then
domain_name=$(get_xml name $d)
break
fi
done

dialplan=$(get_xml dial_plan $user)
if [ -z "$dialplan" ]; then
plan="Default"
else
plan=$(get_xml name dial_plan/$dialplan.xml)
fi
password=$(get_xml password extensions/$id.xml)
pin=$(get_xml mb_pin extensions/$id.xml)
username=$(get_xml name user_alias/$primary.xml)
if [ -z "$pin" ]; then
echo $domain $username $password \($plan\)
else
echo $domain $username $password \($plan\) [$pin]
fi
fi
done


this one shows DID, i want to take them and make it into 1.
#!/bin/bash
# Show all account names that are a telephone number:

function get_xml()
{
gawk -v tag=$1 'BEGIN{regex="<" tag ">([^<]*)</" tag ">";}{ match($0, regex, m); for(i = 1;; i++) { if(!(i in m))\
break; printf("%s\n",m[i]);}}' $2
}

for alias in user_alias/*.xml
do
name=${alias:11} # only the name
idx=${name%.xml} # only the number
name=$(get_xml name $alias)
if [ -z "$name" -o "${#name}" -lt 8 ]; then continue; fi
user=$(get_xml user $alias)
domain=$(get_xml domain $alias)
domainname=$(get_xml name domains/$domain.xml)
echo $name $domainname
done
 
Old 10-26-2009, 11:59 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
In general terms, you have to make sure the variable names don't conflict between the two scripts, then just remove the #!/bin/bash from the second one and add it to the first. If there were any exit codes in the first script that would terminate it early you'd have to remove those too, but I don't see any here.

And since the second script resets the variables anyway, there may be no need to do anything special. Go ahead and try combining them and see what happens.

When it all comes down to it, you just have to make sure the individual commands still run and that nothing stops the script before you want it to. A shell script generally just executes commands in the order it comes across them.

By the way, please use [code][/code] tags around your script code, to help preserve formatting and to keep everything readable.
 
Old 10-27-2009, 01:13 PM   #3
boondocksaint
LQ Newbie
 
Registered: Oct 2009
Distribution: RHEL4, RHEL5, CentOS, Customized Linux
Posts: 11

Rep: Reputation: 1
you could also make a separate file ex. runAll.sh and run both of your scripts with that file if you want to keep the two original scripts as separate entities. Your runAll.sh file would look something like...

Code:
#!bin/bash

./script1
./script2
 
Old 10-28-2009, 08:49 PM   #4
pbxuser911
LQ Newbie
 
Registered: Oct 2009
Location: USA
Posts: 12

Original Poster
Rep: Reputation: 0
i want to combine both of them into the same script,
my problem is i dont understand how to read the script
all the functions of that scrpt i want to combine
 
Old 10-29-2009, 02:19 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Time to start learning bash I think, otherwise you'll be forever dependent on waiting for others to give you the answer. Start with these:

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 10-29-2009, 10:17 AM   #6
boondocksaint
LQ Newbie
 
Registered: Oct 2009
Distribution: RHEL4, RHEL5, CentOS, Customized Linux
Posts: 11

Rep: Reputation: 1
If you want to combine them into one function, it looks like you just need to copy the following function into the first script file since you are already using the first function, get_xml(), in both...

Code:
for alias in user_alias/*.xml
do
name=${alias:11} # only the name
idx=${name%.xml} # only the number
name=$(get_xml name $alias)
if [ -z "$name" -o "${#name}" -lt 8 ]; then continue; fi
user=$(get_xml user $alias)
domain=$(get_xml domain $alias)
domainname=$(get_xml name domains/$domain.xml)
echo $name $domainname
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
combining scripts squall0366 Linux - Newbie 3 07-01-2009 12:58 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Need help - combining variables in shell vegas35 Programming 2 10-29-2007 09:28 PM
Need help with shell scripts thorney Linux - Newbie 4 11-27-2005 11:18 PM
Combining 2 perl scripts together meluser Programming 5 03-18-2003 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration