Read user input and update data(txt) file? - bash

I am doing a simple restaurant sit booking system as my learning of bash script. I have this data file, store in txt file (data.txt) in this format:
100X00
20X0X0
3000XX
4X00XX
The output, when user run the program like this:
A B C D E
1 X
2 X X
3 X X
4 X X X
Then, I prompt user where do you want to sit?
User will enter in comma-separated format, example: A1,B1
Once the seat is not occupied, I will update to the data.txt file. My current code:
echo "Please enter sit number?"
read -a usersSits
But then, I don't know how to proceed from this point. How to do the check and how to update? Anyone?

You can look at this:
Automatic update data file and display?
which seems to be a rather good start for what you want to do...

Related

How to get the full name of a folder if I only know the beginning

I am receiving an input from the user which looks like follows:
echo +++Your input:+++
read USER_INPUT
The way I should use it is to retrieve the full name of a folder which starts with that input, but that contains other stuffs right after. All I know is that the folder is unique.
For example:
User input
123456
Target folder
/somepath/someotherpath/123456-111-222
What I need
MYNEED=123456-111-222
I was thinking to retrieve this with an MYNEED=$(ls /somepath/someotherpath/$USER_INPUT*), but if I do this I will get instead all the content of /somepath/someotherpath/123456-111-222 because that's the only folder existing with that name so the ls command directly goes to the next step.
May I have your idea to retrieve the value 123456-111-222 into a variable that I will need to use after?
basename extracts the filename from the whole path so this will do it:
MYNEED=$(basename /somepath/someotherpath/123456*)

how to split a string into an array?

I'm trying to show to the user the list of zip file contained in a folder and give user the ability to choice which file to elaborate.
i'm trying this
roms=$(ls ~/roms/*.zip)
PS3="Choose a ROM "
select opt in "${roms[#]}" "quit"; do
....
done
The problem is that my menu is showed in this way:
1) /home/realtebo/roms/rom_01.zip
/home/realtebo/roms/rom_02.zip
/home/realtebo/roms/rom_02_v2.zip
....
2) quit
Instead I need this
1) /home/realtebo/roms/rom_01.zip
2) /home/realtebo/roms/rom_02.zip
3) /home/realtebo/roms/rom_02_v2.zip
...
n+1) quit
How to 'explode' the result of ls as an array?
I'm using bash under linux mint 17.3
You don't really need a variable (and using ls to populate the variable is certainly discouraged -- see http://mywiki.wooledge.org/ParsingLs) so the code can be significantly simplified to
PS3="Choose a ROM "
select opt in ~/roms/*.zip "quit"; do
:
If you want the file names in an array, just use the wildcard instead of ls (again, see the link above about why using ls breaks things):
roms=(~/roms/*.zip)
select opt in "${roms[#]}" "quit"; do
:

Read a value for a key constructed in runtime

I want to be able to read user input e.g. "Please enter your sex:", if the user enters male I want to read the property male_interests from a property file. On the other hand if the user enters female, i want to read female_interests from the property file. Is this possible? If yes, how?
I have already tried doing this:
#!/bin/bash
echo Please enter sex
read sex
property_name="$sex"_interests
source messages.properties
echo ${$property_name} ## I tried more crap but nothing worked
Please suggest. Thanks in advance.
BashFAQ/006. foo=1; bar=foo; echo ${!bar}.
– 4ae1e1
or echo ${!property_name}
– VK Kashyap

Bash-shell script revise and suggestion

I am new to shell scripting and I am trying to automatise a process.
I think the structure is ok, but i get notice which i think that it should not be there
# STEP 1#
echo " Untar the file s"
time tar -xf $tarfiles
time tar -xf *.tar
#STEP 2#
fname=(wnd10m.gdas.2010*.grb2)
echo " convert into .nc "
cdo -f nc copy $fname $fname.nc
#STEP 3#
ofile1=$fname.nc
echo "re-format into structured grid 0.5x0.5"
cdo remapbil,r720x361 $ofile1 remap$R.nc
#STEP 4#
ofile2=$remapR.nc
echo "produce desired mesh"
cdo sellonlatbox,lon1,lon2,lat1,lat2 $ofile2 $grid.nc
#STEP 5#
ofile3=$grid.nc
echo "merge the files based on tstep"
cdo mergetime $ofile3 final$R.nc
STEP 1 and STEP 2 are performed although it only manipulates and alters one of the file that it was included in the .tar file.
The process so far gives out the STEP 1 & 2, but during STEP 3 I do not get the new file as remap$R.nc with R being an additional ending so generated is not confused with other produced in the previous step.
After I run it this is what i get
convert into .nc
cdo copy: Processed 987365376 values from 2 variables over 744 timesteps ( 234.66s )
re-format into structured grid 0.5x0.5
cdo remapbil: Processed 987365376 values from 2 variables over 744 timesteps ( 53.59s )
produce desired mesh
Error (cdo sellonlatbox) : Output file name .nc is equal to input file name on position 1!
merge the files based on tstep
cdo mergetime: Open failed on >.nc<
No such file or directory
I am trying to de-bug my file but don't know what I have done wrong, because the process is supposed to handle a lot of files converting them into every stage I paid significant attention to assigning correct and different fnames.
I would be grateful if you could have a look and give me your inputs.
p.s. cdo is a code I am using so you can disregard it, my focus is on the process itself
thanks
$remapR is different to remap$R. Move your dollar sign.
You can use set -xv to debug your script.
compare these:
cdo remapbil,r720x361 $ofile1 remap$R.nc
and
ofile2=$remapR.nc
this looks like a classical typo
Hello all as a continuation to the issue i had and the user #Bushmills help me a lot (thanks!!!)
I attach the code that I did with advice i got, i tested and it works, so hopefully another user (novice like me) may find this useful.
file="wnd10m.gdas."
year='2010'
month='01 02'
ext="grb2"
for((y=$year;y<=$year;y++));
do
for m in $month
do
fname=$file$y$m.$ext
#STEP 1#
cdo -f nc copy $fname $fname.nc
#STEP 2#
ofile1=$fname.nc
cdo remapbil,r720x361 $ofile1 R$ofile1 #produces a file with the name of the R+ofile1
example if the previous file was named FILE the command will produce a new file named RFILE
#STEP 3#
ofile2=($R*.nc)
use all the file starting with R
cdo sellonlatbox,$lon1,$lon2,$lat1,$lat2 $ofile2 grid_$ofile2
done
done
thank you for the help

bash script, prompt user to select ID or name completion

I am working on a script that reads the directories in a path and prompts the user for witch directory to choose. The script so far looks like this:
select choises in ${list_files[#]}; do
if CONTAINS_ELEMENT $choises "${list_files[#]}"; then
break
else
echo "invalid choise (PS use the ID numbers)! Try again:"
fi
done
the "CONTAINS_ELEMENT" thingy is a function that checks if the variable $choises is a member of the list_files array. This script works fine, and the output is something like this:
1) first_folder
2) second_folder
3) yet_another_folder
And the user can choose witch folder by typing in the corresponding ID value (ie "2" for "second_folder").
What I am struggling with is this: Is there a way to check if the supplied answer is a correct number -or- a correct file name? And if you can write in the file name manually, is there a way to implement name completion in this script?
The actual reply to select is held in the variable $REPLY, I need to somehow check both $choises and $REPLY. The completion mechanism is much more tricky I suspect..
Does anyone have a good suggestion here? It would be very cool if you could help me get in these features!!
select choises in ${list_files[#]}; do
if CONTAINS_ELEMENT $choises "${list_files[#]}" || CONTAINS_ELEMENT $REPLY "${list_files[#]}"; then
break
else
echo "invalid choise (PS use the ID numbers)! Try again:"
fi
done
This makes both the ID number and the full directory name work, the code is a bit ugly though, and there is no name completion.

Resources