user3521205
user3521205

Reputation: 41

Add input to an existing file with Bash

I am working on a library record app (for school).

I need to be able to collect user input and write to an existent file (add new record). However, when I try to do so, I get the following error:

./minilib.sh: line 12: : No such file or directory

Here is my function for adding new records

records = "/lib_records.txt"

add_book(){
    echo
    echo "Enter Book Name:"
    read name
    echo "Enter Book Author:"
    read author_name
    echo "$name $author_name" >> "$records"  #this is my line 12
}

Any idea what may be causing the error? Any help is greatly appreciated. Here are the file permissions:

-rwxrwxrwx. 1 GSUAD\ GSUAD\domain^users    0 Oct 30 18:04 lib_records.txt
-rwxrwxrwx. 1 GSUAD\ GSUAD\domain^users 1253 Oct 30 18:40 minilib.sh

Upvotes: 0

Views: 301

Answers (2)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

You shouldn't store file in / directory (/lib_records.txt), because you will probably get a Permission denied error. Secondly, remove spaces in the first line.

Upvotes: 1

stones333
stones333

Reputation: 8958

Here are 2 issues for your shell script:

  1. records="./lib_records.txt": should not have space before and after =
  2. "./lib_records.txt" instead of "/lib_records.txt"

Here is modified script for you.

records="./lib_records.txt"

add_book(){
    echo
    echo "Enter Book Name:"
    read name
    echo "Enter Book Author:"
    read author_name
    echo "$name $author_name" >> "$records"  #this is my line 12
}

add_book

Upvotes: 2

Related Questions