Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 19882  / 1 Year ago, mon, february 13, 2023, 6:16:57

I need to run a program in the background. I know that using '&' after a command runs the command in the background.



In the following script:



#!/bin/bash
echo amt of time delay nsec
read nano
echo amt of time delay in sec
read sec
echo no.of times
read i
while [ $i -ne 0 ]
do
./nanosleep $sec $nano
./schello
i=$[i-1]
done


I have a few data that I get from the user. Is there a way I can run the program in background and while calling the program can I also specify the data required (like nano, sec,i) as arguments or through some other way?


More From » bash

 Answers
1

If your script is called, let say test.sh, then you can for example:




  • pipe your input:



    echo -e "1
    2
    3
    " | test.sh &


    where 1, 2, and 3 are the values for $nano, $sec and, respectively $i.


  • take the input from a file:



    test.sh < arguments.txt &

  • use a here string:



    test.sh <<< $'1
    2
    3
    ' &


[#26335] Tuesday, February 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rmiend

Total Points: 292
Total Questions: 101
Total Answers: 111

Location: Azerbaijan
Member since Tue, Aug 9, 2022
2 Years ago
rmiend questions
Tue, Jan 18, 22, 23:52, 2 Years ago
Sat, Sep 17, 22, 15:44, 2 Years ago
;