Check to ensure that the \
at the end of line 1 has no characters following it (spaces, tabs, and so on). If it does have characters after it, it won't be treated as a line continuation, and line 2 will be treated as a new command, invoking an error something like what you're seeing:
./freezemethod: line 2: syntax error near unexpected token '|'
The best way to check this is to run a dump command like:
od -xcb ./freezemethod | head -30l
and examine the binary information for the first two lines of your file, the one that doesn't work.
By way of confirmation, the following file testprog.sh
:
echo hello \
| cut -c1-2
when run with bash testprog.sh
, will output the first two characters of hello
:
he
If you place a space character after the \
, you'll see:
hello
testprog.sh: line 2: syntax error near unexpected token '|'
testprog.sh: line 2: '| cat'
with the echo
working fine without the filter (outputting the full word), and the separate command causing issues because it begins with |
.
And, based on what you posted in a comment, the problem is exactly what I described. Your od
command produced (my addition to the last line):
root@w2ran0301:/tmp/#: od -xcb ./freezemethod | head -30l
0000000 636d 6c63 2069 6164 6174 6573 2074 6873
m c c l i d a t a s e t s h
155 143 143 154 151 040 144 141 164 141 163 145 164 040 163 150
0000020 776f 2d20 722d 6365 7275 6973 6576 5c20
o w - - r e c u r s i v e \
157 167 040 055 055 162 145 143 165 162 163 151 166 145 040 134
0000040 0a0d 207c 7761 206b 462d 2f22 2022 2127
^^^^
The 0a0d
sequence is (you have to read it reversed) actually a CR/LF
sequence which means your first line is:
mccli dataset show --recursive \^M
(with ^M
representing the CR at the end of the line).
That means there is a character between the \
and the end of the line, so the \
is escaping it rather than acting as a line continuation character.
There are many options for removing those CR characters from files, such as those shown in this answer.
One method would be to first back up the file with:
cp freezemethod freezemethod-cr
and then use that backup file to recreate the original without the carriage returns:
sed 's/\r$//' freezemethod-cr >freezemethod
This should give you a freezemethod
with the correct line endings.