# Execute bash scripts from inside Natural Programs?

**URL:** <https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Linux\
**Created:** [February 14, 2007, 7:46am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950 "2007-02-14T07:46:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [February 14, 2007, 7:46am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/1 "2007-02-14T07:46:09Z")

</div>

We’re at that part of the conversion where we are looking at our RJE submits (IBM/VSE to Suse Linux) and I was trying to execute bash scripts from inside of natural. I am trying to figure out how to change the path to where the script exists, to be able to ./ the bash script, keeps telling me ‘No such file or directory’.  
And, I have the permissions set to -rwxrwxrwx , So hopefully that is not the problem. Anyone else execute (bash)scripts from inside of Natural where you have to change the path?

thanks in advance  
John

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 14, 2007, 4:17pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/2 "2007-02-14T16:17:44Z")

</div>

Let’s say you want to execute the script test.bash which is located in the directory /my/dir. You got 2 possiblilities.

#1:

```auto
call 'shcmd' '/my/dir/test.bash'
```

This means: Always work with absolute paths. But then you have to ensure, that your script also uses absolute paths.

#2:  
At the moment, the regarding Linux-command is regarded as a spam-word by this forum (See [http://natural.forums.softwareag.com/viewtopic.php?p=3023#3023](http://natural.forums.softwareag.com/viewtopic.php?p=3023#3023) ). So I’m not able to post it. Maybe I’ll add it later, if the word is removed from the spam-list…

---

<div class="post-metadata">

**Author:** ![KlaBue](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@KlaBue](https://techcommunity.softwareag.com/u/KlaBue)\
**Post date:** [February 14, 2007, 10:53pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/3 "2007-02-14T22:53:50Z")

</div>

> [@](#):
>
> Anyone else execute (bash)scripts from inside of Natural …

yes, of course.

> [@](#):
>
> … where you have to change the path?

it depends.  
Most of our central-Scripts are located in a central folder and the name of the folder is part of the PATH-Variable. Therefore the script is simply started by its name:

```auto

os-command := 'adarep2excel.bsh'
callnat 'USR1052N' usr1052l   
if Response ne 0 
	...
end-if
```

if it is not a central script one can specify the full path name, as mathias wrote.

some remarks on starting scripts from natural:

1. use USR1052N instead of SHCMD.
2. start the script with the nohup-command. otherwise the execution of the script is terminated if the parent process comes to an end.
3. use the ampersand (&) to indicate that this script has to be executed in batch.
4. in addition you can specify the keyword [NO]SCREENIO. (see description of USER1052N)

```auto

os-command := 'nohup veryLongRunningScript.bsh & NOSCREENIO'
callnat 'USR1052N' usr1052l   
if Response ne 0 
	...
end-if
```

hth

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 15, 2007, 1:43pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/4 "2007-02-15T13:43:48Z")

</div>

> [@KlaBue](#):
>
> 1. use USR1052N instead of SHCMD.

Why? I cannot find a remark in the Natural documentation that SHCMD should be avoided. [http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat623win/pg/pg\_furth\_syscom.htm](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat623win/pg/pg_furth_syscom.htm)

> [@](#):
>
> 1. start the script with the nohup-command. otherwise the execution of the script is terminated if the parent process comes to an end.
> 2. use the ampersand (&) to indicate that this script has to be executed in batch.

Good hints! By default Natural waits until the shell command terminates. nohup and ampersand are only neccessary, if you want to submit a shell command and you don’t want to wait for its termination.

---

<div class="post-metadata">

**Author:** ![KlaBue](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@KlaBue](https://techcommunity.softwareag.com/u/KlaBue)\
**Post date:** [February 15, 2007, 11:48pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/5 "2007-02-15T23:48:39Z")

</div>

> [@](#):
>
> Why? I cannot find a remark in the Natural documentation that SHCMD should be avoided.

It seems to be a matter of opinion: if i have the choice to use a “call” or a “callnat” i will always decide to use the callnat.

btw: there are some other things, that should be avoided, not mentioned in the Natural documentation :lol:

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [February 20, 2007, 7:31pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/6 "2007-02-20T19:31:49Z")

</div>

> [@Matthias](#):
>
> Maybe I’ll add it later, if the word is removed from the spam-list…

Here’s #2:

```auto
call 'shcmd' 'cd /my/dir && ./test.bash'
```

The double-ampersand means: If the change-directory-command was successfull, execute test.bash. This is a suitable solution, if you want to work with relative paths inside your script.

---

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [February 22, 2007, 11:26pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/7 "2007-02-22T23:26:40Z")

</div>

Okay, thanks for all the input.  
Let me see if I can get it to work.

---

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [June 7, 2007, 7:16am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/8 "2007-06-07T07:16:12Z")

</div>

Mathias, Still there?? haha, I know its been a few months, but hey such is the nature of this project. Here is what I tried :  
Logged in as sag,  
call ‘shcmd’ ‘cd Batch && ./NatBatch.bsh’  
Then: call ‘shcmd’ 'pwd \> /usr/sag/test\_path  
then I read the file test\_path as a work file. What I am getting is /usr/sag as the  
current path. I expected to see /usr/sag/Batch but I did not. Seems as though it will only do one shcmd, any subsequent shcmd do not seem to have any effect.  
The NatBatch.bsh didnt run either. I cleared out the test\_path file so I can tell where my current path is, but it’s always only usr/sag, and I would expect it to be  
usr/sag/Batch.  
Any thots?

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [June 7, 2007, 3:18pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/9 "2007-06-07T15:18:33Z")

</div>

> [@papajohn](#):
>
> call ‘shcmd’ ‘cd Batch && ./NatBatch.bsh’  
> Then: call ‘shcmd’ 'pwd \> /usr/sag/test\_path  
> then I read the file test\_path as a work file. What I am getting is /usr/sag as the  
> current path. I expected to see /usr/sag/Batch but I did not.

I guess you do the pwd to “debug” the first shcmd.

Every “call ‘shcmd’” starts a new Unix-process. And every Unix-process starts on the home directory.

> [@](#):
>
> The NatBatch.bsh didnt run

NatBatch.bsh only starts if the preceding “cd Batch” was successful. Maybe you mistyped the directory name…

Please try the following:

```auto
call 'shcmd' 'cd Batch; pwd > /usr/sag/test_path' 
```

The semicolon means: pwd is executed regardless if “cd Batch” was successful or not.

---

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [June 8, 2007, 2:54am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/10 "2007-06-08T02:54:23Z")

</div>

Mathias,  
:lol: Thanks for taking time to show me the correct way, You are the Man…  
When I tried your sample I received the correct path in my work file, then I added ./NatBatch.bsh to that same call and it worked perfectly.  
Thank you

---

<div class="post-metadata">

**Author:** ![Magne\_Haug](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Magne\_Haug](https://techcommunity.softwareag.com/u/Magne_Haug)\
**Post date:** [June 22, 2007, 3:48pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/11 "2007-06-22T15:48:06Z")

</div>

If a script starts with the command

cd ${0%/\*}

it will change the directory to where the script is located.

It looks a bit odd, but the $0 contains the full path and name of the script,  
and the %/\* strips away the script name leaving only the full path.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [June 22, 2007, 6:00pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/12 "2007-06-22T18:00:24Z")

</div>

> [@Magne Haug](#):
>
> but the $0 contains the full path and name of the script,

From my point of view, this is not completely correct (if we’re talking about the bash). $0 contains the name of the shell. This could be simply “bash” or if you call a script without creating a subshell the name of the initial script.  
Please try out the following:

content of ~/test.bash

```auto
#!/bin/bash
echo $0
. ~/testdir/test2.bash
```

content of ~/testdir/test2.bash

```auto
#!/bin/bash
echo $0
```

If you execute ~/test.bash, your output is the following:

```auto
~/test.bash
~/test.bash
```

---

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [July 4, 2007, 2:05am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/13 "2007-07-04T02:05:52Z")

</div>

Ok, thanks again for all the help .

Now that I can submit a script with an absolute value,  
I was thinking if I can set up variables which contain the script,  
for example:  
An online program with a list of reports, that the user will just put an ‘X’ in the first column of the row, where:  
Row 1 = \_\_ Report 1  
Row 2 = \_\_ Report 2… etc  
When user puts an ‘X’ to select a report… My program will put a value to the report name and then submit the ‘shcmd’ .  
So I would set up the variable to contain the script like:  
Local  
1 Report1 (a25) init “/path1/path2/Script1.bsh”  
1 Report2 (a25) init “/path1/path2/Script2.bsh”  
I Then would do call ‘shcmd’ ‘Report1’ ??  
Probably too simple, but that’s what I envisioned,  
anyone doing something similar…  
I will try and see what happens anyway.  
If you have suggestions, as always I appreciate the help…

Results:  
I setup a local with initial values equal to the scripts…  
when I call USR1052N in order to execute the selected script  
It returns an error 127…

[/b]

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [July 5, 2007, 4:01pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/14 "2007-07-05T16:01:36Z")

</div>

> [@papajohn](#):
>
> Local  
> 1 Report1 (a25) init “/path1/path2/Script1.bsh”  
> 1 Report2 (a25) init “/path1/path2/Script2.bsh”  
> I Then would do call ‘shcmd’ ‘Report1’ ??

Delete the quotes around Report.

```auto
call 'shcmd' Report1
```

Of course you can do more complex shcmd-calls. Like that:

```auto
define data local
1 #command (A) dynamic const <'ls'>
1 #parameter (A) dynamic const <'-l'>
1 #files (A/1:10) dynamic const <'/etc/hosts','/etc/passwd'>
1 #output-file (A) dynamic const <'/tmp/shcmd-test.txt'>
*
1 #shcmd (A) dynamic
end-define
compress #command #parameter #files(*) ">" #output-file into #shcmd
display #shcmd (AL=78)
call 'shcmd' #shcmd
end
```

---

<div class="post-metadata">

**Author:** ![papajohn](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/papajohn/32/85_2.png) [@papajohn](https://techcommunity.softwareag.com/u/papajohn)\
**Post date:** [July 5, 2007, 9:41pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/15 "2007-07-05T21:41:35Z")

</div>

Thanks again Matthias, Looks like I need to check out the documentation on ‘dynamic’ variables… that is exactly what I needed.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [July 6, 2007, 11:16am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/16 "2007-07-06T11:16:27Z")

</div>

> [@papajohn](#):
>
> Looks like I need to check out the documentation on ‘dynamic’ variables… that is exactly what I needed.

The trick is to use a Natural-Alpha-Field which contains the OS-command.

Unfortunately, the Natural for Unix documentation doesn’t mention that.

[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat631unx/pg/pg\_furth\_syscom.htm](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat631unx/pg/pg_furth_syscom.htm)

But the documentation for windows does:

[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat631win/pg/pg\_furth\_syscom.htm](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat631win/pg/pg_furth_syscom.htm)

The next thing is to create an OS-command by compressing some components into a target field. Of course you can do this without using dynamic variables…

---

<div class="post-metadata">

**Author:** ![Anton\_Britz](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/anton_britz/32/123_2.png) [@Anton\_Britz](https://techcommunity.softwareag.com/u/Anton_Britz)\
**Post date:** [October 6, 2010, 6:17am UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/17 "2010-10-06T06:17:10Z")

</div>

Thanks for all the pointers/clues but none of the URL’s in these discussions work any more ( The Software AG documentation URL pointers but it’s better than nothing at all )

Anton

😃

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 6, 2010, 12:03pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/18 "2010-10-06T12:03:57Z")

</div>

To get the links to work you have to insert the latest Natural version.

i.e.

[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat639unx/pg/pg\_furth\_syscom.htm](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat639unx/pg/pg_furth_syscom.htm)

[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat639win/pg/pg\_furth\_syscom.htm](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat639win/pg/pg_furth_syscom.htm)

---

<div class="post-metadata">

**Author:** ![Anton\_Britz](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/anton_britz/32/123_2.png) [@Anton\_Britz](https://techcommunity.softwareag.com/u/Anton_Britz)\
**Post date:** [October 6, 2010, 8:41pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/19 "2010-10-06T20:41:51Z")

</div>

Thanks Matthias for responding because I looked at the “dates of last posts” on all these discussions and it looked a little “dead” to me…

a) Yes, the URL’s to Natural documentation could be adapted , as yuo suggested but  
2) URL pointers back into these discussions, can not be “adapted” , such as this URL at the top of these discussions… and my point was :

“What is the point in having these Software AG archive of discussions if the URL’s become obsolete because of documents being moved around on the Software AG server. If you have a plain/vanilla Windows 2000 Server, with NO Tamino, the users can do the same and the documents on the server, will stay in place” :

[http://natural.forums.softwareag.com/viewtopic.php?p=3023#3023](http://natural.forums.softwareag.com/viewtopic.php?p=3023#3023) )

Note: Information is knowledge/ power but not if you have kids managing it all

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:41pm UTC](https://techcommunity.softwareag.com/t/execute-bash-scripts-from-inside-natural-programs/61950/20 "2021-04-02T14:41:03Z")

</div>


