Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Scripting] funktioniert auf shell aber nicht im Skript?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
disi
Veteran
Veteran


Joined: 28 Nov 2003
Posts: 1354
Location: Out There ...

PostPosted: Sun Jul 08, 2012 8:56 am    Post subject: [Scripting] funktioniert auf shell aber nicht im Skript? Reply with quote

Ich versuche hier etwas zu automatisieren aber habe Probleme mit den String, irgendwie?!?

Hier mal das Skript:
Code:
#!/usr/local/bin/zsh
#/usr/local/bin/bash

DIR="/tank1/data/backup/postgresql"

DATABASES=$(su -m pgsql -c 'psql -l')

echo ${DATABASES}

LIST=$(echo ${DATABASES} | awk '{ print $1}' | grep -vE '^-|^List|^Name|template[0|1]|^\(|^\|')
echo ${LIST}


echo ${DATABASES}, da gibt er noch die Namen aus
echo ${LIST}, hier ist die Variable leer :(

Auf der Kommandozeile tut er:
Code:
disi-disk# DATABASES=$(su -m pgsql -c 'psql -l'); echo ${DATABASES}; LIST=$(echo ${DATABASES} | awk '{ print $1}' | grep -vE '^-|^List|^Name|template[0|1]|^\(|^\|'); echo ${LIST}
                            List of databases
   Name    |  Owner   | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-----------+-------+-------------------
 my_wiki   | postgres | UTF8     | C         | C     |
 postgres  | pgsql    | UTF8     | C         | C     |
 template0 | pgsql    | UTF8     | C         | C     | =c/pgsql         +
           |          |          |           |       | pgsql=CTc/pgsql
 template1 | pgsql    | UTF8     | C         | C     | =c/pgsql         +
           |          |          |           |       | pgsql=CTc/pgsql
(4 rows)
my_wiki
postgres


Wie ihr seht, habe ich auch Bash versucht, kein Unterschied :(
_________________
Gentoo on Uptime Project - Larry is a cow
Back to top
View user's profile Send private message
disi
Veteran
Veteran


Joined: 28 Nov 2003
Posts: 1354
Location: Out There ...

PostPosted: Sun Jul 08, 2012 9:09 am    Post subject: Reply with quote

Arrrggghhh, ich hatte es immer so gestartet:
Code:
# sh peng.sh

also nimmt er /bin/sh und unter FreeBSD ist das auch sh und kuemmert sich nicht im die erste Zeile welche Shell er nehmen soll :idea:

//edit: vermutlich braucht er absolute Paths fuer grep etc?!? Damit es sauber ist und portable...

//edit: stimmt ja:
Code:
% ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jun 24 22:50 /bin/sh -> bash

Man wird irgendwann zu faul in Linux...
_________________
Gentoo on Uptime Project - Larry is a cow
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3919
Location: Hamburg

PostPosted: Sun Jul 08, 2012 1:41 pm    Post subject: Reply with quote

disi wrote:

//edit: vermutlich braucht er absolute Paths fuer grep etc?!? Damit es sauber ist und portable...
wohl eher für psql
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Jul 08, 2012 6:55 pm    Post subject: Reply with quote

Dein Script ist Shell-unabhängig, außer dass ggf. "${DATABASES}" gequoted werden sollte - dass zsh automatisch quotet dürfte der einzige Unterschied sein.
Solange Du nur POSIX-Optionen benutzt (was du tust) würde ich keine absoluten Pfade für "awk" und "grep" benutzen (außer Du hast Angst, dass jemand Dein Script mit verbogenem PATH aufruft, um ein eigenes Kommando einzuhängen - für diese Probleme sollte man allerdings besser an anderer Stelle ansetzen).
Wenn Du schon sowieso awk benutzt, solltest Du auch das "grep" von awk benutzen.
Zum eigentlichen Problem habe ich außer der erwähnten fehlenden Quotes keine Idee. :roll:
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3919
Location: Hamburg

PostPosted: Sun Jul 08, 2012 7:00 pm    Post subject: Reply with quote

Ich stochere auch nur im Nebel herum, dabei fällt mir dies hier auf :
Code:
#/usr/local/bin/bash

kann nixht funktionieren, da fehlt ein "!".
Außerdem immer daran denken, daß es nicht hilft, die shebang Zeile auszukommentieren, da ein "#!" nur in der ersten Zeile einer Datei eine spezielle Auswertung erfährt.
Back to top
View user's profile Send private message
disi
Veteran
Veteran


Joined: 28 Nov 2003
Posts: 1354
Location: Out There ...

PostPosted: Mon Jul 09, 2012 6:46 am    Post subject: Reply with quote

Jo, danke fuers Feedback :)

Es funktioniert wenn ich es so starte:
Code:
# ./pg_backup.sh

_________________
Gentoo on Uptime Project - Larry is a cow
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum