- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- variable substitution
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 08:43 AM
тАО04-29-2008 08:43 AM
I am trying to set a variable and run a test on it later but m struggling a bit:
count=0
for data in 1 2 3 4
do
count=$((count+1))
if [[ ! -f /tmp/test ]] ; then
diafail$count=1
fi
I basically want diafial$count to be tested later
so
if [[ diafail$count -ne 1 ]] ; then
etc etc
but this isnt working
any ideas?
thanks
chris
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 08:51 AM
тАО04-29-2008 08:51 AM
Re: variable substitution
first, i would do some 'echo's in this script.
For example 'echo $count'
Then, I don't know if 'count=$((count+1))' works like you want.
Maybe 'count=(($count+1))'?
HTH
Volkmar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 09:03 AM
тАО04-29-2008 09:03 AM
Solutionin this case 'eval' is your friend or change to use arrays. I set test 1+3 in my example.
touch /tmp/test1 /tmp/test3
cat /tmp/chtst
#!/usr/bin/ksh
typeset -i count=0 max=4
while [ count+=1 -le max ]
do
if [ -f /tmp/test$count ]
then eval diafail$count=1
else eval diafail$count=0
fi
done
count=0
while [ count+=1 -le max ]
do
if eval [ \$diafail$count -eq 1 ]
then print test $count active
fi
done
mfG Peter
- Tags:
- eval
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 09:18 AM
тАО04-29-2008 09:18 AM
Re: variable substitution
I'd use an array of variables:
# cat ./myscript
#!/usr/bin/sh
set -A diafail
count=0
for data in 1 2 3 4
do
count=$((count+1))
if [[ ! -f /tmp/test ]] ; then
diafail[$count]=1
else
diafail[$count]=0
fi
if [[ ${diafail[$count]} -ne 1 ]] ; then
echo "OK"
else
echo "BAD"
fi
done
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 11:20 AM
тАО04-29-2008 11:20 AM
Re: variable substitution
for COUNT in 1 2 3 4
do
eval "VAR$COUNT=$COUNT"
eval "print VAR$COUNT=\$VAR$COUNT"
done
Now a bit of explanation is needed so you turn on shell tracing like this:
set -x
+ eval VAR1=1
+ VAR1=1
+ eval print VAR1=$VAR1
+ print VAR1=1
VAR1=1
+ eval VAR2=2
+ VAR2=2
+ eval print VAR2=$VAR2
+ print VAR2=2
VAR2=2
+ eval VAR3=3
+ VAR3=3
+ eval print VAR3=$VAR3
+ print VAR3=3
VAR3=3
+ eval VAR4=4
+ VAR4=4
+ eval print VAR4=$VAR4
+ print VAR4=4
VAR4=4
So you see that $COUNT must first be turned into a simple character, then the entire line is evaluated. In the print statement, we escape the $ in the string $VAR$COUNT so that the $ remains but $COUNT gets turned into a number.
This coding would be a bit easier to understand if you used an array:
set -A DIAFAIL 0 0 0 0 0
for COUNT in 1 2 3 4
do
if [[ ! -f /tmp/test$COUNT ]]
then
DIAFAIL[$COUNT]=1
fi
done
echo ${DIAFAIL[@]}
In the above example, the DIAFAIL array tests for /tmp/test1 then test2, test3 and finally test4. If any of them are not files, the corresponding array location will be 1, otherwise 0. Note that arrays always start at 0 so there will be a DIAFAIL[0] value printer with the [@] shorthand.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-29-2008 11:27 AM
тАО04-29-2008 11:27 AM
Re: variable substitution
This works. But if you want count to be an integer you could use (()) for arithmetic expressions:
(( count = 0 ))
(( count += 1 ))
>Maybe 'count=(($count+1))'?
This doesn't return a value and gets a syntax error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2008 12:54 AM
тАО04-30-2008 12:54 AM
Re: variable substitution
I'll give them a whirl and let you know.
thanks
Chris.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2008 01:07 AM
тАО04-30-2008 01:07 AM
Re: variable substitution
Array apears to be the best option for my task .....
Chris.