You are not logged in.

#1 2016-12-06 18:09:33

saultdon
Member
From: Treaty 08
Registered: 2013-06-25
Posts: 41

[SOLVED] zsh parse error near `\n' on last line

I made changes to some functions in my .zshrc.local file with nano and now zsh can't source it at all.

The nano command I use is

# nano -LES -T 4 -w$ .zshrc.local

I get this error when I open a new terminal:

/home/saultdon/.zshrc.local:102: parse error near `\n'

Line 102 is the last line in the file.

I tried deleting the last newline and even running dos2unix thinking it was a dos newline issue but still get the same result.

Here's some info that might be useful:

% ls -lah
...
lrwxrwxrwx   1 saultdon users   16 Feb 15  2014 .zshrc -> /etc/skel/.zshrc
lrwxrwxrwx   1 saultdon users   14 Feb 15  2014 .zshrc.global -> /etc/zsh/zshrc
lrwxrwxrwx   1 saultdon users   24 Mar 15  2014 .zshrc.local -> /usr/local/etc/zsh/zshrc
lrwxrwxrwx   1 saultdon users   28 Mar 15  2014 .zshrc.pre -> /usr/local/etc/zsh/zshrc.pre
% pacman -Ss zsh | grep installed
extra/grml-zsh-config 0.12.6-1 [installed]
extra/zsh 5.2-2 [installed]
community/zsh-completions 0.22.0-1 [installed]
community/zsh-syntax-highlighting 0.5.0-1 [installed]

Contents of .zshrc.local

% cat .zshrc.local
# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=100000000
SAVEHIST=$HISTSIZE
setopt appendhistory hist_ignore_all_dups autocd extendedglob nomatch
bindkey -e
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/saultdon/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

# enable colors!
autoload -U colors && colors

# The "command not found" hook for pkgfile
source /usr/share/doc/pkgfile/command-not-found.zsh

# aliases be here
alias nano='nano -LES -T 4 -w$'
alias ls='ls --color=auto'
alias dmesg='dmesg -HL'
alias makepkg='makepkg -Cfis --noconfirm'
alias reflector='sudo reflector --verbose -l 10 -f 10 -p https --country "Canada" --country "United States" --save /etc/pacman.d/mirrorlist'
alias rsync_del='rsync -hav4 --stats --progress --delete --exclude "System Volume Information" --exclude \$RECYCLE\.BIN --exclude \.directory'
alias rsync_dry='rsync -hav4 --stats --progress --delete --dry-run --exclude "System Volume Information" --exclude \$RECYCLE\.BIN --exclude \.directory'
alias cower='cower -c'

# functions for world peace
h() { if [ -z "$*" ]; then history 1; else history 1 | egrep "$@"; fi; }

mnt() { test -e $1 || if grep /mnt/$1 /proc/mounts; then sudo umount /mnt/$1; else sudo mount /mnt/$1; fi; }

spatialite_mkvalid() {
        for db in "${1}"
        do
                echo '  '... vacuuming "${db}"
                spatialite -silent "${db}" "PRAGMA page_size = 65536; PRAGMA page_cost = 100000; VACUUM;"

                for layer in $(ogrinfo "${db}" | tail -n+3 | cut -d ' ' -f 2)
                do
                        echo '    '... repairing "${layer}"
                        spatialite -silent "${db}" "UPDATE "${layer}" SET GEOMETRY = ST_MakeValid(GEOMETRY) WHERE ST_IsValid(GEOMETRY) = 0;"
                done
                echo '  '... repaired "${db}" and vacuuming
                spatialite -silent "${db}" "VACUUM;"
        done
}

asus_fan() {
        sudo modprobe -r asus-fan
        sudo modprobe asus-fan
        sudo asus-fan-create-symlinks.sh
        sudo systemctl restart fancontrol
}

fix_geom() {
        for tbl in $(ogrinfo pg:"host=sfngis dbname=witaskewin" | grep ${1} | cut -d ' ' -f 2)
        do
                psql -h sfngis -d witaskewin -a << EOF
        done
                UPDATE ${tbl} SET geom = ST_MakeValid(geom) WHERE ST_IsValid(geom) IS FALSE;
                VACUUM ANALYZE ${tbl};
                REINDEX TABLE ${tbl};
        EOF
}

truncate_tbl() {
        for tbl in $(ogrinfo pg:"host=sfngis dbname=witaskewin" | grep ${1} | cut -d ' ' -f 2)
        do
                psql -h sfngis -d witaskewin -a -c "TRUNCATE ${tbl} RESTART IDENTITY;"
        done
}

# zsh-syntax-highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# Pacman alias examples
alias pacupg='sudo pacman -Syu'         # Synchronize with repositories and then upgrade packages that are out of date on the local system.
alias pacin='sudo pacman -S'            # Install specific package(s) from the repositories
alias pacins='sudo pacman -U'           # Install specific package not from the repositories but from a file 
alias pacre='sudo pacman -R'            # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns'         # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacrep='pacman -Si'               # Display information about a given package in the repositories
alias pacreps='pacman -Ss'              # Search for package(s) in the repositories
alias pacloc='pacman -Qi'               # Display information about a given package in the local database
alias paclocs='pacman -Qs'              # Search for package(s) in the local database
alias paclo="pacman -Qdt"               # List all packages which are orphaned
alias pacc="sudo pacman -Scc"           # Clean cache - delete all not currently installed package files
alias paclf="pacman -Ql"                # List all files installed by a given package
alias pacexpl="pacman -D --asexp"       # Mark one or more installed packages as explicitly installed 
alias pacimpl="pacman -D --asdep"       # Mark one or more installed packages as non explicitly installed

# '[r]emove [o]rphans' - recursively remove ALL orphaned packages
alias pacro="pacman -Qtdq > /dev/null && sudo pacman -Rns \$(pacman -Qtdq | sed -e ':a;N;$!ba;s/\n/ /g')"

# Additional pacman alias examples
alias pacupd='sudo pacman -Sy && sudo abs'         # Update and refresh the local package and ABS databases against repositories
alias pacinsd='sudo pacman -S --asdeps'            # Install given package(s) as dependencies
alias pacmir='sudo pacman -Syy'                    # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist

Last edited by saultdon (2016-12-06 18:48:45)


"We don't inherit the land from our ancestors - we borrow it from our children."

Offline

#2 2016-12-06 18:18:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,525
Website

Re: [SOLVED] zsh parse error near `\n' on last line

The EOF on line 67 needs to be unindented.  Otherwise the here document never ends.

Also you probably should get rid of that pacupd and pacmir aliases - they will not cause parsing issues, but an alias for commands that you should really never use doesn't serve much purpose.  If you do regularly use those commands, you are essentially playing russion roulette with your installation.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-12-06 18:21:13

saultdon
Member
From: Treaty 08
Registered: 2013-06-25
Posts: 41

Re: [SOLVED] zsh parse error near `\n' on last line

Trilby wrote:

The EOF on line 67 needs to be unindented.  Otherwise the here document never ends.

Thanks just did that and also had to move the closing curly bracket so it looked like

EOF}

at the end of the function.


"We don't inherit the land from our ancestors - we borrow it from our children."

Offline

#4 2016-12-06 18:23:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,525
Website

Re: [SOLVED] zsh parse error near `\n' on last line

Actually the done is out of place.  That really makes no sense the way it is written.  The for loop doesn't get a done, you just have "done" as the first line of each here document.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2016-12-06 18:26:46

saultdon
Member
From: Treaty 08
Registered: 2013-06-25
Posts: 41

Re: [SOLVED] zsh parse error near `\n' on last line

So I need to place done after the EOF?

From the command line, this works:

for tbl in $(ogrinfo pg:"host=sfngis dbname=witaskewin" | grep econ_ | cut -d ' ' -f 2); do psql -h sfngis -d witaskewin -a << EOF; done
UPDATE ${tbl} SET geom = ST_MakeValid(geom) WHERE ST_IsValid(geom) IS FALSE;              
VACUUM ANALYZE ${tbl};
REINDEX TABLE ${tbl};
EOF

And the function actually works as expected when I call it. But just today the \n error came up and this wasn't the function I was messing with.


"We don't inherit the land from our ancestors - we borrow it from our children."

Offline

#6 2016-12-06 18:30:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,525
Website

Re: [SOLVED] zsh parse error near `\n' on last line

What you just posted is completely different from what is in your shellrc file posted in the first post.  What you just posted will work, but it is hard to read.  Putting the "done" on the life after the EOF at the end of the here document will always work and is easier for humans to parse.  Putting the "done" inside the here document as you have done in your rc file will never work.

Note that the following two examples are completely different:

cat <<EOF > outfile
Hello World
EOF
cat <<EOF
> outfile
Hello World
EOF

The first prints "hello world" into a file called outfile and outputs nothing.  The second one only writes to the stdout printing the text "> outfile" and "Hello world".  A here document begins at the first line after the starting "EOF" symbol.

For a loop consider three formats:

# Example A:
for a in INPUT; do
   SOME_COMMAND << EOF
here document contents here
more here document contents here
EOF
done

# Example B:
for a in INPUT; do
   SOME_COMMAND << EOF; done
here document contents here
more here document contents here
EOF

# Example C:
for a in INPUT; do
   SOME_COMMAND << EOF
done
here document contents here
more here document contents here
EOF

A or B will work as desired.  But B is confusing to a human reader while A is easier to understand.
C will just completely fail as the "done" command is just part of the here document and the for loop
is never closed.

In every case, the closing "EOF" must be unindented at the start of the line.  Here documents are handy, but by necessity they often screw up the indentation we use to visually separate logical blocks.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2016-12-06 18:39:57

saultdon
Member
From: Treaty 08
Registered: 2013-06-25
Posts: 41

Re: [SOLVED] zsh parse error near `\n' on last line

Yup, cause that's what I typed at the command line to make sure it worked before making it a function (which was also working since I made it a function months ago) - so I got confused about the error today.

But hey! for some reason placing the done at the end of the function, after EOF worked like you've pointed out.
Not sure why it worked before in it's original form then all of a sudden started getting the error after changing the mnt() function...

But looks like all is well, thank a lot for your help.


"We don't inherit the land from our ancestors - we borrow it from our children."

Offline

#8 2016-12-06 18:48:29

saultdon
Member
From: Treaty 08
Registered: 2013-06-25
Posts: 41

Re: [SOLVED] zsh parse error near `\n' on last line

And thanks again for your helpful for loop education.

I can see how examples B and C are different and just assumed that the ';' could be translated to a newline when making a function but apparently not.


"We don't inherit the land from our ancestors - we borrow it from our children."

Offline

Board footer

Powered by FluxBB