Created
September 13, 2010 22:03
-
-
Save roktas/578140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PROGNAME=${0##*/} | |
# Veritabanından bir tabloyu CSV olarak ithal et. | |
whisper() { | |
local name description value | |
name="$1" | |
description="$2" | |
printf >&2 "$description " | |
stty -echo 2>/dev/null | |
read value | |
stty sane 2>/dev/null | |
echo >&2 | |
eval $(echo "$name=\"$value\"") | |
} | |
ask() { | |
local name description value | |
name="$1" | |
description="$2" | |
default="$3" | |
printf >&2 "$description" | |
if [ -n "$default" ]; then | |
printf >&2 " [$default]" | |
fi | |
printf >&2 " " | |
read value | |
if [ -z "$value" ]; then | |
value="$default" | |
fi | |
eval $(echo "$name=\"$value\"") | |
} | |
ask dbname "db ismi (ör. moodle)" | |
# erişim sorunu olursa "root" hesabını kullan | |
ask dbuser "'$dbname' db kullanıcısı" "$dbname" | |
ask dbserver "db sunucusu" "localhost" | |
ask dbtable "table to be exported?" | |
whisper dbpass "'$dbname' db kullanıcı parolası" | |
csvfile="${dbname}-${dbtable}-$(date +'%Y%m%d').csv" | |
if [ -L "$csvfile" ]; then | |
echo >&2 "Refusing to follow $csvfile which is a symlink" | |
exit 1 | |
elif [ -f "$csvfile" ] && ! [ -w "$csvfile" ]; then | |
echo >&2 "Existing file $csvfile is not writable" | |
exit 1 | |
fi | |
if ! mysql --password="$dbpass" -u "$dbuser" "$dbname" -f -B -e "set names 'utf8'; select * from $dbtable;" | | |
sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' >"$csvfile"; then | |
echo >&2 "Error in export." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment