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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-21-2007, 07:55 AM
|
#1
|
|
Member
Registered: Jun 2004
Location: Bharat
Distribution: RedHat, Debian, FreeBSD, Fedora, Centos
Posts: 114
Rep:
|
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
|
|
|
|
09-21-2007, 09:30 AM
|
#2
|
|
Member
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31
Rep:
|
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
|
|
|
|
09-22-2007, 11:48 AM
|
#3
|
|
Member
Registered: Sep 2007
Posts: 42
Rep:
|
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 11:49 AM.
|
|
|
|
09-22-2007, 11:24 PM
|
#4
|
|
Member
Registered: Jun 2004
Location: Bharat
Distribution: RedHat, Debian, FreeBSD, Fedora, Centos
Posts: 114
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:04 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|