Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED!]Bash scripting questions

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
tony763
Posts: 8
Joined: 2015-05-16 20:44

[SOLVED!]Bash scripting questions

#1 Post by tony763 »

[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.

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

Re: Bash scripting questions

#2 Post by GarryRicketson »

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/837 ... 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.

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Bash scripting questions

#3 Post by Dai_trying »

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

tony763
Posts: 8
Joined: 2015-05-16 20:44

Re: Bash scripting questions

#4 Post by tony763 »

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!

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

Re: [SOLVED!]Bash scripting questions

#5 Post by GarryRicketson »

Your welcome ,

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: [SOLVED!]Bash scripting questions

#6 Post by Dai_trying »

This beginners guide has been very useful to me in the past and I still use it for reference.

Post Reply