LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2019, 02:42 PM   #16
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 150

Rep: Reputation: 111Reputation: 111

Minor correction. I just checked my copy and the examples(with the exception of bash specific features) were tested using ksh but will work with bash also. So they weren't tested with both like I had originally said but the book's focus is posix.
 
Old 06-17-2019, 05:05 AM   #17
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,953

Rep: Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267
Quote:
Originally Posted by Mechanikx View Post
Minor correction. I just checked my copy and the examples(with the exception of bash specific features) were tested using ksh but will work with bash also. So they weren't tested with both like I had originally said but the book's focus is posix.
Advantage BASH: as with KSH you can do things better and faster in less code than other shells. Advantage POSIX: what you do to POSIX standard will work in ANY shell , although some things require specific shells to be in POSIX mode for the behavior. (Most things just work)

Some of the "*nix bible" shell chapters also handle shell differences. This is an area where research on the web may present more current and complete information.
https://www.howtogeek.com/68563/HTG-...-LINUX-SHELLS/
https://en.wikipedia.org/wiki/Compar...command_shells

Note that most distributions do not install a POSIX shell by default. Most link the common shell to that system to /bin/sh, and that shell fires up in POSIX MODE if called that way. This does not change ALL of the shell features, but modifies some behaviors to be more POSIX standard.

Last edited by wpeckham; 06-17-2019 at 05:28 AM.
 
Old 06-17-2019, 07:44 AM   #18
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,698

Rep: Reputation: Disabled
Quote:
Originally Posted by FOSSilized_Daemon View Post
Do you by chance know of a POSIX way to do something similar to that or do I have to just do them one at a time?

edit:

mkdir -p $HOME/docs $HOME/docs/notes $HOME/docs/pdfs $HOME/docs/dev $HOME/dl $HOME/vids $HOME/music $HOME/pics $HOME/.config $HOME/.builds
Yea, the only way is to be more verbose or do it in a loop, but using a loop would mean more than one invocation of mkdir instead of only one.
 
Old 06-17-2019, 08:52 AM   #19
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,881

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
this is how I do it.
Code:
#base path
arrayDir=/run/media/$USER/250NTFS

#dir name 
move1=( 
$arrayDir/Music_2_do1
$arrayDir/Music_2_do2
$arrayDir/Music_2_do3
$arrayDir/Music_2_do4
)

move2=( 
$arrayDir/TempMusic1
$arrayDir/TempMusic2
$arrayDir/TempMusic3
$arrayDir/TempMusic4
)

 
#create holding dirs
mkdir -pv "${move1[@]}"
mkdir -pv "${move2[@]}"
this allow easy reassignment to the base path. It is just chaining the one line for the base path if I plugin a different external drive named something other that what is posted. keeping the rest of the code intact that use the path, and dir names. Eliminating having to change every line that uses the path/dir

the use of $USER makes it more flexible so it doesn't matter which user is logged in using this script. Auto mount mounts in /run/media/(users logged name)/device adding $USER gives current user logged in using that script.

Last edited by BW-userx; 06-17-2019 at 08:55 AM.
 
Old 06-17-2019, 09:02 AM   #20
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,698

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
this is how I do it.
Code:
#base path
arrayDir=/run/media/$USER/250NTFS

#dir name 
move1=( 
$arrayDir/Music_2_do1
$arrayDir/Music_2_do2
$arrayDir/Music_2_do3
$arrayDir/Music_2_do4
)

move2=( 
$arrayDir/TempMusic1
$arrayDir/TempMusic2
$arrayDir/TempMusic3
$arrayDir/TempMusic4
)

 
#create holding dirs
mkdir -pv "${move1[@]}"
mkdir -pv "${move2[@]}"
this allow easy reassignment to the base path. It is just chaining the one line for the base path if I plugin a different external drive named something other that what is posted. keeping the rest of the code intact that use the path, and dir names. Eliminating having to change every line that uses the path/dir

the use of $USER makes it more flexible so it doesn't matter which user is logged in using this script. Auto mount mounts in /run/media/(users logged name)/device adding $USER gives current user logged in using that script.
Just to make it clear, I was talking about posix solutions and your solution is for bash. Not to say either approach is better or worse.
 
Old 06-17-2019, 09:06 AM   #21
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,881

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
Quote:
Originally Posted by orbea View Post
Just to make it clear, I was talking about posix solutions and your solution is for bash. Not to say either approach is better or worse.
my bag... I was just going for simplicity in bash. either way hope he learns something... more options.
 
Old 06-17-2019, 09:46 AM   #22
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,698

Rep: Reputation: Disabled
Now that I thought about it more, here is a similar way to do it with portable shell.

Code:
#!/bin/sh

# I think $USER should be available everywhere?
# https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
arrayDir="/run/media/$USER/250NTFS"

move1=" \
$arrayDir/Music_2_do1 \
$arrayDir/Music_2_do2 \
$arrayDir/Music_2_do3 \
$arrayDir/Music_2_do4"

move2=" \
$arrayDir/TempMusic1 \
$arrayDir/TempMusic2 \
$arrayDir/TempMusic3 \
$arrayDir/TempMusic4"

# printf is highly preferred rather than using echo with posix
# and is used to work around how zsh does not split unquoted
# variables by default.
set -- $(printf %s "$move1 $move2")
 
# The posix spec doesn't have -v for mkdir
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
mkdir -p -- "$@"

Last edited by orbea; 06-17-2019 at 10:18 AM.
 
Old 06-17-2019, 09:57 AM   #23
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,881

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
Quote:
Originally Posted by orbea View Post
Now that I thought about it more, here is a similar way to do it with portable shell.

Code:
#!/bin/sh

# I think $USER should be available everywhere?
# https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
arrayDir="/run/media/$USER/250NTFS"

move1=" \
$arrayDir/Music_2_do1 \
$arrayDir/Music_2_do2 \
$arrayDir/Music_2_do3 \
$arrayDir/Music_2_do4"

move2=" \
$arrayDir/TempMusic1 \
$arrayDir/TempMusic2 \
$arrayDir/TempMusic3 \
$arrayDir/TempMusic4"

# printf is highly preferred rather than using echo with posix
# and is used to work around how zsh does not split unquoted
# variables by default.
set -- $(printf %s "$move1 $move2")
 
# The posix spec doesn't have -v for mkdir
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
mkdir -p "$@"
testing that in zsh though shebang directs shell to be used no matter what shell is running it.

I just changed the base path to $HOME because I didn't have, and did not want to plug in my external of same name.
Code:
userx@archomeo 100% 4.70G   17.06.19 09:52:29AM
~ chmod +x mkdirtest

userx@archomeo 100% 4.69G   17.06.19 09:52:43AM
~ ./mkdirtest

userx@archomeo 100% 4.69G   17.06.19 09:52:47AM
~ ls               
bin	     Downloads	Music_2_do1  Pictures		  Templates   TempMusic4
create-user  Dropbox	Music_2_do2  post-install-update  TempMusic1  Videos
Desktop      mkdirtest	Music_2_do3  PostInstallUpdates   TempMusic2
Documents    Music	Music_2_do4  Public		  TempMusic3
Yet when I changed the shebang to #!/bin/zsh and ran it in a zsh shell it still worked.

but what is this actually doing?
Code:
set -- $(printf %s "$move1 $move2")
because in a zsh shell i didn't see any output, is that working in tandem with
Code:
mkdir -p "$@"
causing $@ to be each path/dir when mkdir is called?

POSIX Environment Variables

Last edited by BW-userx; 06-17-2019 at 10:08 AM.
 
Old 06-17-2019, 10:18 AM   #24
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,698

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
Code:
set -- $(printf %s "$move1 $move2")
because in a zsh shell i didn't see any output, is that working in tandem with
Code:
mkdir -p "$@"
causing $@ to be each path/dir when mkdir is called?
POSIX shell has only one array (The "$@" array) unlike bash where you can create many arrays as in your example. Here I used 'set' to assign the "$move1" and "$move2" variables to the "$@" array. I can then use this with 'mkdir' as an argument to pass all of the directories from the move variables. Some care has to be taken to not overwrite the "$@" array before actually using it, but it can be set as many times as needed. The '--' is used to indicate that there will be no more arguments and in the case any of the directories starts with a '-' character they will not be recognized by 'set' as options. In retrospect this should be used for 'mkdir' too.

Code:
mkdir -p -- "$@"
Someone could also use 'eval' instead of `printf', but this can be dangerous unless you know exactly what the "$move1" and "$move2" variables contain. For example if someone had a variable that contained the wrong combination of quotes and 'rm -rf' disastrous things could happen.

Code:
eval "set -- $move1 $move2"
 
3 members found this post helpful.
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can someone explain for me this shell script please? (It was detected on my honeypot) haz12 Linux - Security 8 05-02-2016 11:34 AM
How can you tell that someone is bad? or Good ? someone is bad? or Good ? abrenar General 10 02-24-2009 02:42 PM
Cloning disk partitions; could someone review this? mlitwinowich Linux - General 2 10-03-2008 01:17 PM
Could someone tell me what's wrong with my shell script please? RowanB Programming 4 11-11-2004 11:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration