bash script-how can user choose how long to run it for?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash script-how can user choose how long to run it for?
I am currently writing a bash script to output the statistics of vmstat, iostat and other commands. The script allows the user to type in how long they wish to run it for and how many repetitions.
I have been running stress on Ubuntu with stress -c 2 -t 40s for example and it stresses for 40 seconds. I would like however to run stress in bash and for the user to select how long they want to run stress for and for the system to read this and then exit afterwards.
This is part of the code for vmstat/iostat one, which works properly and responds to the user input and then returns to the main menu ok. I would prefer to have the stress input in a different script but what is the syntax for the user to choose how long to run stress for? If I have used vmstat $INT even on its own, the script just freezes.
#!/bin/bash
#
# Script to output system statistics
echo "Enter the number of intervals (seconds) for the sar statistics:"
read int
echo "enter the number of output lines (count) to process:"
read cnt
clear
#set while
while true; do
clear
echo "*******************************"
echo "* Please choose an option *"
echo "*******************************"
echo "* [1] To view process creation statistics *"
echo "* [2] To view I/O and transfer rate statistics *"
echo "* [3] To view paging statistics *"
echo "Press A to quit."
echo "************************"
echo -n "Enter your menu choice [1-3]: "
D=`/bin/date '+%B.%d'`
INT=$int
CNT=$cnt
#Variable to read input from the keyboard.
read mychoice
case $mychoice in
1) echo "process creation statistics";
vmstat $INT $CNT |tee /tmp/sar_proc_stat_$D
echo "This file will be saved to /tmp/sar_pc_stat_$D"
sleep 3 ;;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.