Bash 脚本缩进命令行

Bash 脚本缩进命令行

你好,我使用了以下脚本,该脚本运行并退出后,导致我的命令行在键入时不显示任何文本,并且每次回车后都不显示新行,而不是像

root@alix:~# 

root@alix:~# 

我明白了

root@alix:~# root@alix:~# root@alix:~# root@alix:~#  etc

当使用 ctrl c 时我得到

root@alix:~# 
            root@alix:~# 
                        root@alix:~#  etc

脚本如下:

#!/bin/bash

###
### Run command for given number of seconds then kill it
###

read -p "How long should I run for? ==> " count_secs
echo "Time specified: " $count_secs

if [ $count_secs -gt 0 ]
then
   ###
   ### number of seconds greater than zero
   ###

   watch -n 0.5 'iw dev wlan0 station dump | grep "signal avg" >> processmonitor.log' >>/dev/null & 

   ###
   ### assume that the PID of the command is $$
   ###
   my_PID=$!
   sleep $count_secs
   kill -15 $my_PID
fi

谢谢。

答案1

您可以使用以下代码而无需终止任何进程,在指定时间(秒)结束后它将退出。

#!/bin/bash
###
### Run command for given number of seconds then kill it
###
read -p "How long should I run for? ==> " count_secs
echo "Time specified: " $count_secs
# $SECONDS is a shell variable
# we can use it in conjuction with
# our user input 
while [ "$SECONDS" -le "$count_secs" ]
do
  # execute any commands here (put your code bellow and remove echo commands)
  echo `date +%r`
  echo "i am doing my work now"
  # sleep (in secs) before the next execution
  sleep 1
done
# when the count_secs elapses exit
exit 0

相关内容