Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need to test for all conditions, I think, but if valid and connected is the only "TRUE" answer. This is a dropbox directory, so can exist, be valid, but dropbox not active, so not connected.
No connected as dropbox creates sync'd copies of the files/dirs residing onliune, on one computer, like symlinks, which are always mounted, exist, even writable, but actually do not write into the online source unless dropbox is connected.
Therefore if you want to run a backup to dropbox, so you have the online copy, this will not work without dropbox connected.
Not sure if BASH can actually do that. Perhaps the test needed is to see if dropbox is actually running as a process, so something like:
Code:
ps -A | grep -i dropbox
However right now the problem is more basic. No vars are passing into the function. I added an echo, right after line #12 to show if $DIR ever gets set and it does not, so either the function is not being called correctly or no vars are being passed.
I think you missed a very important point:
the return you specified is not a return value of the function, so in this line: vdr=valid_dir "/home/files/Dropbox"; vdr will not be set to 0, 1, 2 or 3. Actually this line is syntactically incorrect (if it was a bash script). In line 5 you ought to use " instead of '.
I think you missed a very important point:
the return you specified is not a return value of the function, so in this line: vdr=valid_dir "/home/files/Dropbox"; vdr will not be set to 0, 1, 2 or 3. Actually this line is syntactically incorrect (if it was a bash script). In line 5 you ought to use " instead of '.
No expert here, but BASH book I'm working from says "return $value" will return either numeric or string value assigned to "value".
Would be nice to tell us which book is that, I'm afraid that statement is invalid or something is missing.
shellcheck is used to check the script, not for debugging.
It reported an error in line 3: Couldn't parse, that means it is syntactically incorrect. Actually you need to use -eq for comparison, without - it does not work.
When you add that - you will get another error: Add a shebang.
That means please start your script with #!/bin/bash. Next you will get: vdr is referenced but not assigned.
You need to fix all the errors/warnings reported by shellcheck (we can help you to explain the messages).
pan64,
No expert here, but BASH book I'm working from says "return $value" will return either numeric or string value assigned to "value".
TBNK
It's been awhile, but (assuming that the syntax *were* correct...not ignoring those points) doesn't setting the return value to any non-zero value cause the result of the function to simply be "false"? I find this:
Quote:
Bash functions, unlike functions in most programming languages do not allow you to return a value to the caller. When a bash function ends its return value is its status: zero for success, non-zero for failure.
and this
Quote:
return [n]
Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
Line 3:
if [ 0 eq $vdr ]; then
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1072: Expected test to end here (don't wrap commands in []/[[]]). Fix any mentioned problems and try again.
$
See this says nothing at all about why line #2:
Code:
vdr=valid_dir "/home/files/Dropbox";
is not calling/executing correctly. It must start with/at the 1st error, which it does not!
I have tried:
Code:
vdr=valid_dir("/home/files/Dropbox");
and
vdr=valid_dir{"/home/files/Dropbox"};
and other combinations but nothing is passing a value into the function. Even declared the function the following ways:
Code:
function valid_dir {
},
function valid_dir () {
},
and
valid_dir () {
}
Some say the function has to be declared first in the code calling it, other say not. I've been able to use functions both ways but this function is not working no matter what I try.
This is not the C programming language. That line is simply setting variable vdr to the literal string "valid_dir /home/files/Dropbox". Oops, sorry. It's adding a variable vdr set to the literal string "valid_dir" in the environment and trying to invoke a command "/home/files/Dropbox" with that environment. There is nothing illegal about that. It's just nothing like what you want, and does not invoke the "valid dir" function at all.
The return value from the most recent command, shell function, or shell script** is available in the variable "$?", and that is the only place it can be found. If you want to save it in another variable, you have to do that immediately in another command:
Code:
valid_dir "/home/files/Dropbox"
vdr=$?
** It gets more complicated for compound commands like "if ...; then ...fi" and "for ...; do ... done". The bash manpage has the details.
Last edited by rknichols; 06-26-2017 at 06:09 PM.
Reason: Oops, sorry. ...
var=$(testfunc) is taking the STDOUT from testfunc ("hello there") and putting it in the variable "var". The exit code from testfunc is only accessible with the special variable "$?", which in this case contains "17".
Last edited by suicidaleggroll; 06-26-2017 at 04:27 PM.
because that is syntactically acceptable, but will not do what you want (actually it has another meaning).
in bash the functions can only return with a single number, nothing more.
The returned value will be put into $?, the syntax var=function(something) - and all the other variants you tried - is incorrect in bash.
what you want probably works this way:
Code:
vdr=$(func_name argument)
but in this case you must not use return to set the return value, but echo or print.
Following up on all my open threads, looking to close/solve them all.
Cheers!
TBNK
Bumping your thread without content is frowned upon here at LQ.
Do not resurrect necro threads even if you are OP. LQ Rules
Quote:
Do not post if you do not have anything constructive to say in the post.
When posting in an existing thread, ensure that what you're posting is on-topic and relevant to the thread. If the content of your post will interfere with the current discussion, you should start a new thread.
While almost every question does get an answer, we cannot guarantee a response. If your thread does not receive any responses, it will automatically be bumped twice.Threads should not be manually bumped without including additional information.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.