I am working on an option for my program and it should work like this:
user inputs title
user inputs author
system then checks user's title & author input in the text file named BookDB.txt
if there is already existing record in the text file, system will prompt an error
else it will continue user to input price, quantity available and quantity sold.
book will then be added
I tried playing around with grep but to no avail.
Below are my codes for this particular function.
function fnAddBook()
{
echo "Title: "
read inputTitle
echo "Author: "
read inputAuthor
if grep -Fq "$inputTitle" BookDB.txt; then
if grep -Fq "$inputAuthor" BookDB.txt; then
echo "Error!"
fi
else
echo "Price: "
read inputPrice
echo "$inputTitle:$inputAuthor:$inputPrice" >> BookDB.txt
echo "New Book successfully added!"
fi
}
contents of BookDB.txt
format of the contents | Title:Author:Price:QtyAvail:QtySold
Hello World:Andre:10.50:10:5
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
UPDATED PROBLEM:
In this case, even if I typed "Catch Me If You Can" as title + "Ubuntu Team" as Author, it raises the error. How can I modify the codes such that it checks line by line?
Thanks in advance to those who helped! :)
There are 3 problems with your code.
The first is that the x option to grep causes it to match only complete lines, and since you put author and title on the same line, this will not match.
With the x option "Gaiman" does not match "Gaiman:Nation:$20", if you remove the x from the grep-options, this will work.
The second problem is that the two greps are independent of eachother. Thus if you have a book titled 'Nation' and a book by 'Gaiman' it will be considered a match, even if the 'Nation' book you have is 'The wealth of Nations' and the Gaiman book you've got is 'Anansi Boys'.
The third problem is that grep will find partial matches. If you try to enter the book "It", then grep will conclude it's already in the database, because "It came from the desert" is.
You need a sentinel-value to delineate the titles to fix this. (the sentinel must be some character that cannot exist in book-titles or author-names)
function fnAddBook()
{
echo "Title: "
read inputTitle
echo "Author: "
read inputAuthor
if grep -Fq "$inputTitle:inputAuthor:" BookDB.txt
then
echo "Error!"
else
echo "Price: "
read inputPrice
echo "$inputTitle:$inputAuthor:$inputPrice" >> BookDB.txt
echo "New Book successfully added!"
fi
}
This assumes that ':' cannot occur in authornames or booknames.
Related
I'm trying to combine two lists containing names (if available) and emails with a standard email text in bash (shell)
(I had to delete the irrelevant code as it contains some private info, so some of the code might look unusal.)
The first half of the code checks if there is a name list along with the email list.
The second half combines only the email address and text if no name is available, if the name list is available it also 'tries' to combine the name, email and text.
f1 = email list and f2 = name list.
As you can see in the first half of the code below, $f2 should show the names if the list is available but it does not show anything in the log file.
I been trying to sort this problem out for two days but nothing has worked. When names are available it always outputs as "Hello ..." when it should be "Hello John D..."
#FIRST HALF
if [ "$names" = "no" ]
then
text="Hello..."
elif [ "$names" = "yes" ]
then
text="Hello $f2..."
fi
#SECOND HALF
if [ "$names" = "no" ]
then
for i in $(cat $emaillist); do
echo "$text" >> /root/log
echo "$i" >> /root/log
done
elif [ "$names" = "yes" ]
then
paste $emaillist $namelist | while IFS="$(printf '\t')" read -r f1 f2
do
echo "$text" >> /root/log
echo "$f1" >> /root/log
done
fi
When you run text="Hello $f2", $f2 is looked up at the time of the assignment; an exact string is assigned to text, and only that exact string is used later, on echo "$text".
This is very desirable behavior: If shell variables' values could run arbitrary code, it would be impossible to write shell scripts that handled untrusted data safely... but it does mean that implementing your program requires some changes.
If you want to defer evaluation (looking up the value of $f2 at expansion time rather than assignment), don't use a shell variable at all: Use a function instead.
case $names in
yes) write_greeting() { echo "Hello $name..."; };;
*) write_greeting() { echo "Hello..."; };;
esac
while read -r name <&3 && read -r email <&4; do
write_greeting
echo "$email"
done 3<"$namelist" 4<"$emaillist" >>/root/log
Some enhancements in the code above:
You don't need paste to read from two streams in lockstep; you can simply open them on different file descriptors (above, FDs 3 and 4 are chosen; only 0, 1 and 2 are reserved, so larger numbers could have been selected as well) with a separate read command for each.
Opening your output sink only once for the entire loop (by putting the redirection after the done) is far more efficient than re-opening it every time you want to write a single line.
Expansions, such as "$namelist" and "$emaillist", are always quoted; this makes code more reliable if dealing with filenames with unusual characters (including spaces and glob expressions), or if IFS is at a non-default value.
Am writing a script.Eg:
echo "my name is 'read' and am from 'read' city" > outfile.txt
When it runs it's not printing the sentence first, i.e my name is. Rather it's asking first to enter 2 inputs for 2 read commands used, then its forming the complete sentence like "my name is sudhir and am from vizag city"
I want script to execute 1st "my name is read(ask for input) and am from read(ask for another input) city" and after giving inputs it should redirect to outfile.txt in one shot.
How to handle this? Is it feasible to achieve in single sentence?
Because I want to use same logic for 480 questions all in one file and persons how don't have any scripting knowledge should able to add more questions taking reference of previous questions present in same file.
There isn't a pretty way to do this, since read writes the newline character from the input to the terminal, but we can do this in two passes.
Get the input from the user
Write the results to the file
You could put the following in a script
#!/bin/bash
echo -n 'my name is '; read -r name
echo -n ' and I am from '; read -r city
echo ' city'
printf "my name is %s and I am from %s city\n" \
"$name" "$city" > output.txt
And to the user it would look like this
my name is sudhir
and I am from vizag
city
But it would be like this in the file
my name is sudhir and I am from vizag city
You can write a function that takes a string with placeholders and asks the user to input for each of them:
#!/bin/bash
fill() {
arg="$*"
result=""
while [[ "$arg" =~ ([^_]*)(_+)(.*) ]]
do
read -rp "${BASH_REMATCH[1]# }${BASH_REMATCH[2]}: " input
result+="${BASH_REMATCH[1]}${input}"
arg="${BASH_REMATCH[3]}"
done
result+="$arg"
printf '%s\n' "$result"
}
exec > outputfile
fill "My name is ____ and I am from ___."
fill "My new years resolution is ____."
Example:
$ ./myscript
My name is ____: Sudhir
and I am from ___: Vizag
My new years resolution is ____: learning Bash instead of asking SO to write my scripts
$ cat outputfile
My name is Sudhir and I am from Vizag.
My new years resolution is learning Bash instead of asking SO to write my scripts.
ok.. i'm having trouble with updating new book title.. i've already searched for solutions on this website.. and ive tried them but none of them work..
function update_book
{
#echo "Title: "
read -p $'Title: ' updatetitle
#echo "Author: "
read -p $'Name: ' updatename
if grep -Fq "${updatetitle}:${updatename}" BookDB.txt
then
echo "Book found!"
if ! [ -f BookDB.txt ] ; then
touch BookDB.txt
fi
selection=0
until [ "$selection" = "f" ]; do
echo ""
echo ""
echo "Book Update System"
echo ""
echo ""
echo "a) Update title"
echo "b) Update Author"
echo "c) Update Price"
echo "d) Update Qty Available"
echo "e) Update Qty Sold"
echo "f) Back to main menu"
echo -n "Enter your option: "
read selection
echo ""
case $selection in
a) upd_title;press_enter;;
b) upd_author;press_enter;;
c) upd_price;press_enter;;
d) upd_qty_avail;press_enter;;
e) upd_qty_sold;press_enter;;
f) main_menu;press_enter;;
* ) tput setf 4;echo "Please enter a, b, c, d, e, or f";tput setf 7; press_enter
esac
done
else
echo "Error!! Book does not exist!" #not found
fi
}
ok i've made some changes to the codes for this function.. found out that i should use awk to update from old to new. and use grep to get line number..so it's best i stick with this..
function upd_title
{
read -p 'New title: ' title
#awk '/liine/{ print NR; exit }' BookDB.txt
grep -n 'regex' | sed 's/^\([0-9]\+\):.*$/\1/'
awk 'NR==n{$1=a}1' FS=":" OFS =":" n=$linenumber a = $title BookDB.txt
echo "New title successfully updated!!"
}
but after i tried that code.. this is what i got:
Advanced Book Inventory System
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
Enter your option: 3
Title: The Notebook
Name: Nicholas Sparks
Book found!
Book Update System
a) Update title
b) Update Author
c) Update Price
d) Update Qty Available
e) Update Qty Sold
f) Back to main menu
Enter your option: a
New title: Notebook
still doesn't update the title.. >.< anyone can help me point out what's the problem? am i missing something here
help me how to do this.. thanks! :D :D
$updatetitle and $updateauthor defined by the read statements in update_book() are not visible in the upd_title() and other functions you define.
Try exporting them after they are read in. If this doesn't work, pass them in as positional parameters to update_title():
upd_title $update_title $update_author
...
function upd_title
{
update_title=$1
update_author=$2
...
}
My bash script asks user for their first name, last name, address and phone number and writes this information that is input by the user to a file of the format "firstname.lastname"; however I want to repeat this a number of times (I actually wanted to do something like a do..while loop where it runs atleast one time and asks user if they want to continue creating accounts or not but I see there is no do...while for bash it seems). So when I execute this bash script in the terminal it will ask for how many accounts to be made and I provide all the input but the it only runs one time. What am I doing wrong? Here is my script.
#!bin/bash
echo "How many accounts are you creating?"
read num
echo "Enter first name"
read fName
echo "Enter last name"
read lName
echo "Enter address"
read add
echo "Enter phone number"
read phn
echo "Enter gender m for male and f for female"
read gender
if [ "$gender" == "m" ]
then
sex="male"
elif [ "$gender" == "f" ]
then
sex="female"
else
echo"Invalid gender. Restart the script and enter a valid gender"
exit 0
fi
for (( i = 0; i<=num; i++))
do
cat > $fName.$lName <<-EOF
Full Name: $fName $lName
Address: $add
Phone number: $phn
Gender: $gender
EOF
done
zerobandwidth's answer is correct, but as an alternative answer, it is in fact quite easy to do what you initially wanted to do:
while true; do
# Your account creation code goes here
echo "Create another account (y/n)?"
read another
if [ "$another" != y ]; then
break
fi
done
Put your input code inside the loop, or wrap that code in a function and call the function inside the loop.
I have a problem in this line:
--description=\""$(<$varLeer/media/festplatte/txt/$varDesc)\""
I want it to get out the following:
--description="$(<$varLeer/media/festplatte/txt/$varDesc)"
But how do I get it that it does not "make use" of the letters "$(< and )" ?
I hope you understand what I mean. I read this tutorial (http://wiki.bash-hackers.org/syntax/quoting) but I don't understand how I can write down the "$(< and the )" without the shell using it "as Code".
To get maybe a bit clearer, I want to get the second code to be shwon but with the "$(< )" but marked as "real code".
What I tried is this - but it did not work:
--description=\"$(<"$varLeer/media/festplatte/txt/$varDesc\")"
//EDIT: Lets be much clearer at what I want to do.
There is a phyton script called "youtube-uploader" (https://code.google.com/p/youtube-upload/wiki/Readme). Now that I don't have to write "youtube-upload ....... INSERT VERY MUCH HERE ;)" every video I want that the uploaded parts get their title, description etc automatically. My idea was to just open my ytscript.bash with nano, edit the first top variables and then just do "sh ytscript.bash", insert my password and relax.
The part would be +1 every time the loop is used and with this there is automatically an own fitting title for every part. Here is my code so far:
#!/bin/bash
# Variablen
varSpielart="Lets Play: "
varSpielname="Diablo 3 "
varParttext="- PART "
varStartpart=1
varEndpart=2
varExtras=" [PS4][1080p]"
varKategorie=Games
varDesc="lpd3desc.txt"
varDateiname=lpd3
varDateiendung=.m4v
varKeywords="ps4, spieleule, piupload"
varPlaylist="UNDEFINIERT"
varAnzahlparts=`expr $varEndpart - $varStartpart`
varLeer=" "
echo "Gebe dein Passwort ein!"
read varPasswort
echo YT-UPLOAD: BEGINNE NEUEN UPLOAD
echo YT-UPLOAD: SPIEL $varSpielname
echo YT-UPLOAD: ANZAHL PARTS: $varAnzahlparts
varDurchgang=$varStartpart
while [ $varEndpart -gt $varDurchgang ]
do
cd youtube-upload
youtube-upload --email="email#email.de" --password=$varPasswort --private --category=$varKategorie --title=\""$varSpielart$varSpielname$varParttext$varDurchgang$varExtras\"" \
--description=$(<$varLeer/media/festplatte/txt/$varDesc) \
/media/festplatte/upload/$varDateiname"p"$varDurchgang""$varDateiendung &
varDurchgang=`expr $varDurchgang + 1`
echo Durchgang: $varDurchgang
echo Entpart: $varEndpart
echo Startpart: $varStartpart
echo Anzahlparts: $varAnzahlparts
done
The automatically title works like a charm (except giving me a " at the start and the end of the title on youtube.com without me wanting it! :( ). The description on youtube.com only gets a "" in it with the first answer under this question.
Try single quotes:
--description='"$(<$varLeer/media/festplatte/txt/$varDesc)"'
Try this
temp='$(<'$varLeer'/media/festplatte/txt/'$varDesc')'
--description=$temp