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
Related
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*)
I want to be able to display the content of my command-list document but whenever I do it just prints out "./commands.txt" but if I try the same thing outside of my script it works just fine.
This is my code:
helpFile="./commands.txt"
if [ "$com" = "help" ]
then
cat $helpFile
fi
I don't see where you get the $com variable from, but if you set it based on the first argument this should work:
#!/bin/bash
helpFile="./commands.txt"
com=$1
if [ "$com" = "help" ]
then
cat $helpFile
fi
In the above example $com will be set to the first argument passed to the script, so if you would like to display the contents of ./commands.txt you would call it like ./<script.sh> help
I'm also thinking that you should check so the file really does exists in the current working directory or perhaps try to use an absolute path i.e:
helpFile="/home/commands.txt"
I found out what was wrong. My text editor screwed up and was saving all the new edited content on the desktop instead of the folder with the script and text file. Anyways, thanks for all your help guys, I really appreciate it :)
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.
I've written a script to copy a new user account file to the new user location. It works by reading a list of usernames and copying the file to that location. I can't understand why I needed the done < $USER at the end. Can someone please explain this?
Thanks
USER=/home/example/new.txt
NEWUSER=$USER
LOC=/var/account/
cd /home/example
while read NEWUSER
do
cp _newuser.txt $LOC/$NEWUSER
done < $USER
To iterate over each line in the file /home/example/new.txt, which is the value of variable USER
Pls look at http://en.kioskea.net/faq/1757-how-to-read-a-file-line-by-line
< is input redirection operator (http://www.tldp.org/LDP/abs/html/io-redirection.html)
You can also delete NEWUSER=$USER, since I do not see any use of NEWUSER except the while loop. Due to the while, NEWUSER will be assigned a new value each iteration.
Because read reads the input from standard input (stdin). In order to read from file you need to redirect it to the read command.
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...