0
mccli dataset show --recursive  \
| awk -F"/" '!/^Dataset.*Domain|^0,23000|^----|^$/ {print $2"/"$1}' \
| while read dataset
  do
    echo $dataset
    mccli dataset show --name="$dataset" | grep -i "freezemethod:value=none"
done

It is working in one of the Linux machine..When i copy same script to other Linux. I am getting below error. (I am pasting after insert in vi editor) .

./freezemethod: line 2: syntax error near unexpected token `|'
'/freezemethod: line 2: `| awk -F"/" '!/^Dataset.*Domain|^0,23000|^----|^$/ {print $2"/"$1}' \
pmverma
  • 1,653
  • 13
  • 27

1 Answers1

2

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.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • No, There is no charecters after first line "\", Still i am getting same error.... – anoop srinivasamurthy Mar 23 '15 at 02:22
  • @anoop, I've updated the question with extra information, the best way to detect if there's an actual problem with the line endings. It seems odd that that's not the problem, given the exact same error message in my test program, but the extra information should go some way towards figuring that out. Just make sure you run it on the _failing_ script, not the original. – paxdiablo Mar 23 '15 at 02:34
  • 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 – anoop srinivasamurthy Mar 23 '15 at 06:53
  • @anoop, doesn't matter, you posted just enough to diagnose the problem. See my update for further analysis and the fix. – paxdiablo Mar 23 '15 at 07:42
  • Thank you.. I will look forward for your diagnose and let me know if you need anything else for investigating this....... – anoop srinivasamurthy Mar 24 '15 at 01:45
  • @anoop, it's done. I updated the answer yesterday with the results of the analysis - you have carriage returns at the end of your lines, meaning the backslash characters are not being treated as line continuation characters. You need to get rid of those CR characters, which is where the link at the bottom of the answer comes in. – paxdiablo Mar 24 '15 at 03:39
  • @paxdiabo :- How to get rid of those CR charecters? i am sorry if i am missing something here... – anoop srinivasamurthy Mar 25 '15 at 04:02
  • @anoop, follow the link at the end of my last sentence, repeated here: http://stackoverflow.com/questions/800030/remove-carriage-return-in-unix/800644#800644 - it shows how to modify files from CRLF format to standard UNIX LF format. – paxdiablo Mar 25 '15 at 04:52
  • @paxdiabo:- i ran "cat freezemethod | od -c" " 0000354". I Ran this and got 0000354 charecters and ran sed command as you mentioned "cat freezemethod | sed 's/\r$//' | od -c" , the number of charecters changed from 254 to 345 and i did not see any \r after that, But still getting same error :( – anoop srinivasamurthy Mar 26 '15 at 03:31
  • @paxdiabo :- Okay, I got the issue, Every time we delete the extra space, it is getting created when the run the "od -c" command... How is it possible to create space or extra charecter automatically though we are getting rid of that using SED command? How can we solve this issue? – anoop srinivasamurthy Mar 26 '15 at 04:10
  • @anoop, `cat freezemethod | sed 's/\r$//' | od -c` doesn't actually _change_ the `freezemethod` file, it just modifies what's coming out of it for this pipeline. You need to do something like: `mv freezemethod frz-orig` to save it, then `sed 's/\r$//' frz-orig >freezemethod` to remake the file without newlines. I'll add that to the answer. – paxdiablo Mar 26 '15 at 05:19