[SOLVED!]Bash scripting questions

Need help with C, C++, perl, python, etc?

[SOLVED!]Bash scripting questions

Postby tony763 » 2017-06-27 22:56

[h1=0
store_newgolf="home/john1/learning/store_newgolf"
while (( "$h1"<4))
do
read -p "Enter hole number : " h1
read -p "Enter par : " p1
read -p "Enter strokes : " s1
read -p "Enter fairways hit : " f1
read -p "Enter greens in regulation : " g1
read -p "Enter putting strokes : " ps1
echo "hole number-$h1"
echo "par-$p1"
echo " strokes- $s1"
echo "fairways hit - $f1"
echo "greens in regulation - $ g1"
echo "putting strokes-$ps1"
printf "$h1":"$p1:"$s1":"f1":"$g1":"ps1" >> "store_newgolf"
done]

Hello, I am new at bash scripting and cannot get this script to print on separate lines in the file "store_newgolf" Any help would be greatly appreciated. Also when trying to write a loop I came across this code [- le ] I could not find any explanation for this. What does this mean? Thank you for your assistance.
Last edited by tony763 on 2017-06-28 17:20, edited 2 times in total.
tony763
 
Posts: 8
Joined: 2015-05-16 20:44

Re: Bash scripting questions

Postby GarryRicketson » 2017-06-28 02:15

That does not look like any kind of bash script I have seen.
Any bash script starts with:
Code: Select all
#!/bin/bash

I am not any kind of expert on bash, but even if I was, I would not
start writing a complete tutorial in a forum post, especially when
there are so many readily available on line.
I think your best approach would be to start with some bash tutorial,
there are many:
How to start writing a bash script
One of many:
https://www.linux.com/learn/writing-simple-bash-script

This statement is puzzling :
Also when trying to write a loop I came across this code [- le ]---snip--

If you are trying to write a loop, ok, so you start writing your code, How is it
you come across other code that you do not know what it does ?
I could not find any explanation for this. What does this mean?

It appears to me, you are not actually writing anything, what you are doing or
trying to do is copy/paste other code that some one else wrote, with out having
a clue as to what the code actually does.
That is not a good practice at all, and not wise. You could easily copy/paste something into a script , that does things you really don't want to have happen.
On the same :
Also when trying to write a loop I came across this code
Code: Select all
 [- le ]
I could not find any explanation for this.


You did not really look anywhere did you ?

It means " less than or equal to "
and
A simple search would have give a explanation :
what does the [- le ] in a loop mean
This is from the first hit at:
https://stackoverflow.com/questions/8374233/what-does-the-lt-mean-in-the-boolean-expression-for-this-bash-while-loop
Code: Select all
man test

n1 -lt n2
True if the integer n1 is algebraically less than the integer n2.

n1 -le n2
True if the integer n1 is algebraically less than or equal to the
integer n2.


Also :
Code: Select all
man bash

is help full.
How to write loops with bash
For details on writing various loops.
Last edited by GarryRicketson on 2017-06-28 05:52, edited 1 time in total.
"What we expect you have already Done"

Before doing anything, read the Debian documentation:
Debian Documentation
How to ask the smart way
Debian Foro Español
======================
For the Birds
User avatar
GarryRicketson
 
Posts: 4219
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Bash scripting questions

Postby Dai_trying » 2017-06-28 05:26

There are a couple of minor issues with your code, I have rectified them and tested it for you, save this as a separate file and run diff file1 file2 to see the differences.

Code: Select all
h1=0
store_newgolf="/home/john1/learning/store_newgolf"
while (( "$h1"<4))
do
   read -p "Enter hole number : " h1
   read -p "Enter par : " p1
   read -p "Enter strokes : " s1
   read -p "Enter fairways hit : " f1
   read -p "Enter greens in regulation : " g1
   read -p "Enter putting strokes : " ps1
   echo "hole number - $h1"
   echo "par - $p1"
   echo "strokes - $s1"
   echo "fairways hit - $f1"
   echo "greens in regulation - $g1"
   echo "putting strokes - $ps1"
   printf "$h1":"$p1":"$s1":"$f1":"$g1":"$ps1\n" >> "$store_newgolf"
done
Dai_trying
 
Posts: 316
Joined: 2016-01-07 12:25

Re: Bash scripting questions

Postby tony763 » 2017-06-28 17:17

Dear GarryRicketson and Dai_trying,

Thank you both for taking the time to help me with my problem! I really appreciate your suggestions, advice and comments. I found them very helpful and informative.
The script runs correctly now and I have learned a lot!
Thank you for the help!
tony763
 
Posts: 8
Joined: 2015-05-16 20:44

Re: [SOLVED!]Bash scripting questions

Postby GarryRicketson » 2017-06-28 17:34

Your welcome ,
User avatar
GarryRicketson
 
Posts: 4219
Joined: 2015-01-20 22:16
Location: Durango, Mexico


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable