Unset array element and save to file in Bash

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Unset array element and save to file in Bash
# 1  
Old 12-03-2016
Unset array element and save to file in Bash

Code:
#!/bin/bash

X=(2H 4S 10D QC JD 9H 8S)

How do I unset the 10D from this array and save it to a file?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-03-2016 at 12:29 PM.. Reason: Added CODE tags.
# 2  
Old 12-03-2016
Code:
echo ${X[2]} > file

What do you mean by "unset"? Delete the element?
# 3  
Old 12-03-2016
example:
Code:
X=(2H 4S 10D QC JD 9H 8S)
>savefile 
end=${#X[*]}
echo "end = $end"
for i in `seq 0 $(( end -1 ))`  # arrays start with element == zero
do  
   echo "$i is = ${X[i]}" 
   if [ "${X[i]}" = "10D" ] ; then     
      echo  "${X[i]}" >> savefile  # to allow multiple "unsets"
      unset  X[i]      
      echo "$i is now unset and is = ${X[i]}"
      echo "    contents of savefile = `cat savefile`"
   fi 
done

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 12-03-2016
unset array elements in Bash

Thank you for your reply.

In this particular case X represents the computer's hand in a game of crazy 8's. The 10D is the card that the computer has chosen to play for its turn. So I need to remove it from the computer's hand and It will now be the Active card in play.
Once the element is unset, can it still be called ${X[2]}?

Cogiz
# 5  
Old 12-03-2016
Hi,

Quote:
Once the element is unset, can it still be called ${X[2]}?
No, you can't. Because element is removed from array.

See the example:
Code:
X=(2H 4S 10D QC)
$ echo ${X[2]}
10D
$ unset X[2]
$ echo ${X[*]}
2H 4S QC

Here X[2] is QC
This User Gave Thanks to greet_sed For This Post:
# 6  
Old 12-03-2016
Thank you. It makes more sense now.

Cogiz
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated. My example: sed -i '14 i\ # add these lines add these lines to... (5 Replies)
Discussion started by: Nvizn
5 Replies

3. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

4. Shell Programming and Scripting

File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records. for name in ${files}; do ---this loop is for all the file names in the array for i in $(wc -l < $name); do --this loop is for all the records in... (20 Replies)
Discussion started by: usrrenny
20 Replies

5. Homework & Coursework Questions

Save output into file bash scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 ... (1 Reply)
Discussion started by: shdin271
1 Replies

6. Shell Programming and Scripting

Save output into file bash scripting

Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 .............. 17 i wonder how to save the output into a single string and into a file. i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order,... (3 Replies)
Discussion started by: shdin271
3 Replies

7. Shell Programming and Scripting

perl-data from file save to multidimensional array

i have a file,like 1 3 4 5 6 7 8 9 i want to save it into an array. and then i want to get every element, because i want to use them to calculate. for example: i want to calculate 1 + 3. but i cannot reach my goal. open (FILE, "<", "number"); my @arr; while (<FILE>){ chomp;... (1 Reply)
Discussion started by: pp-zz
1 Replies

8. Shell Programming and Scripting

[bash] redirect (save) array to a file

Hi, I'm trying to write a function that redirects the contents of an array to a file. The array contains the lines of a data file with white space. The function seems to preserve all white space when redirected except that it seems to ignore newlines. As a consequence, the elements of the... (7 Replies)
Discussion started by: ASGR
7 Replies

9. Shell Programming and Scripting

Using unset to delete array elements

Hi, I am writing a BASH script. My questions regard deleting elements of arrays. I have an array: michael-browns-powerbook-g4-15:~ msb65$ test_array=(1 2 3 4) michael-browns-powerbook-g4-15:~ msb65$ echo ${test_array} 1 2 3 4 To delete the second element of test_array I type:... (3 Replies)
Discussion started by: msb65
3 Replies
Login or Register to Ask a Question