哪里错了呢?

sh/bash/dash/ksh/zsh等Shell脚本
回复
qjse
帖子: 5
注册时间: 2009-04-10 22:10

哪里错了呢?

#1

帖子 qjse » 2009-04-10 22:21

cd ubuntu
for d in *; do
if [ -d "$d" ]; then
h=`date +%H`
while [ $h -ge 7 -a $h -lt 23 ]; do
h=`date +%H`
done
tar cvf "$d.tar" "$d"
fi
done

这个程序无法运行啊?我是菜鸟请指点一下
就是将ubuntu目录中所有的目录单独打包,然后存放到/backup分区中
7点以后程序暂停,23点继续运行。
上次由 qjse 在 2009-04-14 16:10,总共编辑 3 次。
qjse
帖子: 5
注册时间: 2009-04-10 22:10

Re: 这个想法如何实现?

#2

帖子 qjse » 2009-04-11 12:06

这样的功能如何实现? 高手指点一下~
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 这个想法如何实现?

#3

帖子 hellojinjie » 2009-04-11 12:52

貌似蛮复杂的

想想怎么做。。。thinking。。。
Say hello to everyday!
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 这个想法如何实现?

#4

帖子 aerofox » 2009-04-11 21:53

代码: 全选

cd ubuntu
for d in *; do
    if [ -d "$d" ]; then
        h=`date +%H`
        while [ $h -ge 7 -a $h -lt 23 ]; do
            h=`date +%H` 
        done
        tar cvf "$d.tar" "$d"
    fi
done
qjse
帖子: 5
注册时间: 2009-04-10 22:10

Re: 谢谢

#5

帖子 qjse » 2009-04-14 15:43

cd ubuntu
for d in *; do
if [ -d "$d" ]; then
h=`date +%H`
while [ $h -ge 7 -a $h -lt 23 ]; do
h=`date +%H`
done
tar cvf "$d.tar" "$d"
fi
done

这个程序无法运行啊?我是菜鸟请指点一下
就是将ubuntu目录中所有的目录单独打包,然后存放到/backup分区中
7点以后程序暂停,23点继续运行。
shyl
帖子: 3
注册时间: 2007-09-18 22:19

Re: 哪里错了呢?

#6

帖子 shyl » 2009-04-14 16:29

貌似很多问题噢。

首先,如果你的目的应该是每小时执行一次。那应该把判断时间的循环放在外面。
另外,建议你的程序每执行一次以后用sleep语句休眠一个小时,而不是轮询CPU。

所以,建议你试试这么写

#!/bin/bash
cd ubuntu
h=`date +%H`
while [ 1 -eq 1 ]; do
if [ $h -ge 7 -a $h -lt 23 ]; then
#do nothing
else
for d in *; do
if [ -d "$d" ]; then
tar cvf "$d.tar" "$d"
fi
done
fi

sleep 3600
h=`date +%H`
done
qjse
帖子: 5
注册时间: 2009-04-10 22:10

Re: 哪里错了呢?

#7

帖子 qjse » 2009-04-14 16:54

程序出现错误
./a: line 7: 在未预料的“else”附近出现语法错误
./a: line 7: `else'
也就是对ubutnu目录中的目录单独打包,到7点就暂停,23点开始直到打完
shyl
帖子: 3
注册时间: 2007-09-18 22:19

Re: 哪里错了呢?

#8

帖子 shyl » 2009-04-14 17:16

这样行不?

#!/bin/bash
cd ubuntu
h=`date +%H`
while [ 1 -eq 1 ]; do
if [ $h -lt 7 -o $h -ge 23 ]; then
for d in *; do
if [ -d "$d" ]; then
tar cvf "$d.tar" "$d"
fi
done
fi

sleep 3600
h=`date +%H`
done
pope123
帖子: 34
注册时间: 2008-09-21 1:01

Re: 哪里错了呢?

#9

帖子 pope123 » 2009-04-14 17:58

shyl 写了:这样行不?

#!/bin/bash
cd ubuntu
h=`date +%H`
while [ 1 -eq 1 ]; do
if [ $h -lt 7 -o $h -ge 23 ]; then
for d in *; do
if [ -d "$d" ]; then
tar cvf "$d.tar" "$d"
fi
done
fi

sleep 3600
h=`date +%H`
done
nice
qjse
帖子: 5
注册时间: 2009-04-10 22:10

Re: 哪里错了呢?

#10

帖子 qjse » 2009-04-15 12:56

测试了一下,不能运行。
还有就是,这个程序需要添加什么参数吗?
pope123
帖子: 34
注册时间: 2008-09-21 1:01

Re: 哪里错了呢?

#11

帖子 pope123 » 2009-04-16 9:48

shyl 写了:这样行不?

#!/bin/bash
cd ubuntu
h=`date +%H`
while [ 1 -eq 1 ]; do
if [ $h -lt 7 -o $h -ge 23 ]; then
for d in *; do
if [ -d "$d" ]; then
tar cvf "$d.tar" "$d"
fi
done
fi

sleep 3600
h=`date +%H`
done
这个程序可以运行阿,不用加任何参数的,把它放在跟ubuntu文件夹同一层的目录下运行就可以了。
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 哪里错了呢?

#12

帖子 hellojinjie » 2009-04-16 16:36

其实我想写个来练练手,但是楼主的需求描述不太详细啊
Say hello to everyday!
回复