Hi,everyone:
Today, I learned many about some tools about Linux and blog. Although it is still in summer vacation, I think it is time to start my another more important jobs; I know it is not enough to learn only some elementary acknowledge about communication; I think I spent too little time to practice everything which the teacher gave me before.

I have learned about how to use shell script, but there are still some problem to solve, that is, I did not know how to use if...else: The original example of "if...else" is "if [ "$k" == "6" ]; then" when we want to say "if $k equal 6, and then .... But what about 'if $k bigger than 6' ? In the beginning, I knew that was "if [ "$k" > "6" ]; then", I tested it again and again, and at last I knew it was wrong.


This is my original code:

#!bin/bash
#this is my first homework in summer, 2008 in TONIC
#the target of this homework is how to use shell script
# to ping and get some result, so that's it
sk=0
for (( k=1; k<=18; k=k+1 ))
do

echo "$k"
if [ "$k" < "7" ]; then
cd ~/summer_practice
mkdir 1_6
cd 1_6
fi

if [ "$k" > "6" ] && [ "$k" < "13" ]; then 
cd ~/summer_practice
mkdir 7_12
cd 7_12
fi

if [ "$k" > "12" ]; then 
cd ~/summer_practice
mkdir 13_18
cd 13_18
fi


echo "start ping someone's IP at $k th time!!"
j=`ping -c 1 www.google.com`
pwd
echo $j >> test_$k.txt
echo "and I will sleep 1 minute and then again to ping the same IP addr"
sleep 2
done
cd ..

Because of some problems about if...else, so I deleted the 'if...else', increasing other two 'for', so it is done successfully, and the code is below

#!bin/bash
#this is my first homework in summer, 2008 in TONIC
#the target of this homework is how to use shell script
# to ping and get some result, so that's it
k=0
mkdir 1_6
cd 1_6
for (( k=1; k<=6; k=k+1 ))
do
echo "start ping someone's IP at $k th time!!"
j=`ping -c 1 www.google.com`
pwd
echo $j >> test_$k.txt
echo "and I will sleep 1 minute and then again to ping the same IP addr"
sleep 2
done
cd ~/summer_practice

mkdir 7_12
cd 7_12
for (( k=6; k<=12; k=k+1 ))
do
echo "start ping someone's IP at $k th time!!"
j=`ping -c 1 www.google.com`
pwd
echo $j >> test_$k.txt
echo "and I will sleep 1 minute and then again to ping the same IP addr"
sleep 2
done
cd ~/summer_practice

mkdir 13_18
cd 13_18
for (( k=13; k<=18; k=k+1 ))
do
echo "start ping someone's IP at $k th time!!"
j=`ping -c 1 www.google.com`
pwd
echo $j >> test_$k.txt
echo "and I will sleep 1 minute and then again to ping the same IP addr"
sleep 2
done
cd ~/summer_practice


It looks stupid, but the answer is right

and that is, thanks everyone~~ 

  • there are some problem about displaying on the frontpage, and I can not upload my code to the server in this time, and I do not know the reason. So I think I hove to change another day to try to upload my shell file.

 

arrow
arrow
    全站熱搜

    Hentyduary 發表在 痞客邦 留言(0) 人氣()