Connecting and changing variables in Bash script

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Connecting and changing variables in Bash script
# 1  
Old 11-17-2016
Connecting and changing variables in Bash script

Code:
#!/bin/bash

X=$(</home/cogiz/computerhand.txt)                  # (3S 8C 2H 6D QC 8S 4H 5H)
Y=$(</home/cogiz/topcardinplay.txt)                   # KS
A=( "${Y[@]::1}" )
B=( "${Y[@]:1}" )

for e in ${X[*]}; do
  if [[ $e =~ $A|$B|8 ]]; then                                 # searching for valid cards K,S or 8
        echo "$e " >> /home/cogiz/validcards.txt 
  else
        echo "o" >> /home/cogiz/validcards.txt     # adds a 0 to line if no valid card is found to save position
  fi           
done

grep -n -v -b "o" /home/cogiz/validcards.txt | cut -c1 | xargs > /home/cogiz/position.txt    # removes all 0's, leaving position no.

rm /home/cogiz/validcards.txt

for e in ${X[*]}; do
   if [[ $e =~ $A|$B|8 ]]; then                                      # recalculates valid cards to play
     printf "$e " >> /home/cogiz/validcards.txt
   fi
done

VC=()
validcards=$(</home/cogiz/validcards.txt)                         # valid cards in hand available to play
for i in validcards; do                                                          #   (3S 8C 8S)
   VC+=(${validcards[*]})                                       
done

P=()
position=$(</home/cogiz/position.txt)                            # positions available cards are in computer hand
for i in position; do                                                         #   (1 2 6)
   P+=(${position[*]})
done

echo ${#VC[@]} > /home/cogiz/validcardchoices.txt  
N=()
N2=$(</home/cogiz/validcardchoices.txt)
for i in N2; do                                                                           # total of available choices (3)
   N+=(${N2[*]})
done

echo $RANDOM % $N + 1 | bc > /home/cogiz/randomvalidcardchoice.txt             # random choice between 1 and 3
                                                                                                                                 
O=$(</home/cogiz/randomvalidcardchoice.txt)

echo "The computer has chosen to play the card in position $O"     # in this case (3)

#######################################################################

How would I go about connecting the random choice of which card to play (3rd card of 3 valid cards which sits in position 6 of computer's hand) to the proper card in the array
${X[5]}, and therefore making this card (8S) the new topcardinplay ($Y)?

Thank you in advance for any help you can give me.
Cogiz

Last edited by Scrutinizer; 11-17-2016 at 12:41 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing date using bash script

I am trying to change dates in a bash script. I have a start time and an endtime and want to increment the times. Basically the month and day have to be incremented in a loop to create two strings, stm and etm defining the start and end times. stm="2014-05-13T00:00:00"... (4 Replies)
Discussion started by: novilatte
4 Replies

2. Shell Programming and Scripting

Problem with variables and bash script

From the command line: dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX -rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Discussion started by: dionbl
2 Replies

3. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

4. Shell Programming and Scripting

Changing script from csh to bash

Hello Guys I have a script working fine on csh, but I would like to change it to bash, how I should change this command to be able to work as bash script. :wall: if ( $fsw > "0" ) then foreach swath ( `awk 'BEGIN {for (i='$fsw';i<='$lsw';i++) printf ("%s\n", i) }'` ) ## work to be done... (2 Replies)
Discussion started by: jiam912
2 Replies

5. UNIX for Advanced & Expert Users

Bash script with export variables

Hi all guys, how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it. But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script. Consequently in... (8 Replies)
Discussion started by: idro
8 Replies

6. Shell Programming and Scripting

Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#) #!/bin/bash #Sample Script VAR1= echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." read VAR1 case $VAR1 in 1) echo "Pick... (4 Replies)
Discussion started by: eclerget
4 Replies

7. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

8. Shell Programming and Scripting

changing cron using bash script

How can I change the cron entries only for ABC and XYZ from dosomething_1.0.sh to nowchanged_2.0 using a bash script ? Any help will be appreciated. # # ABC 00,05,10,15,20,25,30,35,40,45,50,55 * * * * /mydir/dosomething_1.0.sh 1>/dev/null 2>&1 # # ## # DEF... (4 Replies)
Discussion started by: jville
4 Replies

9. Shell Programming and Scripting

Changing File Time Stamp (Bash Script)

I need some help recovering from a "slight" screwup. We just moved 3 TB of data from one RAID Array to another. Low lever archive files. This was done with a regular cp (for some reason) and now we have lost all the timestamps on the files, and we urgently need to get the timestamps back on these... (7 Replies)
Discussion started by: chj
7 Replies

10. Shell Programming and Scripting

Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search... (5 Replies)
Discussion started by: Breakology
5 Replies
Login or Register to Ask a Question