linux Part 8
Linux Part 8
Text files and text editors
We use the text editor such as nano or vi to edit text files, such as shell
scripts and configuration files.
Pico Editor
Nano
VI
Emacs
*Important Learn Vi for the exam
The editor lets you edit text files that are store in a plain format, text files cant include any graphics, fonts, you cant change fonts or emphasize the
text in any way.
Some files are editable only under the user, some files can only be edited as the root user, files such as config files.
We will look at these files
Human files
Program files
Formatted text files
Program and system configuration files
Program log files
When you change your files save a backup copy as you may need to revert
to the file if mistakes are made.
Scripts
A script is a program written in an interpreted language, typically associated with a shell or a compiled program. Users can write scripts that to automate tedious boring jobs or to perform new or complex tasks and manage startup processes
Shell scripts are edited in text editor
A shell script begins with
#! This tells linux this is a script
/bin/bash this is used to identify the shell used to run the script
/bin/sh is a symbolic link it, it can also point to a different shell if needed.
Bash scripts can run in other shells, however it may not if the script is bash specific. After writing a bash script it must be modified so it can be executed . This can be used using the change mod command “chmod” To edit or change scripts via Linux GUI, you can edit with kwrite in KDE a in Gnome Gedit
Commands
Shell scripts run commands that interact with shell, most of the commands are going to be external, these are programs that are located in /bin/ or /usr/bin directory or some other directory on your file system. These
programs are also going to use internal programs, these can be used by running by using their names in the script, programmed parameters can
also be added.
Example
If you wanted to open 2 exterm windows and run kmail you might use
/usr/bin/xterm &
/usr/bin/xterm &
& specifies run the xterm in a new window and the 2nd line specifies to run
it again.
/usr/bin/kmail &
Specifies to run kmail in its own window.
So the full script will looks like this
#!/bin/bash
/usr/bin/xterm &
/usr/bin/xterm &
/usr/bin/kmail &
The & also tells the script that it can go on and continue to execute programs without waiting for the results of the previous program. You may choose not to use the & so that when a program like xterm runs then you would have to close the xterm window before the script continues to run the rest of the scripts If the script issues a error then you will have to trouble-shoot Typically & is not going to be used at the end of every command, because a lot of the time the script is waiting for one command to finish and taking the output of that script as input for the next command, in that case you leave the & out.
Common commands used in Scripts
ls
mv
cp
rm
All these commands can be used for repetitive tasks, like:
Backups
Moving logs
Consolidating files
Deleting files
Another command we find often in scripts is grep which we use to search for things, we can use it to search for certain strings, to display the lines of strings into a file or the screen. We can also use the find command, while grep is going to search for patterns in the contents of files find will look for certain files based on file names, ownerships and other permissions at the file system level, so if you are looking for particular files find can be useful so scripts will use that alot.
Another command that is used is cut. Cut will extract text from a field within a file, so it can pull information from a file and then copy it to other places echo is heavily used in scripts as it can display a message to the user in your command-line terminal, for example you could echo echo “please wait till I start up the computer” then I might run a bunch of
commands after that. Any command can be used in a script.
Arguments & Variables
To really expand the utility of a script we use variables and arguments. Variables are used as a placeholder, such as <filename>, this will be determined when the script runs, the values of variables can be passed to a script Generated internally to a script or extracted from a scripts environment.
An Environment is just a set of variables, such as the current directory, the search path for running programs or anything like that which a program can access. Arguments are variables that are passed to the script, these are also called parameters. These are represented in a script as a dollar sign followed by a number from 0 upwards.
$0 stands for the name of the script
$1 is the first parameter to the script
$2 is the 2nd parameter to the script
These parameters when you run your script act just like the arguments we passed to other commands as we were using the commands before. For example when we use ls <filename> that file name is really a variable and that’s a parameter in that case, this is the same idea with scripts.
Example Script
Useradd -m $1
passwd $1
mkdir -p /shared/$1
chown $1.users /shared/$1
chmod 775 /shared/$1
In -s /shared/1$ /home/$1 /shared
chown $1.users /home/$1/shared
Notice where $1 sign is in the script several times, so whatever parameter is given to the script is going to be used and inputted wherever you see $1
The script example below creates the following
#mkuser rwelsh
This script will work silently unless input is needed by the user
The dollar sign gets replaced by variable
#! /bin/bash
useradd -m $1 – rwelsh creates a user account name
passwd $1xyz1234 this will change the account password
mkdir -p /shared/$1rwelsh – will create a directory in the /shared directory tree
chown $1rwelsh. users /shared/$1rwelsh
chmod 775 /shared/rwelsh $1 – this will set the read write permissions
In -s /shared/1$rwelsh /home/$1rwelsh /shared sets a symbolic link
chown $1rwelsh. users /home/$1rwelsh /shared – this will set the ownership to the folder
Variables
In this example we will use variables in a script which is for pinging IP addresses.
#!/bin/bash
ip=`route -n | grep UG | tr -s “ “ | cut -f 2 -d “ “`
$ping=”/bin/ping”
echo “checking to see ifFFm