From 5804a7ce3e210cfd3f7ac1cce693a47004c7a00c Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Fri, 12 Apr 2002 09:37:11 +0000
Subject: [PATCH] Allow dropdb and others to use identifiers with spaces;  IFS
 cleanup for pg_dumpall.

---
 src/bin/pg_dump/pg_dumpall.sh | 16 ++++++++++------
 src/bin/scripts/createdb      | 10 +++++-----
 src/bin/scripts/createlang.sh | 10 +++++-----
 src/bin/scripts/createuser    | 10 +++++-----
 src/bin/scripts/dropdb        | 12 ++++++------
 src/bin/scripts/droplang      | 10 +++++-----
 src/bin/scripts/dropuser      | 10 +++++-----
 src/bin/scripts/vacuumdb      | 10 +++++-----
 8 files changed, 46 insertions(+), 42 deletions(-)

diff --git a/src/bin/pg_dump/pg_dumpall.sh b/src/bin/pg_dump/pg_dumpall.sh
index 6cae8b37115..6a8ec8d3ebc 100644
--- a/src/bin/pg_dump/pg_dumpall.sh
+++ b/src/bin/pg_dump/pg_dumpall.sh
@@ -6,7 +6,7 @@
 # and "pg_group" tables, which belong to the whole installation rather
 # than any one individual database.
 #
-# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.20 2002/04/11 21:22:27 momjian Exp $
+# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.21 2002/04/12 09:37:10 momjian Exp $
 
 CMDNAME="`basename $0`"
 
@@ -42,6 +42,7 @@ else
         fi
     done
 fi
+IFS="$_IFS"
 
 # As last resort use the installation directory. We don't want to use
 # this as first resort because depending on how users do release upgrades
@@ -218,6 +219,7 @@ while : ; do
         echo "  ALTER GROUP \"$GRONAME\" ADD USER \"$username\";"
     done
 done
+IFS="$_IFS"
 
 test "$globals_only" = yes && exit 0
 
@@ -234,12 +236,13 @@ exec 4<&0
 
 $PSQL -d template1 -At -F "$NL" \
     -c "SELECT datname, coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), pg_encoding_to_char(d.encoding), datistemplate, datpath FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) WHERE datallowconn ORDER BY 1;" | \
-while read DATABASE ; do
+while : ; do
     IFS="$NL"
-    read DBOWNER
-    read ENCODING
-    read ISTEMPLATE
-    read DBPATH
+    read DATABASE || break
+    read DBOWNER || break
+    read ENCODING || break
+    read ISTEMPLATE || break
+    read DBPATH || break
     IFS="$_IFS"
     if [ "$DATABASE" != template1 ] ; then
 	echo
@@ -261,6 +264,7 @@ while read DATABASE ; do
 	fi
     fi
 done
+IFS="$_IFS"
 
 $PSQL -d template1 -At -F "$NL" \
     -c "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;" | \
diff --git a/src/bin/scripts/createdb b/src/bin/scripts/createdb
index 52d734178dc..91d544f9e00 100644
--- a/src/bin/scripts/createdb
+++ b/src/bin/scripts/createdb
@@ -12,12 +12,12 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createdb,v 1.20 2002/02/24 23:27:58 tgl Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createdb,v 1.21 2002/04/12 09:37:10 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 MB=
 TEMPLATE=
@@ -42,7 +42,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -51,7 +51,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 	--username|-U)
 		PSQLOPT="$PSQLOPT -U $2"
@@ -60,7 +60,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
diff --git a/src/bin/scripts/createlang.sh b/src/bin/scripts/createlang.sh
index d2aabe30c55..229cb945245 100644
--- a/src/bin/scripts/createlang.sh
+++ b/src/bin/scripts/createlang.sh
@@ -7,12 +7,12 @@
 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.34 2002/02/18 23:11:30 petere Exp $
+# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.35 2002/04/12 09:37:10 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 PSQLOPT=
 dbname=
@@ -55,7 +55,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -64,7 +64,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 	--username|-U)
 		PSQLOPT="$PSQLOPT -U $2"
@@ -73,7 +73,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
diff --git a/src/bin/scripts/createuser b/src/bin/scripts/createuser
index 02e97c58c42..9f0088f0877 100644
--- a/src/bin/scripts/createuser
+++ b/src/bin/scripts/createuser
@@ -9,14 +9,14 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.23 2002/02/18 05:48:44 momjian Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.24 2002/04/12 09:37:10 momjian Exp $
 #
 # Note - this should NOT be setuid.
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 NewUser=
 SysID=
@@ -54,7 +54,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -63,7 +63,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 # Note: These two specify the user to connect as (like in psql),
 #       not the user you're creating.
@@ -74,7 +74,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
diff --git a/src/bin/scripts/dropdb b/src/bin/scripts/dropdb
index 42ff00a0a15..bd94bee4fc7 100644
--- a/src/bin/scripts/dropdb
+++ b/src/bin/scripts/dropdb
@@ -11,12 +11,12 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropdb,v 1.14 2002/02/18 05:48:44 momjian Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropdb,v 1.15 2002/04/12 09:37:11 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 PSQLOPT=
 dbname=
@@ -49,7 +49,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -58,7 +58,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 	--username|-U)
 		PSQLOPT="$PSQLOPT -U $2"
@@ -67,7 +67,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
@@ -136,7 +136,7 @@ if [ "$forcedel" = f ]; then
 fi
 
 
-dbname=`echo $dbname | sed 's/\"/\\\"/g'`
+dbname=`echo "$dbname" | sed 's/\"/\\\"/g'`
 
 ${PATHNAME}psql $PSQLOPT -d template1 -c "DROP DATABASE \"$dbname\""
 if [ "$?" -ne 0 ]; then
diff --git a/src/bin/scripts/droplang b/src/bin/scripts/droplang
index 579c570cf72..5e84b36e8a5 100644
--- a/src/bin/scripts/droplang
+++ b/src/bin/scripts/droplang
@@ -7,12 +7,12 @@
 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/droplang,v 1.21 2002/02/18 05:48:44 momjian Exp $
+# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/droplang,v 1.22 2002/04/12 09:37:11 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 PSQLOPT=
 dbname=
@@ -54,7 +54,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -63,7 +63,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 	--username|-U)
 		PSQLOPT="$PSQLOPT -U $2"
@@ -72,7 +72,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
diff --git a/src/bin/scripts/dropuser b/src/bin/scripts/dropuser
index 298710ee3b7..803b7caad15 100644
--- a/src/bin/scripts/dropuser
+++ b/src/bin/scripts/dropuser
@@ -9,14 +9,14 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropuser,v 1.15 2002/02/18 05:48:45 momjian Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/dropuser,v 1.16 2002/04/12 09:37:11 momjian Exp $
 #
 # Note - this should NOT be setuid.
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 PSQLOPT=
 forcedel=t
@@ -49,7 +49,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -58,7 +58,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 # Note: These two specify the user to connect as (like in psql),
 #       not the user you're dropping.
@@ -69,7 +69,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
diff --git a/src/bin/scripts/vacuumdb b/src/bin/scripts/vacuumdb
index b14991d178d..2a751d931b1 100644
--- a/src/bin/scripts/vacuumdb
+++ b/src/bin/scripts/vacuumdb
@@ -12,12 +12,12 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/vacuumdb,v 1.20 2002/02/18 05:48:45 momjian Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/vacuumdb,v 1.21 2002/04/12 09:37:11 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
 CMDNAME=`basename "$0"`
-PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
+PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`
 
 PSQLOPT=
 full=
@@ -43,7 +43,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --host=*)
-                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
+                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                 ;;
 	--port|-p)
 		PSQLOPT="$PSQLOPT -p $2"
@@ -52,7 +52,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --port=*)
-                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
+                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                 ;;
 	--username|-U)
 		PSQLOPT="$PSQLOPT -U $2"
@@ -61,7 +61,7 @@ do
                 PSQLOPT="$PSQLOPT $1"
                 ;;
         --username=*)
-                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
+                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                 ;;
 	--password|-W)
 		PSQLOPT="$PSQLOPT -W"
-- 
GitLab