だから私はクラスのために書かなければならないライブラリスクリプトを持っています。addbook、deletebook、checkout など、いくつかの機能があります。問題は、私のチェックアウト機能にあります。
colin@Colins-Samsung:~$ bash --version GNU bash、バージョン 4.2.45(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL バージョン3 以降http://gnu.org/licenses/gpl.html
最初にここに私の文字列があります(コンマで区切られたフィールド)
フォームブック、著者、ライブラリ、日付
string= Unix for programmers,Graham Glass,mylibrary,11-11-13
コードの前の行でタイトル、ライブラリ、および日付を既に宣言していますが、著者を宣言しようとすると、このコード行を使用します
author=`awk -F, '/'$title'/ {print $2}' $library`
これが私の問題があるところだと思います
文字列に最終的に発生するのは、作成者が null になるため、関数が完了すると、文字列は次のようになります
Unix for programmers,,mylibrary,11-11-13
'/'$title'/ {print $2}' という行の部分で何かが起こっているように見えます。私の質問は何ですか?
私はもう試した
author=`awk -F, "/'$title'/ {print $2}" $library`
私も試してみました##
author=`egrep "$title" $library | awk -F, '{print $2}' $library`
しかし、両方のアカウントで、暴走した正規表現または無効なコマンドのいずれかのエラーが発生します。
これが私が修正しようとしている機能全体です
checkout(){
echo please enter your username
read username
uCount=`egrep "$username" $library | wc -l`
if (( $uCount >=3 ))
then
echo "Sorry, you can only have 3 books checked out at a time"
else
echo "what is the name of the book you would like to check out?"
read title
exists=`egrep "$title" $library | wc -l`
if (( $exists == 0 ))
then
echo "Sorry, but this book does not exist"
else
author=`awk -F, '/'$title'/ {print $2}' $library`
##author=`egrep "$title" $library | awk -F, '{print $2}' $library`
##author=`awk -F, "/'$title'/ {print $2}" $library`
##String = unix,graham glass,mylib,11-11-13
updated=$title,$author,$username,`date +%F`
sed -e "/$title/d" $library > $tmp
echo $updated >> $tmp
mv $tmp $library
echo "$title succesfully checked out"
fi
fi
お知らせ下さい。助けてくれてありがとう