0

I'm trying to test my script by running it inside another script. I googled up some solutions and implemented them (btw - both scripts are in one directory) but I keep getting a following error.

./tr_znaki.sh: 13: ./tr_znaki.sh: Syntax error: "(" unexpected

Here is my code.

#!/bin/bash
echo "this script is about to run another script"
a=ab
b=AB
echo "$a" 
echo "$b" 
sh ./tr_znaki.sh "$a" "$b"

Any ideas?

znaki_tr.sh

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
2
  • OK, so what does tr_znaki.sh look like? Commented Apr 7, 2016 at 21:49
  • What is the Line 13 of ./tr_znaki.sh? Commented Apr 7, 2016 at 21:50

2 Answers 2

2

The problem is you're running the second script with sh rather than bash. Arrays are a bash extension, so

ize1=${#arr1[@]} #zapisanie rozmiaru arr1 do zmiennej

can't be used with sh. Run the script with:

bash ./tr_znaki.sh "$a" "$b"

Or change tr_znaki.sh to begin with

#!/bin/bash

and run it with

./tr_znaki.sh "$a" "$b"
Sign up to request clarification or add additional context in comments.

2 Comments

I must have deleted it because on top of the file I had some comments for myself, but the line #!/bin/bash is present in the tr_znaki.sh file.
The shebang line only matters if you run the script as a command, it has no effect if you run sh or bash explicitly with the script as an argument.
1

I would recommend add your script to execute in a variable: Try something like this:

      #!/bin/bash
      tr_znakiScript="/Directory/From/My/Script/tr_znaki.sh"
      echo "this script is about to run another script"
      a=ab
      b=AB
      echo "$a" 
      echo "$b"
     . $tr_znakiScript "$a" "$b"

remove the sh at the beginning

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.