I'm looking for a way of processing a shell script to determine:
It doesn't need to recurse down through the dependencies, just list what it runs directly. I could probably write something that does this myself but it must have been done before ... I'm just not finding it.
As a child process of the main shell, a subshell executes a list of commands in a shell script as a batch (so-called "batch processing"). In some cases, you may want to know the process ID (PID) of the subshell where your shell script is running. This PID information can be used under different circumstances.
This answer is not useful. Show activity on this post. #$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# , as you had it, prints the number of arguments passed to a shell script (like $* prints all arguments).
There are many ways to display a text file in a shell script. You can simply use the cat command and display back output on screen. Another option is to read a text file line by line and display back the output. In some cases you may need to store output to a variable and later display back on screen.
A function is a block of code that carries out a certain activity. A function can be called and reused. In shell scripting, functions are analogous to other programming languages' subroutines, procedures, and functions. Your function's name is function_name , and you'll call it by that name throughout your scripts.
You can use 'strace' to run a script and see everything the script and its subprocesses do, including looking for and opening files. For example:
$ cat foo.sh
#!/usr/bin/env bash
touch /tmp/foon
$ chmod +x foo.sh
$ strace -f -e execve,access,open,stat -o foo.trace ./foo.sh
$ cat foo.trace
32176 execve("./foo.sh", ["./foo.sh"], [/* 42 vars */]) = 0
32176 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
32176 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
32176 open("/usr/local/lib/tls/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
...
32176 execve("/bin/bash", ["bash", "./foo.sh"], [/* 42 vars */]) = 0
...
32177 execve("/usr/bin/touch", ["touch", "/tmp/foon"], [/* 41 vars */]) = 0
32177 open("/tmp/foon", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
...
32176 --- SIGCHLD (Child exited) @ 0 (0) ---
$
I've trimmed a lot of the other activity going on there (opening system libraries; looking up locale data; and much more). Check out 'man strace' for details on what the options mean; -f, -o, and -e are the ones I use most often.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With