Bash – am I getting MAD here?
Johnny Rosenberg
gurus.knugum at gmail.com
Wed Jul 27 15:59:18 UTC 2016
Hi!
I really really don't understand what's going on here. I have a rather big
script and there is something that doesn't work properly, and after hours
of debugging, I found the problem line.
Since the script is very big, 19 files at the moment, I made a shorter
example that has exactly the same problem. Here it is:
*#!/bin/bash*
*FullPathName() {*
* # Declare local variables*
* local Path="$1"*
* if [[ ${Path:0:1} != / ]]; then # Relative path.*
* echo "${PWD}/${Path}"*
* else # Absolute path.*
* echo "${Path}"*
* fi*
*}*
*ToURI () {*
* # Declare local variables.*
* export local FileName="$1"*
* perl -MURI::file -e 'print URI::file->new($ENV{'FileName'})."\n"'*
*}*
*File[1]=$(FullPathName "${1}")*
*FileName[1]=$(basename "${File[1]}")*
*SelectedProject="Projekt Johnny Guitar"*
*RemoteRootFolder="http://web.comhem.se/something
<http://web.comhem.se/something>"*
*RemoteProjectFolder="${RemoteRootFolder}/"$(ToURI "${SelectedProject}")*
*echo "${RemoteProjectFolder}"*
as you can see, the *File* and *FileName* variables are not used for
anything in this example, but they still illustrate the problem. I named
the file *TEST.sh*, put it in my *~/bin* folder which is included in my
*PATH* environment variable, so I will run this test with a random file as
an argument.
Now, let's run this:
*~$ TEST.sh .netrc*
*http://web.comhem.se/something/ <http://web.comhem.se/something/>*
*~$ *
Obviously, *$(ToURI "${SelectedProject}" *didn't do anything, but why?
So, as I said, after hours of debugging, I came up with this:
*#!/bin/bash*
*FullPathName() {*
* # Declare local variables*
* local Path="$1"*
* if [[ ${Path:0:1} != / ]]; then # Relative path.*
* echo "${PWD}/${Path}"*
* else # Absolute path.*
* echo "${Path}"*
* fi*
*}*
*ToURI () {*
* # Declare local variables.*
* export local FileName="$1"*
* perl -MURI::file -e 'print URI::file->new($ENV{'FileName'})."\n"'*
*}*
*File[1]=$(FullPathName "${1}")*
*# FileName[1]=$(basename "${File[1]}") ############## Disabled line
###############*
*SelectedProject="Projekt Johnny Guitar"*
*RemoteRootFolder="http://web.comhem.se/something
<http://web.comhem.se/something>"*
*RemoteProjectFolder="${RemoteRootFolder}/"$(ToURI "${SelectedProject}")*
*echo "${RemoteProjectFolder}"*
So, the only difference is that I disabled the line *FileName[1]=$(basename
"${File[1]}")*.
Now, let's try again:
*~$ TEST.sh .netrc*
*http://web.comhem.se/something/Projekt%20Johnny%20Guitar
<http://web.comhem.se/something/Projekt%20Johnny%20Guitar>*
*~$ *
So, suddenly it works, but WHY?
I early suspected *basename*, but in the original script there are several
*basename* statements, but they don't mess anything up.
Seriously, what is going on here?
*~$ bash --version | grep release*
*GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)*
*~$*
*Any help is greatly appreciated, since this is a real showstopper for me
at the moment.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20160727/50ba7e9a/attachment.html>
More information about the ubuntu-users
mailing list