LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2013, 08:51 AM   #1
thecazz
LQ Newbie
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian
Posts: 8

Rep: Reputation: Disabled
bash: create random script with select case


Hi trying to create a script that random the weapon order on my CS GO server

I have this code

Code:
awk -v loop=12 -v range=23 'BEGIN{
  srand()
  do {
    numb = 1 + int(rand() * range)
    if (!(numb in prev)) {
       print numb
       prev[numb] = 1
       count++
    }
  } while (count<loop)
}'
And it work, it random 12 numbers between 1-23 and no duplicate.

Now I trying to add a select case because it is not the number I'm after it is some string that I like to write to a file.

For exemple

Code:
echo " \"nova\"      {\"kills\"   \"1\" }" >> text.txt
so it write to a file with that code.

But I get error when I did try add a select case.
 
Old 08-04-2013, 09:36 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Instead of printing the generated number grep the file holding the mappings for that number and then print the required items?
 
Old 08-04-2013, 10:06 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Excuse me but are you trying to generate a code to run in Bash later? Or do you want to run the generate code itself immediately? If that's the case you could just use Bash instead with the RANDOM variable instead of passing it to awk. Was the code originally awk?
 
Old 08-04-2013, 10:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If staying with awk and the weapons are all on individual lines, once the numbers are calculated you could simply return the rows of the generated numbers.
 
Old 08-04-2013, 11:37 AM   #5
thecazz
LQ Newbie
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: Disabled
I'm not sure what you all mean.

yes, I like to generate the code immediately.

I know the "numb" have the generated number that I want to have.

Can't I just add a select case inside the loop.
 
Old 08-04-2013, 11:46 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Your code is for awk. But you want to generate those lines which runs in a shell (echo is for shells, while print is for awk), and your thread title says Bash. So you want to generate the bash or shell script code from Awk then run it in a shell or bash?
 
Old 08-04-2013, 11:58 AM   #7
thecazz
LQ Newbie
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ahh so you say the code I have is from awk and try to run it from bash.

To tell the truth I have problem alot but very very new to bash and Linux.
so I was just looking around a Little, try to read some example and try to make my own script.

I have read the beginner of bash so I know a Little.


That I'm after is to create a script to run with crontab to generate a new weapon order for my CS GO server.

so if I can't mix awk with bash, how do I make so it run 12 randomnumber that is not duplicate in bash?

I was thinking about add all the weapon in a select case

Code:
"mp9"			{ "kills"	"1" }
"mac10"			{ "kills"	"1" }
"mp7"			{ "kills"	"1" }
"bizon"			{ "kills"	"1" }
"ump45"			{ "kills"	"1" }
"p90"			{ "kills"	"1" }
"nova"			{ "kills"	"1" }
"mag7"			{ "kills"	"1" }
"xm1014"		{ "kills"	"1" }
"sawedoff"		{ "kills"	"1" }
"galilar"		{ "kills"	"1" }
"famas"			{ "kills"	"1" }
"ak47"			{ "kills"	"1" }
"m4a1"			{ "kills"	"1" }
"sg556"			{ "kills"	"1" }
"aug"			{ "kills"	"1" }
"awp"			{ "kills"	"1" }
"m249"			{ "kills"	"1" }
"negev"			{ "kills"	"1" }
"glock"			{ "kills"	"1" }
"hkp2000"		{ "kills"	"1" }
"tec9"			{ "kills"	"1" }
"p250"			{ "kills"	"1" }
"deagle"		{ "kills"	"1" }
"fiveseven"		{ "kills"	"1" }
"elite"			{ "kills"	"1" }
"knifegg"		{ "kills"	"1" }
take 12 of this to a config file
 
Old 08-04-2013, 12:12 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by thecazz View Post
That I'm after is to create a script to run with crontab to generate a new weapon order for my CS GO server.
Use shuf, perhaps:
Code:
shuf -n 12 <original-weapon-list.txt >12-weapons-in-random-order.txt
 
3 members found this post helpful.
Old 08-04-2013, 12:29 PM   #9
thecazz
LQ Newbie
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: Disabled
hmm have to read about that, I have no idea how it works

/Update

WOW, and I mean WOW what a nice command

I need to say WOW again.

Thanks alot.

Last edited by thecazz; 08-04-2013 at 01:02 PM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is it possible to create a bash script to create multiple new bash scripts Batistuta_g_2000 Linux - Newbie 6 02-19-2013 11:42 AM
Help with bash select and case neocontrol Programming 1 01-03-2013 09:42 PM
need a shell script to select a random image frieza Linux - General 1 05-14-2008 04:04 AM
BASH script with case ( mandriva ) hellreaper Programming 3 02-19-2008 07:36 AM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 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