Bash script does not work as expected

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Bash script does not work as expected
# 1  
Bash script does not work as expected

Repeat this text in a file named notes.txt and run the script
Before bash is a good language a blank line appears
Also, the following notes are displayed incorrectly
What is bad?

Title   : Note 1
Category: Computer
Date    : 2019-04-28 17:34:19
BASH is good language

 while : ; do
 read line1
 read tit
 read line2
 read cat
 read dat
 read line3
 while [ "$not" != "==================================" ]; do
  read not
 read nro1
 read nro2
# note.tmp
 echo "$line1" >note.tmp
 echo "$tit" >>note.tmp
 echo "$line2" >>note.tmp
 echo "$cat" >>note.tmp
 echo "$dat" >>note.tmp
 echo "$line3" >>note.tmp
 echo "$note" >>note.tmp
 echo "$nro1" >>note.tmp
 echo "$nro2" >>note.tmp
 cat note.tmp
 read -p "Press enter to continue" jj </dev/tty
done <notes.txt

Moderator's Comments:
Mod Comment
Please wrap all code, data/files, input & output/errors in CODE tags.
It makes them easier to read and preservces spacing for indented code or fixed-width data.

Last edited by rbatte1; 05-08-2019 at 09:41 AM..
# 2  
echo -ne "$note" >>note.tmp

--- Post updated at 07:32 ---

For what's here infinite "while" ?

--- Post updated at 07:48 ---

read -p "Press enter to continue" jj </dev/tty

there is option -u
read -p "Press enter to continue" -u 1 jj

while :: do
done < notes.txt

Totally pointless loop
Then it's better
while [ "$jj" != q ]; do
} < notes.txt
read -p "Press enter to continue" jj
echo "Start over"
sleep 2

Last edited by nezabudka; 05-08-2019 at 03:40 AM..
# 3  
infinite while is because the notes can have 1, 2 or more lines, until find the fourth line with equal signs.
Thank you
# 4  
The following appends even the first line as a new line, so the initial empty string becomes an empty line:
 while [ "$not" != "==================================" ]; do
  read not

You must either branch on a state variable (e.g. $firstrun), or use the "$separator variable" trick as follows:
 note="" sep=""
  read not &&
  [ "$not" != "==================================" ]

EDIT: I have changed the oder of read and [ ]. Otherwise one must initialize not="" (or it might have the value from a previous cycle).

Last edited by MadeInGermany; 05-09-2019 at 05:26 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #699
Difficulty: Medium
The noosphere represents the highest stage of biospheric development, its defining factor being the development of humankind's rational activities, according to biogeochemist Vladimir Vernadsky.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script unable to disable expected output

I'm trying to understand why a script behaves different when run through a pipe. My OS: Linux myip 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux This script ( #!/bin/bash echo whoami: whoami echo who: who echo who... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Encyrpt does not work in bash script

Hi All, I am looking here from any one. I am preparing script for create user and set password to remote servers. I have made password less. and able to add user and also able to set entry in /etc/shadow. But it does not make complete entry in shadow file. it lefts initial... (6 Replies)
Discussion started by: yash_message
6 Replies

3. Shell Programming and Scripting

Why my SETUID does not work as expected?

Hi All, Thanks for your help in advanced. Could you please kindly help on why my SETUID does not work? create a file, only root can read write it /tmp>ls -l a.log -rw------- 1 root root 3 Nov 12 18:57 a.log create a script under root with SETUID /tmp>ls -l -rwsr-sr-x 1 root... (3 Replies)
Discussion started by: summer_cherry
3 Replies

4. UNIX for Dummies Questions & Answers

sed command does not work as expected

Why when I use this command do I get "E123"? echo NCE123 | sed -n 's/\(.*\)\(\{1,\}\{1,5\}\)\(.*\)/\2/p' But when I used this command, I get NCE123? echo NCE123 | sed -n 's/\(.*\)\(\{3\}\{1,5\}\)\(.*\)/\2/p' I thought \{1,\} would mean any number of characters and \{1,5\ would mean 1-5... (1 Reply)
Discussion started by: newbie2010
1 Replies

5. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

6. Shell Programming and Scripting

Joining Two Files Does not Work as Expected

Hi, I would like some help with the above awk command. I am trying to use the join command to join two files, no luck. I need to put the second column from file2.txt into each matching field of file1.txt. It works OK up to the value of 1000 of the matching column (1at column in both... (13 Replies)
Discussion started by: yirgacheffe
13 Replies

7. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

8. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

9. Shell Programming and Scripting

Bash script on startup does not respond as expected

Hi, I have a bash script which I have referenced in the rc.local of my fedora linux OS. However it doesnt respond the same as when run in terminal from fedora. The bash script has a series of interactive questions that require user input as shown: #!/bin/bash echo "Do you want to use... (1 Reply)
Discussion started by: Crigamorfin
1 Replies

10. Shell Programming and Scripting

Script doesn't work as expected when run on cron

The script checks for free space stats on Oracle. If there are any tablespaces with more than 85% usage it prints the details of the tablespace. If all the tablespaces have more than 15% free space, then "All tablespaces have more than 15 pct free space" must be printed on the screen. When I run... (2 Replies)
Discussion started by: RoshniMehta
2 Replies

Featured Tech Videos