CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

why `cd ${0%/*} || exit 1 # Run from this directory'

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By zwlwf

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 6, 2019, 21:09
Default why `cd ${0%/*} || exit 1 # Run from this directory'
  #1
Member
 
Ran
Join Date: Aug 2016
Posts: 69
Rep Power: 9
random_ran is on a distinguished road
I notice that OpenFOAM use this command almost in every tutorial:

#+BEGIN_SRC sh
cd ${0%/*} || exit 1 # Run from this directory
#+END_SRC

I usually delete this line then execute: `./Allrun'

I checkout the bash manual ( This is Edition 4.2, last updated 28
December 2010, of 'The GNU Bash Reference Manual', for 'Bash', Version
4.2. )

At the page: 3.4.2 Special Parameters

#+BEGIN_QUOTE
'0'
Expands to the name of the shell or shell script. This is set at
shell initialization. If Bash is invoked with a file of commands
(*note Shell Scripts:, '$0' is set to the name of that file. If
Bash is started with the '-c' option (*note Invoking Bash:, then
'$0' is set to the first argument after the string to be executed,
if one is present. Otherwise, it is set to the filename used to
invoke Bash, as given by argument zero.

#+END_QUOTE

I also check '-c' option on page: 6.1 Invoking Bash
#+BEGIN_QUOTE
'-c STRING'
Read and execute commands from STRING after processing the options,
then exit. Any remaining arguments are assigned to the positional
parameters, starting with '$0'.

#+END_QUOTE

Interesting, when I use the command: `bash -c ./Allrun

It works.


Why they put this line here?
__________________
Yours in CFD,

Ran
random_ran is offline   Reply With Quote

Old   March 7, 2019, 01:23
Default
  #2
New Member
 
Weiliang Zhu
Join Date: Apr 2016
Posts: 11
Rep Power: 11
zwlwf is on a distinguished road
Just as the comment tells : # Run from this directory.

${i%pattern} is a bash operator, which means remove the least pattern from the end of variable i . For an example, $0 is /home/xxx/a.out, '/*' matches the /a.out, so ${0%/*} becomes /home/xxx, that is the directory of execuatable file, a.out.
kandelabr likes this.
zwlwf is offline   Reply With Quote

Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[swak4Foam] Problem installing swak_2.x for OpenFoam-2.4.0 towanda OpenFOAM Community Contributions 6 September 5, 2015 21:03
Working directory via command line Luiz CFX 4 March 6, 2011 20:02
critical error during installation of openfoam Fabio88 OpenFOAM Installation 21 June 2, 2010 03:01
Re : Problem Installing OpenFOAM on Centos -5.3 mohanphy OpenFOAM Installation 1 February 7, 2010 19:09
Gerris software installation mer Main CFD Forum 2 November 12, 2005 08:50


All times are GMT -4. The time now is 15:46.