ninigi
ninigi

Reputation: 143

Run script A inside a script B, and then exit script B without exiting script A

I'm writing a test for a script which I have written and works like the simplest tr command. My idea for a test was that I would run in parallel tr command and my script, save output to variables and compare the variables. First of all, how to stop script from runnning forver? I enclose codes of both the test and my scipt. Secondly, how to save output of my script and tr to a variable? I've tried var=$(function), but it's not working. And is there a way to call them in parallel? Eg. I'll start the test and then write 'abba' and I would like both scripts to change it in pallalel to 'ABBA'.

And is it a good idea? I've never written 'formal' tests before.

Test code:

#!/bin/bash
 **tr_znakiScript="/home/wiktoria/skrypty/testy/tr_znaki.sh"
  echo "this script is about to run another script"
  a=ab
  b=AB
  echo "$a" 
  echo "$b"

  . $tr_znakiScript "$a" "$b"

  exit 1

 # x=2;
#while [ $x -le 2 ]; do
#echo "Napis pojawił się po raz: $x"
#x=$[x + 1]
#hash=$(. $tr_znakiScript "$a" "$b")
#. $tr_znakiScript "$a" "$b"
#echo $hash
#done
#echo $hash**

tr_znaki.sh code

#!/bin/bash
first_two="$1 $2" #zapisuje dwia pierwsze znaki do wspolnej zmiennej
a="$1";b="$2" #rozdziela znaki
split1=$(echo $a | fold -w 1) #rozdziela a na litery i wypisuje
split2=$(echo $b | fold -w 1) #rozdziela a na litery i wypisuje
arr1=($split1) #zapisanie stringu do tablicy
arr2=($split2) #zapisanie stringu do tablicy

shift #usuwa pierwszy argument
shift #usuwa drugi argument


size1=${#arr1[@]} #zapisanie rozmiaru arr1 do zmiennej
size2=${#arr2[@]} #zapisanie rozmiaru arr2 do zmiennej

# nieskończona pętla pozwalająca na ciągłe wpisywanie tekstu do translacji, przerywana standardowo ctrl + c
while true
do 
    read tekst #zmienna, do której wpisywany jest przez użytkownika tekst
    mod=$tekst #zapisanie wpisanego tekstu do zmiennej
    for (( i=0; i<${size1}; i++ ));  
    do
        mod=${mod//[${arr1[i]}]/${arr2[i]}} #nowa zmienna z zamianą którejś z wartości w arr na odpowiadającą wartość w arr2
    done
    echo $mod #wypisanie zmienionego tekstu
    #sleep 1 #czeka sekundę

done

Upvotes: 2

Views: 178

Answers (1)

ForceMagic
ForceMagic

Reputation: 506

Try nohup.

Example:

$(nohup ./your/script/here.sh > somefile.out 2> somefile.err < /dev/null &)

The nohup command basically allows you to run a process in the background, but in the meantime, you CAN NOT interact with the program by any means (except for terminating it). (See nohup docs for details)

You can check somefile.out for your output.

Also, for terminating you can use top (process manager) or kill -SIGTERM PID (this can be used in your script: $(kill -SIGTERM PID)). (PID is your Process ID, which can also be found in top.)

For comparing your outputs, you can use ./your/script/A.sh > file.out and then you can use the diff file.out somefile.out for checking the differences of the two files.

Upvotes: 2

Related Questions