Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 23 Jul 2019, 22:55
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Bash Discrepancies
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [17 Posts]   Goto page: 1, 2 Next
Author Message
slavvo67

Joined: 12 Oct 2012
Posts: 1600
Location: The other Mr. 305

PostPosted: Sat 29 Jul 2017, 09:39    Post subject:  Bash Discrepancies  

A few years back, I believe MochiMoppel and others had a thread about differences in BASH versions. Recently, I was scripting and wanted the script to determine whether a directory existed or not. Almost every search example reflects as:

if [ -d "$DIRECTORY" ]; then
echo "$DIRECTORY exists"
fi

Well, that very common example is not working in GNU bash, version 4.3.0(1)-release (x86_64-pc-linux-gnu), so I finally went with the following:

if test -d "$DIRECTORY"
then
echo "$DIRECTORY exists and this one works"
fi

Determining if the directory does not exist:

if test ! -d "$DIRECTORY"; then
echo "$DIRECTORY does not exist and this one works"
fi

This may be on that thread that I couldn't locate so Flash, if you want to move it; no offense taken.

Best,

Slavvo67
Back to top
View user's profile Send private message 
8Geee


Joined: 12 May 2008
Posts: 1971
Location: N.E. USA

PostPosted: Sat 29 Jul 2017, 14:23    Post subject:  

I am wondering if this scripting problem exists in the 4.2 level of BASH. I recall an update offered by Slackware available to Slacko 5.7 that was an update to shellshock vunerability.

The move from 4.1.0(2) to 4.2.53(1) looked suspiciously large, but everything worked except printing using foo/gutenprint 5.2.9 was broken in cups 1.4.8. I had to revert the updated spins back to BASH 4.1.0(2) if printing was desired.

editted for typos in bash version... 4.1.0(2) is correct

/Tangent
8Geee

_________________
Linux user #498913 Some people need to reimagine their thinking.

Last edited by 8Geee on Tue 01 Aug 2017, 16:05; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1853
Location: Japan

PostPosted: Sat 29 Jul 2017, 23:15    Post subject:  

Type [ into a console and press Enter. You should receive a missing `]' error message. Do you?
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1600
Location: The other Mr. 305

PostPosted: Sun 30 Jul 2017, 13:05    Post subject:  

Mochi,

Was that to me or 8Geee

Mine works

# [
bash: [: missing `]
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4856
Location: Blue Springs, MO

PostPosted: Sun 30 Jul 2017, 14:15    Post subject:  

I put together a bunch of shortcuts at
https://github.com/technosaurus/PD-snippets/blob/master/PosixShell/sh

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.

Last edited by technosaurus on Fri 04 Aug 2017, 03:19; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1853
Location: Japan

PostPosted: Sun 30 Jul 2017, 21:51    Post subject:  

slavvo67 wrote:
Mine works
Great. Since the [ command is alive and well you'd better tell us what exactly "is not working". The -d switch? The variable? What error message do you get - if any? And how did you invoke bash?

Try this in a console and report back:
Code:
[ -d / ] && echo true || echo false
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1600
Location: The other Mr. 305

PostPosted: Tue 01 Aug 2017, 13:54    Post subject:  

Received a true for the script, above so the -d seems to be fine. It must be the variable that there's an issue with.
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 897

PostPosted: Tue 01 Aug 2017, 14:13    Post subject:  

This works for me:
Code:

#! /bin/bash
#
DIRECTORY="/usr/local/sbin"

if [ -d "$DIRECTORY" ]; then
 printf "\"%s\" exists\n" $DIRECTORY
fi
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1600
Location: The other Mr. 305

PostPosted: Tue 01 Aug 2017, 14:18    Post subject:  

No good for me....
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1853
Location: Japan

PostPosted: Wed 02 Aug 2017, 00:42    Post subject: Re: Bash Discrepancies  

slavvo67 wrote:
A few years back, I believe MochiMoppel and others had a thread about differences in BASH versions
You mean this one?

Looks familiar. Code works fine in your console (which presumably is invoked with /bin/sh) but not with your bash version when invoked with /bin/bash. You shrugged it off as "probably a typo" and never followed up. Now "it must be the variable", but you don't check it. You seem to have a serious problem with your bash version but given your terse responses there is little to work on.
Back to top
View user's profile Send private message 
8Geee


Joined: 12 May 2008
Posts: 1971
Location: N.E. USA

PostPosted: Wed 02 Aug 2017, 04:40    Post subject:  

After experimenting... as written by jafadmin its just blank. no error, no solve, nada.

inserting a $ throws a bash error directory or file not found (correct, there is no such file)

HOWEVER removing the = in addition to the $ delta also returns not found.

BASH 4.1.0(2)

REMOVED to clarify

regards
8Geee

_________________
Linux user #498913 Some people need to reimagine their thinking.

Last edited by 8Geee on Wed 02 Aug 2017, 16:23; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1853
Location: Japan

PostPosted: Wed 02 Aug 2017, 05:05    Post subject:  

8Geee wrote:

$directory "/usr/local"
Shocked Don't make it worse Laughing

jafadmin's code returns also "just blank" for me as I don't have "/usr/local/sbin" , but it works with /usr/local. And another thing: Such tests should always include a test for false, so it should be if..then ..else..fi.
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 897

PostPosted: Wed 02 Aug 2017, 21:19    Post subject:  

MochiMoppel wrote:
8Geee wrote:

$directory "/usr/local"
Shocked Don't make it worse Laughing

jafadmin's code returns also "just blank" for me as I don't have "/usr/local/sbin" , but it works with /usr/local. And another thing: Such tests should always include a test for false, so it should be if..then ..else..fi.


Yeah, I probably should have used "/etc". Thanks for the observation.
Back to top
View user's profile Send private message 
tallboy


Joined: 21 Sep 2010
Posts: 1387
Location: Drøbak, Norway

PostPosted: Wed 02 Aug 2017, 23:08    Post subject:  

This may be one of those few moments when I actually suggest something that could be useful? Shocked

Perhaps you should run 'echo $PATH' in a terminal to find a suitable place for a script?

tallboy

_________________
True freedom is a live Puppy on a multisession CD/DVD.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4856
Location: Blue Springs, MO

PostPosted: Fri 04 Aug 2017, 03:30    Post subject:  

I know a lot of puppy scripts (including some my own) use $ALL_CAPS for variable names, but it is actually bad practice because $ALL_CAPS is used for system variables like $USER, $PWD, $IFS ... perhaps now $DIRECTORY means something in bash. You can use lower case or camelCase variable names... kindof like how C reserves variables starting with 1 or 2 underscores for the C library and the compiler.

Here are the common test operators as easier to remember functions:
Code:
file_is_block()             [[ -b "$@" ]]
file_is_char()              [[ -c "$@" ]]
file_is_directory()         [[ -d "$@" ]]
file_is_empty()             [[ !  -s "$@" ]]
file_is_executable()        [[ -x "$@" ]]
file_is_mine()              [[ -O "$@" ]]
file_is_modified()          [[ -N "$@" ]]
file_is_my_group()          [[ -G "$@" ]]
file_is_newer_than()        [[ "$1" -nt "$2" ]]
file_is_not_block()         [[ !  -b "$@" ]]
file_is_not_char()          [[ !  -c "$@" ]]
file_is_not_directory()     [[ !  -d "$@" ]]
file_is_not_empty()         [[ -s "$@" ]]
file_is_not_executable()    [[ !  -x "$@" ]]
file_is_not_mine()          [[ !  -O "$@" ]]
file_is_not_modified()      [[ !  -N "$@" ]]
file_is_not_my_group()      [[ !  -G "$@" ]]
file_is_not_newer_than()    [[ !  "$1" -nt "$2" ]]
file_is_not_older_than()    [[ !  "$1" -ot "$2" ]]
file_is_not_pipe()          [[ !  -p "$@" ]]
file_is_not_readable()      [[ !  -r "$@" ]]
file_is_not_regular()       [[ !  -f "$@" ]]
file_is_not_sgid()          [[ !  -g "$@" ]]
file_is_not_socket()        [[ !  -S "$@" ]]
file_is_not_sticky()        [[ !  -k "$@" ]]
file_is_not_suid()          [[ !  -u "$@" ]]
file_is_not_symlink()       [[ !  -L "$@" ]]
file_is_not_there()         [[ !  -e "$@" ]]
file_is_not_to_tty()        [[ !  -t "$@" ]]
file_is_not_writable()      [[ !  -w "$@" ]]
file_is_older_than()        [[ "$1" -ot "$2" ]]
file_is_pipe()              [[ -p "$@" ]]
file_is_readable()          [[ -r "$@" ]]
file_is_regular()           [[ -f "$@" ]]
file_is_sgid()              [[ -g "$@" ]]
file_is_socket()            [[ -S "$@" ]]
file_is_sticky()            [[ -k "$@" ]]
file_is_suid()              [[ -u "$@" ]]
file_is_symlink()           [[ -L "$@" ]]
file_is_there()             [[ -e "$@" ]]
file_is_to_tty()            [[ -t "$@" ]]
file_is_writable()          [[ -w "$@" ]]
files_are_hardlinked()      [[ "$1" -ef "$2" ]]
files_are_not_hardlinked()  [[ ! "$1" -ef "$2" ]]

usage:
file_is_directory "$MyDir" && echo "$MyDir is a directory."

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 2 [17 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0587s ][ Queries: 11 (0.0089s) ][ GZIP on ]