So I have a library script I have to write for class. It has several functions, such as addbook, deletebook, checkout, etc. The problem is in my checkout function.
colin@Colins-Samsung:~$ bash --version GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
so firstly here is my string (fields seperated by commas)
form book,author,library,date
string= Unix for programmers,Graham Glass,mylibrary,11-11-13
I've already declared my title, library, and the date in previous lines of the code, but when I try to declare author I use this line of code
author=`awk -F, '/'$title'/ {print $2}' $library`
I believe this is where my problem lies
What ends up happening to the string is that the author becomes null so after my function completes, the string is now
Unix for programmers,,mylibrary,11-11-13
So it would seem that something happens in the piece of the line: '/'$title'/ {print $2}' My question is what?
I've tried
author=`awk -F, "/'$title'/ {print $2}" $library`
I've also tried ##
author=`egrep "$title" $library | awk -F, '{print $2}' $library`
But on both accounts, I get some error, either a runaway regular expression, or invalid command.
Here is the entire function I'm trying to fix
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
Please advise. Thanks in advance for any help
To add variable in to awk do like this.
awk -v var="$variable" '{$0=var}' file
or
awk '{$0=var}' var="$variable" file
So this:
author=`awk -F, '/'$title'/ {print $2}' $library`
should be like this
author=$(awk -F, '$0~var {print $2}' var="$title" $library)
PS its better to use parentheses var=$(code)
compare back tics
You have a LOT of shell errors in your script, it could fail in many ways for various inputs and data values. You also have logic problems. Imagine what would happen if a guy named Theo
wanted to use your library and you had 3 books in your library whose title contained the word Theory
? Poor old Theo would never be able to borrow a book!
The shell is an environment from which to call tools. It has programming constructs to help you sequence the calls to tools. That is all. You shouldn't be trying to us shell constructs to parse text files - that's what awk was invented to do and so it is very good at it.
If you'd like help writing this correctly, let us know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.