LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 06-15-2017, 04:36 PM   #1
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Rep: Reputation: Disabled
Valid Directory


All,

Trying to get a valid directory from my BASH function at:

https://pastebin.com/xU7DUqQH

Possibilities are:
  1. Directory, exists, is valid and connected,
  2. Directory, exists, is valid but not connected,
  3. Directory, exists, but not valid,
  4. Directory doesn't exists

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.

All help appreciated!

Cheers!

TBNK

Last edited by TBotNik; 06-15-2017 at 04:38 PM.
 
Old 06-15-2017, 05:30 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
if [[ -d <dir> ]] ; then echo "found"; else echo "Not Found" ; fi
"connected"? Like a mount?
nfs, samba, something

https://opensource.com/life/16/10/ho...ical-questions
 
Old 06-16-2017, 12:32 PM   #3
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Code:
if [[ -d <dir> ]] ; then echo "found"; else echo "Not Found" ; fi
"connected"? Like a mount?
nfs, samba, something

https://opensource.com/life/16/10/ho...ical-questions
Habitual,

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.

Cheers!

TBNK

Last edited by TBotNik; 06-16-2017 at 12:36 PM.
 
Old 06-16-2017, 12:50 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,631

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
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 would suggest you to try www.shellcheck.net to check your script.
 
Old 06-16-2017, 01:25 PM   #5
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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 would suggest you to try www.shellcheck.net to check your script.
pan64,

No expert here, but BASH book I'm working from says "return $value" will return either numeric or string value assigned to "value".

Also I've been using shellcheck and the messages it returns is like speaking Chinese or Korean, say nothing at all useful for debugging!

Cheers!

TBNK
 
Old 06-16-2017, 01:41 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,631

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
Quote:
Originally Posted by TBotNik View Post
pan64,

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).
 
Old 06-18-2017, 09:25 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,701

Rep: Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208
Quote:
Originally Posted by TBotNik View Post
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.
 
Old 06-26-2017, 03:30 PM   #8
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Shellcheck output

Here is the actual output from shellcheck:

Code:
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.

Nothing has worked!

Cheers!

TBNK

Last edited by TBotNik; 06-26-2017 at 03:41 PM.
 
Old 06-26-2017, 04:02 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,767

Rep: Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209
Quote:
Originally Posted by TBotNik View Post
See this says nothing at all about why line #2:
Code:
vdr=valid_dir "/home/files/Dropbox";
is not calling/executing correctly.
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. ...
 
Old 06-26-2017, 04:25 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Maybe this will help:
Code:
$ cat tester.sh
#!/bin/bash

function testfunc() {
  echo "hello there"
  return 17
}

echo "welcome"
var=$(testfunc)
echo $?
echo $var

$ ./tester.sh
welcome
17
hello there
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.
 
Old 06-27-2017, 02:02 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,631

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
Quote:
Originally Posted by TBotNik View Post
See this says nothing at all about why line #2:
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.
 
Old 05-03-2020, 11:55 AM   #12
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Followup

All,

Following up on all my open threads, looking to close/solve them all.

Cheers!

TBNK
 
Old 05-06-2020, 04:47 AM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Reported.
 
Old 05-06-2020, 08:20 AM   #14
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,922
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
Moderator Response

Quote:
Originally Posted by TBotNik View Post
All,

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP Valid Hostname > Reverse DNS is not a valid Hostname ..... Anandhc Linux - Newbie 5 03-30-2016 01:19 PM
[SOLVED] Bash Scripting (BT5-R1) - not a valid identifier & \r': No such file or directory markcoker Programming 2 09-11-2011 06:08 AM
Shell script doesn't think a directory is valid (but it is) ssrobins Linux - General 4 04-21-2010 09:23 PM
Not a valid partition :( harsha101087 Linux - Newbie 18 03-06-2009 04:17 PM
Valid subnets. carlosruiz Linux - Networking 10 03-06-2006 11:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration