LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-25-2013, 11:59 PM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
Does 'shuf' seed itself with a zero?


Towards the bottom, the variable $gigartist is sometimes empty when it is printed to the file.
Using
Code:
function randomline() {
  retValue=$(shuf -i 1-9 -n 1 $1)
}
gives me this error"
Quote:
shuf: extra operand `artists'
Try `shuf --help' for more information.
shuf: extra operand `towns'
Try `shuf --help' for more information.
How do I make sure that when
Code:
shuf
is seeded it is not seeded with a zeor?

Code:
#!/bin/bash

function randomline() {
  retValue=$(shuf -n 1 $1)
}

k=10
i=0
loop_count=10
while [ "$i" -lt "$loop_count" ];  do
    gigday=$(($RANDOM%30));
##RANDOM returnds 1 - 9, Needs to be 01 - -09
		if [ "$gigday" -lt "$k" ]; then
## RANDOM sometimes returns a zero
				if [ "$gigday" -eq "0" ]; then
					gigday=1;
				fi
			
			gigday="0$gigday"	
		fi

    gigmonth=$(($RANDOM%12));
		if [ "$gigmonth" -lt "$k" ]; then
				if [ "$gigmonth" -eq "0" ]; then
					gigmonth=1;
				fi
			
			gigmonth="0$gigmonth"	
		fi

	gigyear=2013;
	gigartiswebsite="artistwebsite.html";
	gigvenuwebsite="venuewebsite.html";

	randomline "artists";
    gigartist="$retValue"
	randomline "towns";
    gigtowns="$retValue"
	
	echo "$gigmonth"/"$gigday"/"$gigyear","$gigtowns","$gigartist","$gigartiswebsite","$gigvenuwebsite" >> singer

	i=$(($i + 1));
  
done
Thanks to everyone for the help, feedback, and comments you have given on this project.
 
Old 10-26-2013, 04:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Dafydd View Post
Using
Code:
function randomline() {
  retValue=$(shuf -i 1-9 -n 1 $1)
}
How do I make sure that when shuf is seeded it is not seeded with a zero?
The above shuf command is not correct. If you use the -i LOW-HI option you cannot use an input file. You either choose to take input from a file or set the input with the -i option:
Code:
shuf -n 1 infile   # take 1 random line from infile
shuf -i 1-9 -n 1   # pick 1 number from the 1 to 9 range
The only way shuf can produce an empty line is if there is an empty line in the input file. I tried to reproduce this ad nauseam and I never get an empty/zero return.

$RANDOM generates a number in the 0 to 32767 range, but you can force it to start from 1 instead by doing the following:
Code:
$((RANDOM%30+1))   # This print a random number in the 1 to 30 range (both included)
I've played around with the above script and it does what it is supposed to do.

Here's an alternative which includes some of the info given and a few other changes you might find interesting:
Code:
#!/bin/bash

# get random line
function randomline() {
  retValue=$(shuf -n 1 $1)
}

# set counters
k=10
i=0
loop_count=10

while [ "$i" -lt "$loop_count" ]
do
  # gig day (using RANDOM)
  gigday=$(($RANDOM%30+1))
  [ "$gigday" -lt "$k" ] && gigday="0$gigday"

  # gig month (using shuf)
  gigmonth=$(shuf -i 1-12 -n 1)
  [ "$gigmonth" -lt "$k" ] && gigmonth="0$gigmonth"

  # gig 
  gigyear=2013
  gigartiswebsite="artistwebsite.html"
  gigvenuwebsite="venuewebsite.html"

  # gig artist
  randomline "artists"
  gigartist="$retValue"

  # gig town
  randomline "towns"
  gigtowns="$retValue"

  # store data
  echo "$gigmonth"/"$gigday"/"$gigyear","$gigtowns","$gigartist","$gigartiswebsite","$gigvenuwebsite" >> singer

  # increase counter
  (( i++ ))
done
EDIT: Too focused on other stuff to remember printf, which makes it possible to get rid of the leading 0 check. Here's another variation of your script:
Code:
#!/bin/bash

# get random line
function randomline() {
  retValue=$(shuf -n 1 $1)
}

# set counters
i=0
loop_count=10

while [ "$i" -lt "$loop_count" ]
do
  # gig day (using RANDOM)
  gigday=$(($RANDOM%30+1))

  # gig month (using shuf)
  gigmonth=$(shuf -i 1-12 -n 1)

  # gig 
  gigyear=2013
  gigartiswebsite="artistwebsite.html"
  gigvenuwebsite="venuewebsite.html"

  # gig artist
  randomline "artists"
  gigartist="$retValue"

  # gig town
  randomline "towns"
  gigtowns="$retValue"

  # store data
  printf '%02d/%02d/%d,%s,%s,%s,%s\n' "$gigmonth" "$gigday" "$gigyear" "$gigtowns" "$gigartist" "$gigartiswebsite" "$gigvenuwebsite" >> singer

  # increase counter
  (( i++ ))
done
Here's a link to printf: Bash printf syntax basics

Last edited by druuna; 10-26-2013 at 02:37 PM. Reason: Fixed typo.
 
1 members found this post helpful.
Old 10-26-2013, 04:35 PM   #3
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
As you suggested, it might be an empty line. And it was. I ran both your code suggestions and still came up with the empty artist. So I checked.

towns:
Code:
00000090   2C 20 54 58  0A 4F 64 65  73 73 61 2C  20 54 58 0A  43 6F 6E 72  6F 65 2C 20  , TX.Odessa, TX.Conroe,
000000A8   54 58 0A 54  68 65 20 57  6F 6F 64 6C  61 6E 64 73  2C 20 54 58  0A 48 75 6E  TX.The Woodlands, TX.Hun
000000C0   74 73 76 69  6C 6C 65 2C  20 54 58 0A                                         tsville, TX.
artists:
Code:
00000078   6E 61 20 50  6F 63 68 6F  70 0A 4D 61  72 63 69 61  20 42 61 6C  6C 0A 4D 61  na Pochop.Marcia Ball.Ma
00000090   72 69 61 6E  20 43 61 6C  6C 0A 53 61  72 61 20 48  69 63 6B 6D  61 6E 0A 52  rian Call.Sara Hickman.R
000000A8   75 62 79 20  4A 61 6E 65  0A 0A                                               uby Jane..
Thank you for your feed back and code suggestions. It will most likely be Monday before I return. My daughter just informed my I must put on my costume and go cos-play the role of an Irishman living in Holand and working as a wool merchant buying goods at an English village market during the reign of Henry VIII.

G'day My Lord.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do you seed for torrents? silvyus_06 General 2 02-20-2011 02:33 PM
What should I seed? conanm4 Linux - General 5 08-07-2006 11:18 AM
What else can I seed on bittorent? conanm4 Linux - Distributions 2 08-07-2006 05:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration