Tuesday, 3 April 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

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...