Tuesday, April 3, 2018

bash - paste not running from script but working on command line




I'm running this one liner as part of a larger script-



paste -d '' <(cut -c"1-5" file1) <(cut -c"1-2" file2) <(cut -c"8-" file1)


This takes the first 5 characters of the first file file1, the characters 1-2 of file2 and then the characters 8 onwards of file1 and pastes them all together.




$cat file1
1234567890
1234567890
1234567890
1234567890
1234567890

$cat file2
abcdefghij
abcdefghij

abcdefghij
abcdefghij
abcdefghij

output-
12345ab890
12345ab890
12345ab890
12345ab890
12345ab890



This works perfectly as expected from the command line (output shown above).



However if I put that line into a script (shown here)-



$cat a.sh

#!/bin/bash
paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)



If I run the script I get this error-



$sh a.sh
a.sh: line 2: syntax error near unexpected token `('
a.sh: line 2: `paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)'


Any ideas what is going wrong here? I ran it through shellcheck and it told me the script was fine.



Answer



run as below;



./a.sh 


or



bash a.sh



bash and sh are two different shells. bash is with more features and better syntax.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...