Linux bash控制要强调多少cpu/vm/io

Linux bash控制要强调多少cpu/vm/io,linux,bash,ubuntu-12.04,stress-testing,debian-7.6.0,Linux,Bash,Ubuntu 12.04,Stress Testing,Debian 7.6.0,我创建了一个脚本来强调虚拟机的cpu、vm和io,并将结果写入文件。我以这样的方式编写了脚本,它将读取用户输入,以秒为单位显示他们希望运行每个测试的时间。然而,我不得不硬编码的CPU数量等为4。如何更改此脚本,以便用户可以选择除了多长时间之外还要强调多少cpu/vm/io 脚本如下: #!/bin/bash # # Script to get stress statistics# echo "Enter the number of intervals (seconds) for running

我创建了一个脚本来强调虚拟机的cpu、vm和io,并将结果写入文件。我以这样的方式编写了脚本,它将读取用户输入,以秒为单位显示他们希望运行每个测试的时间。然而,我不得不硬编码的CPU数量等为4。如何更改此脚本,以便用户可以选择除了多长时间之外还要强调多少cpu/vm/io

脚本如下:

#!/bin/bash
#
# Script to get stress statistics#

echo "Enter the number of intervals (seconds) for running the stress statistics:"


read seconds_to_run
# the variable "seconds_to_run" now has the value entered by keyboard

# The stress option chosen will be output to the corresponding file and timestamped with today's date.
out_cpufilename="/tmp/stress_cpu_stat_"$( date +"%B.%d" )
out_vmfilename="/tmp/stress_vm_stat_"$( date +"%B.%d" )
out_iofilename="/tmp/stress_io_stat_"$( date +"%B.%d" )


while true; do
    clear
    echo "*******************************"
    echo "* Choose from the following: *"
    echo "*******************************"
    echo "* [1] To stress cpu statistics *"
    echo "* [2] To view stress vm statistics *"
    echo "* [3] To view stress io statistics *"
    echo "Press A to quit."
    echo "************************"
    echo -n "Enter your menu choice [1-3]: "

    read choice


    case  $choice in    
        1) echo "stress cpu statistics"
            stress -c 4 -t $choice |tee $out_cpufilename 
            echo "This file will be saved to $out_cpufilename"
            sleep 3 ;;

        2) echo "stress cpu statistics"
            stress -m 4 -t $choice |tee $out_vmfilename 
            echo "This file will be saved to $out_vmfilename"
            sleep 3 ;;

        3) echo "stress cpu statistics"
            stress -i 4 -t $choice |tee $out_iofilename 
            echo "This file will be saved to $out_iofilename"
            sleep 3 ;;


        A) #Quit option
            echo You have chosen to quit.
            exit;;   

        *) #Wildcard option in case user enters an invalid option such as "e"
            echo Invalid choice, please make another choice
            sleep 3;;


    esac
done

将为您提供机器的核心数量

您只需将核心数量读入一个变量:

echo -n "Number of cores: "
read cores
在你觉得最合适的位置加上这个。要使用变量而不是硬编码值,请将每次出现的
4
替换为
“$cores”


为了避免空格和特殊字符的典型问题,最好总是将Bash变量括在引号(
“$cores”
)或大括号(
${cores}
)中.

这是否允许用户选择要运行多少?例如,我希望系统说“您希望强调多少CPU”,如果用户输入2,则系统将读取用户输入。这会做同样的事情吗?只需将该值保存在一个变量中,然后进行读取以供用户选择。如果他输入一个,就用那个,否则。。。使用以前保存的值确定现在正在读取它们。我想将这些与vmstat etc如何在没有压力的情况下运行进行比较。可以将压力写入文本文件/csv以便我绘制图表吗?它可以与vmstat等同时运行吗?你现在会怎么做吗?为什么你必须硬编码数字为4?只是我找到的一本书中的一个例子,你知道如何更改它,以便用户可以选择多少而不是总是使用4?好的,现在正在阅读它们。我想将这些与vmstat etc如何在没有压力的情况下运行进行比较。可以将压力写入文本文件/csv以便我绘制图表吗?它可以与vmstat等同时运行吗?你现在能告诉我怎么做吗?
echo -n "Number of cores: "
read cores