Skip to content

Instantly share code, notes, and snippets.

@roktas
Last active February 7, 2019 23:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save roktas/8051467 to your computer and use it in GitHub Desktop.
Save roktas/8051467 to your computer and use it in GitHub Desktop.
Çeşitli kabuk programlama örnekleri
#!/bin/bash
# Mutt ile dosya yolla.
#
# Mutt'da 'set hostname="foo.bar"' ayarı yapılırsa alan adı verilmeyen alıcı
# adreslerde '@foo.bar' kullanılır. Bu ayarın isteğinize uygun şekilde
# yapıldığına emin olun.
# uyarı iletisi
cry() {
echo >&2 "$*"
}
# hata iletisi
die() {
cry "$@"
exit 1
}
# normal ileti
say() {
echo "$*"
}
# komut PATH içinde var mı?
hascommand() {
command -v "$1" >/dev/null
}
# öntanımlı değeri bekleterek kullanıcıdan girdi iste
ask() {
local prompt="$1"
local default="$2"
while :; do
unset REPLY
if [[ -n $default ]]; then
printf "$prompt [$default]? "
read -e REPLY </dev/tty
[[ -n $REPLY ]] || REPLY="$default"
else
printf "$prompt? "
read -e REPLY </dev/tty
fi
break
done
# cevap REPLY değişkeninde
}
hascommand mutt || die "Mutt kurulu olmalı."
FILES=("$@")
if [[ ${#FILES[@]} -eq 0 ]]; then
while :; do
ask "Ek dosya [ENTER son]"
[[ -n $REPLY ]] || break
eval f="$REPLY"
f=$(readlink -f "$f")
if [[ -f $f ]]; then
FILES+=("$f")
else
cry "Böyle bir dosya yok: '$f'"
fi
done
else
for ((i=0; i<${#FILES[@]}; i++)); do
f=$(readlink -f "${FILES[$i]}")
[[ -f $f ]] || die "Böyle bir dosya yok: '$f'"
done
fi
ask "Kime [birden fazla alıcı için boşluk kullanın]"
[[ -n $REPLY ]] || die "Çıktım."
TO="$REPLY"
ask "Konu" "Ekte dosya - $(date +'%Y-%m-%d %H:%M:%S')"
SUBJECT="$REPLY"
ask "İleti?" ""
BODY="$REPLY"
if echo "$BODY" | mutt -s "$SUBJECT" -a "${FILES[@]}" -- $TO; then
say "Gönderildi."
else
die "Gönderim başarısız."
fi
userinfos() {
getent passwd | while IFS=: read a b c d e f; do
echo login "$a" is "$d"
done
}
csource() {
if [ -z "$1" ]; then
echo "Missing operand";
return 1;
fi
local OUTPUT_PATH=$(echo "$1" | sed -e "s/^.*\/\|^/\/tmp\//" | sed -e "s/\.c$//");
gcc "$1" -o "$OUTPUT_PATH" && "$OUTPUT_PATH";
rm "$OUTPUT_PATH";
return 0;
}
extract() {
local c e i
(($#)) || return
for i; do
c=''
e=1
if [[ ! -r $i ]]; then
echo "$0: file is unreadable: \`$i'" >&2
continue
fi
case $i in
*.7z) c='7z x';;
*.Z) c='uncompress';;
*.bz2) c='bunzip2';;
*.exe) c='cabextract';;
*.gz) c='gunzip';;
*.rar) c='unrar x';;
*.zip) c='unzip';;
*)
echo "$0: unrecognized file extension: \`$i'" >&2
continue;;
esac
command $c "$i"
e=$?
done
return $e
}
cl() {
dir=$1
if [[ -z "$dir" ]]; then
dir=$HOME
fi
if [[ -d "$dir" ]]; then
cd "$dir"
ls
else
echo "bash: cl: '$dir': Directory not found"
fi
}
note () {
if [[ ! -f $HOME/.notes ]]; then
touch $HOME/.notes
fi
if [[ $# -eq 0 ]]; then
cat $HOME/.notes
elif [[ "$1" == "-c" ]]; then
echo "" > $HOME/.notes
else
echo "$@" >> $HOME/.notes
fi
}
todo() {
if [[ ! -f $HOME/.todo ]]; then
touch $HOME/.todo
fi
if [[ $# -eq 0 ]]; then
cat $HOME/.todo
elif [[ "$1" == "-l" ]]; then
cat -n $HOME/.todo
elif [[ "$1" == "-c" ]]; then
echo "" > $HOME/.todo
elif [[ "$1" == "-r" ]]; then
cat -n $HOME/.todo
echo -ne "----------------------------\nType a number to remove: "
read NUMBER
sed -ie ${NUMBER}d $HOME/.todo
else
echo "$@" >> $HOME/.todo
fi
}
docview () {
if [ -f $1 ] ; then
case $1 in
*.pdf) xpdf $1 ;;
*.ps) oowriter $1 ;;
*.odt) oowriter $1 ;;
*.txt) leafpad $1 ;;
*.doc) oowriter $1 ;;
*)
echo "don't know how to extract '$1'..."
;;
esac
else
echo "'$1' is not a valid file!"
fi
}
calc() {
echo "scale=3;$@" | bc -l
}
#!/bin/sh
# Kaynak: http://www.imagemagick.org/Usage/thumbnails/#square
[ $# -ge 2 ] || {
echo >&2 "Kullanım: $0 GİRDİ ÇIKTI"
exit 1
}
[ -f "$1" ] || {
echo >&2 "'$1' dosyası bulunamadı"
exit 1
}
convert "$1" \
\( +clone -rotate 90 +clone -mosaic +level-colors white \) \
+swap -gravity center -composite \
\( -frame 10%x10% -mattecolor white \) "$2"
#!/bin/sh
curl -s http://ifconfig.me
#!/bin/sh
PROGNAME="${0##*/}"
# TODO Çözünürlük değerini seçenek yap
DPI=${PNG_DPI:=600}
[ $# -gt 0 ] || {
echo >&2 "Kullanım: $PROGNAME <pdf-dosyası> [<sayfa-başlangıç>] [<sayfa-bitiş>]"
echo >&2 "PDF'den (öntanımlı olarak ${DPI} dpi yoğunluklu) PNG üretir."
echo >&2
echo >&2 "Örnekler: $PROGNAME foo.pdf 19 # foo-19.png dosyasını üretir."
echo >&2 " $PROGNAME foo.pdf 19 21 # foo-[0-2].png dosyalarını üretir."
echo >&2 " $PROGNAME foo.pdf # foo.pdf'teki tüm sayfaları üretir."
exit 1
}
INFILE="$1"
FIRST="$2"
LAST="$3"
alias rungs="gs -dBATCH -dNOPAUSE -sDEVICE=png16m -r'$DPI'"
if [ -z "$FIRST" ] && [ -z "$LAST" ]; then
rungs -sOutputFile="${INFILE%.*}-%03d.png" "$INFILE"
elif [ -z "$LAST" ]; then
# TODO sayfa numaralarını başlangıç ve bitiş ile aynı yap
rungs -dFirstPage="$FIRST" -dLastPage="$FIRST" -sOutputFile="${INFILE%.*}-${FIRST}.png" "$INFILE"
else
rungs -dFirstPage="$FIRST" -dLastPage="$LAST" -sOutputFile="${INFILE%.*}-%03d.png" "$INFILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment