I see you already know linuxcommand.org, which is a good step-by-step tutorial. But I recommend the
BashGuide for a more thorough, and readable, introduction to scripting. It covers all the basic concepts you need to know.
I think the first thing you need to understand is that a shell script is, in the end, just a collection of shell commands contained in a text file. It's simply a way to automate the actions you would normally do in the shell, so pretty much anything you'd run directly can also be placed in a script, and vice versa. Scripts always run serially, from first line to last, although this can include loops, functions, and other complex commands as well, so the execution doesn't always flow exactly line-by-line.
The next thing I think you need to recognize is the concept of environments. The shell you're running (like all commands) has an environment consisting of various variables, functions, and other settings, including the "$PWD" (present working directory), "$PATH", and others. The
env command or the
declare -p built-in, will list the values set in your current environment.
When you execute a script, it will run in a separate process, with an environment of its own. Some of these values are inherited from the parent process (i.e. if the
export flag was set), and other can be set/modified inside the script itself at any time. Remember too that no process can ever directly affect the environment of another, and whenever a process exits, it's environment disappears with it.
Most scripts follow this general pattern:
1) The shebang. The first line tells the system which interpreting program to use for the script. If the code that follows is designed for
bash, then the shebang needs to be
#!/bin/bash. Note that the shebang is only important for direct execution of the script file (there are other ways to run it), but this is certainly what you'll usually want to do anyway.
2) Environment settings. Remember the serial nature of the script; all settings have to be made before the commands that use them. This includes shell variables, user variables, functions, and various shell settings. You'll also need to
export any values that need to be inherited by sub-processes, i.e. any command that's not built in to shell. Most of the time, however these values will only be needed by the shell itself.
3) The commands you will run, in order of operation. These will generally look exactly the same as they would if you ran them directly, except that variables can be substituted for fixed values. This can also include complex commands such as loops and tests. In general, one command has to finish before the next one is executed, although there are ways to run commands in parallel.
Quote:
Is this argument suitable for a script?
|
Can you run it as-is in the shell? If so, then it can be placed in a script (assuming the script's environment is similar enough).
Speaking of which, the $PATH environment variable is simply a list of directories that the shell looks into when trying to find a command. And so it's really only necessary if you want to use commands without typing the full path to them. The following are equivalent:
Code:
$ /path/to/myscript.sh #run the script directly, specifying the absolute path
$ export PATH=/path/to:$PATH #prepend the script's directory to the existing PATH
$ myscript.sh #the run the script
As a bit of more general advice, I recommend not trying to rush into it too fast. In spite of some of the things I just wrote, there are a lot of subtle nuances and gotchas to scripting that can trip you. Build yourself up step-by-step with simple scripts first, and make sure you understand the basics of what you're doing before you dive into anything really deep.
If you post your scripting attempts here you're bound to get a lot of good pointers.
And no, you probably do NOT want to dive into something like
C at this point. Languages like that are several times more complex than shell scripting, and you're likely to only lose yourself further. Working your way up the other way is reasonable, however, as scripting will teach you many of the foundational concepts that are also used in more advanced languages.