bash script, prompt user to select ID or name completion - bash

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.

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 do I make this if then else script

I've been trying to make this if then else script but its wrong every time.
I am brand new at this, maybe I have taken more than I can chew but I am trying to make a script that runs at launch that does this
If file exists /Users/Bob/Library/Safari/Bookmarks.plist
then do nothing
if file does not exist ( i assume this is "else") then move file from /Library/Application\ Support/Bookmarks.plist to /Users/Bob/Library/Safari/Bookmarks.plist
Any feedback is greatly appreciated, thanks in advance.
What you described could be written like that in pseudo code:
if file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
exit
if not file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
As you can see, the first "branch" is not really used, as you anyway plan to ignore this case.
But if we assume, that in the first case, you actually want to do something, like print a nice message, you could write it like that:
if file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
print("Nothing to do!")
else
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
As you can see, in the this example, the "else" part is equivalent to saying "if not file_exists(...)". You could also reverse the order by using "not":
if not file_exists("/Users/Bob/Library/Safari/Bookmarks.plist")
move("/Library/Application\ Support/Bookmarks.plist", "/Users/Bob/Library/Safari/Bookmarks.plist")
else
print("Nothing to do!")
DST="/Users/Bob/Library/Safari/Bookmarks.plist"
SRC="/Library/Application\ Support/Bookmarks.plist"
if [ ! -f "$DST" ]; then
mv "$SRC" "$DST"
fi
This will see if the destination exists. If not, it will move the file. Realistically there should be additional error checking, and I'm not certain a move is the best approach, since there would no longer be an "original" file.

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

Why won't cat work inside my bash-script?

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 :)

Shell script musn't generate "extra" error messages, but is for one odd test case

I must write a basic script that takes input in the form of:
./chext 'param1' 'param2.xx'
and outputs:
param2.param1
specifically, I have to check to see if the file exists, and if it does, rename it with the new extension, and if it does not, generate the message:
param2.xx: No such file
specifically, I'm told that: "No other messages should be issued, including error messages from commands invoked by your script."
This works fine with examples like:
./chext 'com' 'aardvark.dat'
and
./chext 'ret' 'too common.exp'
but for:
./chext 'dat' 'ocelot.dat'
I'm getting the error (created by my professor) "generated unexpected messages"
Can anyone tell me what's so different about this example that my script isn't taking care of?
#!/bin/csh
set ext="$1"
set oldName="$2"
if (-r "$oldName") then
set newName=`echo "$oldName" | sed 's/\.[A-Za-z0-9]*$/'".$ext"'/g'`
mv "$oldName" "$newName"
else
echo "$oldName": No such file
endif
Thanks
You can't rename a file to the name it already has.

Resources