LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-21-2007, 08:55 AM   #1
xxx_anuj_xxx
Member
 
Registered: Jun 2004
Location: Bharat
Distribution: RedHat, Debian, FreeBSD, Fedora, Centos
Posts: 114

Rep: Reputation: 16
grep and assign it's output to variables inside script itself problem


Hi,
I am trying to create a script code which can assign it's output incrementally,
if n output then n new variables assigned,
basic code here is.

#!/bin/bash
NODEA=node2.example.com
NODEB=pr.example.com
N=1
for i in $NODEA $NODEB;do
disks=`ssh $i mdadm --examine --scan |grep , |cut -d = -f2|sed 's/,/ /g'`

for i in $disks ;do
echo $i
done
done


I want to generate a new varaible to store $i value so that I can utilize them,

Thanks and regards
Anugunj Anuj Singh
 
Old 09-21-2007, 10:30 AM   #2
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
You could put the values of $i into an array:

Code:
index=0

for i in $disks; do
    array[index]=$i
    let index+=1
done
 
Old 09-22-2007, 12:48 PM   #3
jdiggitydogg
Member
 
Registered: Sep 2007
Posts: 42

Rep: Reputation: 15
your scripts looks close. however, it does look like you are abusing the same variable i within two loops. also var N doesn't look used?

why not try something like this?

Code:
for i in $NODEA $NODEB
do
        disks=`<your command here>`

        for j in $disks
        do
                echo $j
        done
done
not sure this is related to what you want to do?

Last edited by jdiggitydogg; 09-22-2007 at 12:49 PM.
 
Old 09-23-2007, 12:24 AM   #4
xxx_anuj_xxx
Member
 
Registered: Jun 2004
Location: Bharat
Distribution: RedHat, Debian, FreeBSD, Fedora, Centos
Posts: 114

Original Poster
Rep: Reputation: 16
Hi,
Thanks for support,
I used this and fits my requirement.

$1 and $2 are the variables passed to script (e.g. NODEA , NODEB)

################## raid section #######################
raid_disks () {
ssh $1 mdadm --examine --scan |grep ,|cut -d = -f2| sed 's/,/ /g';
}
DISKS=(`raid_disks $2`)
echo Found ${DISKS[1]}
echo Found ${DISKS[2]}
echo Found ${DISKS[0]}
echo Found ${DISKS[@]}
DISKSB=(`raid_disks $2`)
echo Found ${DISKSB[1]}
echo Found ${DISKSB[2]}
echo Found ${DISKSB[0]}
echo Found ${DISKSB[@]}

I have variables which I can add to the raid failure and recovery tests.
This is the script I am working on. Any good idea to improve it.



#!/bin/bash
USAGE="`basename $0` [Path to mount] [Storage node 1] [Storage node2 ]"
WORK_DIR="$1"
LOG_DIR="$WORK_DIR/log"
ERR="$LOG_DIR/testsuite.log"
RESULTS=$WORK_DIR/results
ROOT_UID=0
FSX_URL='http://jaist.dl.sourceforge.net/sourceforge/ltp/ltp-full-20070731.tgz'
KERNEL_URL='http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.22.6.tar.bz2'
IOZONE_URL='http://www.iozone.org/src/current/iozone3_283.tar'
PS3="Kernel Option:==>"
export PS3

if [ "$1" = "-h" -o "$1" = "--help" ];then
echo "Blah blah blah"
exit 0
fi

if [ "$#" -ne 3 -o ! -d "$1" ];then
echo $USAGE
echo "Try `basename $0` --help for more options."
exit 0
fi

echo "Checking User"
sleep .91
if [ "$UID" -eq "$ROOT_UID" ]
then
echo "You are root. OK"
else
echo "Please run this script as a root user to complete task"
echo "Aborting...."
exit 0
fi
echo "Checking gfs mount."
sleep .91
mount |grep "gfs" >/dev/null
if [ "$?" -ne 0 ] ;then
echo "File system GFS is not mounted, Please mount GFS file system."
echo "Exiting..."
exit 0
else
echo "Found mounted GFS. OK"
fi


if [ ! -d $RESULTS ];then
echo "Creating directory to store test results."
sleep 1
mkdir $WORK_DIR/results
fi
if [ ! -d $LOG_DIR ];then
echo "Creating log diretory."
sleep 1
mkdir $WORK_DIR/log
fi

echo "Performing initial tests of nodes before performing our test suite of failing nodes in different scenarios. be patience"
echo "Checking the secure shell access to the storage nodes $2 $3 ."

for i in "$2" "$3" ;do
sleep 2
echo hi |nc "$i" 22 >/dev/null
echo
echo "Good secure shell port found open on storage node $i"
echo
echo "Now checking remote command execution on storage node $i"
echo "=================================================================================================== ================"
echo -e "$i should not prompt for password , in case it asks then generate id-dsa public key $HOSTNAME:/root/.ssh/id_dsa.pub on this machine with command\n ssh-keygen -t dsa with an empty password and copy it in $i:/root/.ssh/authorized_keys "
echo "=================================================================================================== ================="
echo
echo
sleep 1
echo "******** This is our storage node $i. *********"
echo
ssh $i uname -a
if [ "$?" -ne 0 ];then
echo "Sorry contact your network admin to make sure node $i port 22 is accessible to this machine $HOSTNAME."
echo "Errror remote command execution. Initial condition to perform our test did not met. Node $i is not accessible over secure shell port 22. "
exit 143
fi
done


################## raid section #######################
raid_disks () {
ssh $1 mdadm --examine --scan |grep ,|cut -d = -f2| sed 's/,/ /g';
}
DISKS=(`raid_disks $2`)
echo Found ${DISKS[1]}
echo Found ${DISKS[2]}
echo Found ${DISKS[0]}
echo Found ${DISKS[@]}
DISKSB=(`raid_disks $2`)
echo Found ${DISKSB[1]}
echo Found ${DISKSB[2]}
echo Found ${DISKSB[0]}
echo Found ${DISKSB[@]}
echo -e "Remote command execution over storage nodes $2 $3 test OK. \n"
sleep 2
select i in "kernel install and file I/O reports" "Only file I/O reports" "exit"
do
case $i in
kernel*) echo "You have selected to install the downloaded kernel as well to generate reports."
ANSWER=0
break ;;
Only*) echo "Only reluts will be generated, no kernel will be installed."
ANSWER=1
break;;
exit) echo "Aborting..."
exit 0;;
*) echo "Please select a number from the menu" ;;
esac
done

echo "Checking Network for internet access."

ping -c 1 192.168.10.1 >/dev/null
#ping -c 5 google.com >/dev/null

if [ "$?" -ne 0 ] ;then
echo "You dont have direct access to internet. Please contact your network administrator."
echo -e "[`date`] You don't have direct internet access to download test suites. contact network administrator" >>$ERR
exit 0
else
echo "Internet access checked. OK"
echo "Now downloading test kernel file."
cd $WORK_DIR
wget -c $KERNEL_URL
tar jxvf $WORK_DIR/linux-2.6.22.6.tar.bz2 >/dev/null


cd linux-2.6.22.6


make allyesconfig
make
sudo make modules_install install

fi
echo "Now downloading and installing iozone."
cd $WORK_DIR
echo $PWD
wget -c $IOZONE_URL

echo "Installing iozone test suite"
tar xvf iozone3_283.tar
cd $WORK_DIR/iozone3_283/src/current/
echo "[`date`]==========iozone installation===========" >>$ERR
make linux 2>>$ERR
if [ "$?" -ne 0 ];then
echo "installation of iozone test suite failed , Please check log $ERR for more details"
else
echo "iozone is installed"
echo -e "[`date`] iozone is installed successfully in $PWD \n" >>$ERR

echo "Running the iozone test on downloaded file, this may take significant time."
echo "[`date`]==========iozone test startup==========" >>$ERR
cd $WORK_DIR
if [ -f linux-2.6.22.6.tar.bz2 ];then
echo "Starting iozone test."
$WORK_DIR/iozone3_283/src/current/iozone -ec -r 4 -r 16 -s 32m -s 256m -s 2G -i 0 -i 1 -i 2 -i 8 -b $RESULTS/output_iozone.wks -f $WORK_DIR/linux-2.6.22.6.tar.bz2 2>>$ERR
echo "Results are stored in $RESULTS/output_iozone.wks file."
echo -e "[`date`]*****Test iozone completed***** result is stored in $RESLULTS/ouput_iozone.wks \n" >>$ERR
else
echo -e "[`date`]File does not exist linux-2.6.22.6.tar.bz2" >>$ERR
echo "Test file does not exist. Check $ERR"
exit 0
fi
fi
echo "********Starting fsx test*********"
echo "installing fsx now."
cd $WORK_DIR
echo $WORK_DIR
wget -c $FSX_URL
tar zxvf ltp-full-20070731.tgz
find $WORK_DIR/ltp-full-20070731/ -name "*fsx-linux*.c" -exec cc -o fsx {} \;
$WORK_DIR/fsx -WR -S 0 -l 10000000 -o 1000000 -c 100 -N 10000 -d $WORK_DIR/linux-2.6.22.6.tar.bz2 2>>$ERR
cp -f *.fsx* $RESULTS/
echo "[`date`]fsx results are stored in $RESULTS dir. `ls -l *.fsx*`">>$ERR
echo -e "************* fsx test completed ***************">>$ERR

regards
Anugunj Anuj Singh
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
assign commands to Variables tazbv Programming 2 08-03-2007 03:07 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM
Scripting : Dual line output into next script as variables facets Programming 6 06-10-2004 03:28 PM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 09:26 AM


All times are GMT -5. The time now is 12:00 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration