とくにあぶなくないRiSKのブログ

危ないRiSKのブログだったかもしれない。本当はRiSKだけどググラビリティとか取得できるIDの都合でsscriskも使ったり。

「入門bash 第3版」を読む(8)

頭痛を抱える私が Windows 嬢の面倒を見ながら Cygwin + bash 錠を服用する記録。

進捗

134/348

task4-8 改

pushd()
{
	dirname=$1
	if [ -n "$dirname" ] && [ \( -d "$dirname" \) -a \( -x "$dirname" \) ]
	then
	    DIR_STACK="$dirname ${DIR_STACK:-$PWD}"
	    cd $dirname
	    echo "$DIR_STACK"
	else
	    echo "still in $PWD."
	fi
}
popd()
{
    if [ -n "$DIR_STACK" ];then
	DIR_STACK=${DIR_STACK#* }
	cd ${DIR_STACK%% *}
	echo "$PWD"
    else
	echo "stack empty, still in $PWD"
    fi
}

タスク5-1

fileinfo
if [ ! -e "$1" ];then
    echo "file $1 does not exist."
    exit 1
fi
if [ -d "$1" ];then
    echo -n "$1 is adirectory that you may "
    if [ ! -x "$1" ];then
	echo "not "
    fi
    echo "search."
elif [ -f "$1" ];then
    echo "$1 is a regular file."
else
    echo "$1 is a special type of file."
fi
if [ -O "$1" ];then
    echo "you own the file."
else
    echo "you do not own the file."
fi
if [ -r "$1" ];then
    echo "you have read permission on the file."
fi
if [ -w "$1" ];then
    echo "you have write permission on the file."
fi
if [ -x "$1" -a ! -d "$1" ];then
    echo "you have execute ermission on the file."
fi
実行結果
~ $>ls -l alice
-rwxrwxrwx 1 USER_______NAME なし 143 Nov 21 23:37 alice
~ $>. fileinfo alice
alice is a regular file.
you own the file.
you have read permission on the file.
you have write permission on the file.
you have execute ermission on the file.
~ $>

タスク5-2

listpath
finfo()
{
    if [ ! -e "$1" ];then
    echo "file $1 does not exist."
    exit 1
    fi
    if [ -d "$1" ];then
	echo -n "$1 is a directory that you may "
	if [ ! -x "$1" ];then
	    echo "not "
	fi
	echo "search."
    elif [ -f "$1" ];then
	echo "$1 is a regular file."
    else
	echo "$1 is a special type of file."
    fi
    if [ -O "$1" ];then
	echo "you own the file."
    else
	echo "you do not own the file."
    fi
    if [ -r "$1" ];then
	echo "you have read permission on the file."
    fi
    if [ -w "$1" ];then
	echo "you have write permission on the file."
    fi
    if [ -x "$1" -a ! -d "$1" ];then
	echo "you have execute ermission on the file."
    fi
}
for filename;do
    finfo "$filename"
    echo
done
実行結果
~ $>ls
albums  bash.txt  highest   myfile   task4-3  task4-5  task4-7
alice   fileinfo  listpath  task4-2  task4-4  task4-6  task4-8
~ $>. listpath *
albums is a regular file.
you own the file.
you have read permission on the file.
you have write permission on the file.
you have execute ermission on the file.

(以下省略)

タスク5-3

task5-3
recls()
{
    singletab='\t'
    for tryfile;do
	echo $tryfile
	if [ -d "$tryfile" ];then
	    thisfile=$tryfile
	    recdir $(command ls $tryfile)
	fi
    done
    unset dir singletab tab
}

recdir()
{
    tab=$tab$singletab
    for file;do
	echo -e $tab$file
	thisfile=$thisfile/$file
	if [ -d "$thisfile" ];then
	    recdir $(command ls $thisfile)
	fi
	thisfile=${thisfile%/*}
    done
    tab=${tab%"$singletab"}
}
実行結果
~ $>. task5-3
~ $>recls /srv
/srv
        www
                cgi-bin
                        printenv
                        test-cgi
                htdocs
                        apache_pb.gif
                        apache_pb.png
                        apache_pb22.gif
                        apache_pb22.png
                        apache_pb22_ani.gif
                        index.html
~ $>