Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Linux Tutorial

· Shells and Utilities
· The Shell
· The Search Path
· Directory Paths
· Shell Variables
· Permissions
· Regular Expressions and Metacharacters
· Quotes
· Pipes and Redirection
· Interpreting the Command
· Different Kinds of Shells
· Command Line Editing
· Functions
· Job Control
· Aliases
· A Few More Constructs
· The C-Shell
· Commonly Used Utilities
· Looking for Files
· Looking Through Files
· Basic Shell Scripting
· Managing Scripts
· Shell Odds and Ends

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Recommend Us
Surveys

Features
HOWTOs
News
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 225 guest(s) and 3 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  
Linux Tutorial - Shells and Utilities - Shell Odds and Ends
  Managing Scripts ---- Editing Files  


Odds and Ends

This section includes a few tidbits that I wasn't sure where to put.

You can get the shell to help you debug your script. If you place set -x in your script, each command with its corresponding arguments is printed as it is executed. If you want to just show a section of your script, include the set -x before that section, then another set +x at the end. The set +x turns off the output.

If you want, you can capture output into another file, without having it go to the screen. This is done using the fact that output generated as a result of the set -x is going to stderr and not stdout. If you redirect stdout somewhere, the output from set -x still goes to the screen. On the other hand, if you redirect stderr, stdout still goes to your screen. To redirect sterr to a file start, the script like this:

mscript 2>/tmp/output

This says to send file descriptor 2 (stderr) to the file /tmp/output.

To create a directory that is several levels deep, you do not have to change directories to the parent and then run mkdir from there. The mkdir command takes as an argument the path name of the directory you want to create. It doesn't matter if it is a subdirectory, relative path, or absolute path. The system will do that for you. Also, if you want to create several levels of directories, you don't have to make each parent directory before you make the subdirectories. Instead, you can use the -p option to mkdir, which will automatically create all the necessary directories.

For example, imagine that we want to create the subdirectory ./letters/personal/john, but the subdirectory letters does not exist yet. This also means that the subdirectory personal doesn't exist, either. If we run mkdir like this:

then the system will create ./letters, then ./letters/personal, and then ./letters/personal/john.

Assume that you want to remove a file that has multiple links; for example, assume that ls, lc, lx, lf, etc., are links to the same file. The system keeps track of how many names reference the file through the link count (more on this concept later). Such links are called hard links. If you remove one of them, the file still exists as there are other names that reference it. Only when we remove the last link (and with that, the link count goes to zero) will the file be removed.

There is also the issue of symbolic links. A symbolic link (also called a soft link) is nothing more than a path name that points to some other file, or even to some directory. It is not until the link is accessed that the path is translated into the "real" file. This has some interesting effects. For example, if we create a link like this

you would see the symbolic link as something like this:

drw-r--r-- 1 jimmo support 29 Sep 15 10:06 letter.john-> /home/jimmo/letter.john

Then,the file /home/jimmo/text/letter.john is a symbolic link to /home/jimmo/letter.john. Note that the link count on /home/jimmo/letter.john doesn't change, because the system sees these as two separate files. It is easier to think of the file /home/jimmo/text/letter.john as a text file that contains the path to /home/jimmo/letter.john. If we remove /home/jimmo/letter.john, /home/jimmo/text/letter.john will still exist. However, it will point to something that doesn't exist. Even if there are other hard links that point to the same file like /home/jimmo/letter.john, that doesn't matter. The symbolic link, /home/jimmo/text/letter.john, points to the path /home/jimmo/letter.john. Because the path no longer exists, the file can no longer be accessed via the symbolic link. It is also possible for you to create a symbolic link to a file that does not exist, as the system does not check until you access the file.

Another important aspect is that symbolic links can extend across file systems. A regular or hard link is nothing more than a different name for the same physical file and used the same inode number. Therefore it must be on the same filesystem. Symbolic links contain a path, so the destination can be on another filesystem (and in some cases on another machine). For more on inodes, see the section on filesystems.

The file command can be used to tell you the type of file. With DOS and Windows, it's fairly obvious by looking at the file's extension to determine the files type. For example, files ending in .exe are executables (programs), files ending in .txt are text files, and files ending in .doc are documents (usually from some word processor). However, a program in UNIX can just as easily have the ending .doc or .exe, or no ending at all.

The file command uses the file /etc/magic to make an assumption about the contents of a file. The file command reads the header (first part of the file) and uses the information in /etc/magic to make its guess. Executables of a specific type (a.out, ELF) all have the same basic format, so file can easily recognize them. However, there are certain similarities between C source code, shell scripts, and even text files that could confuse file.

For a list of some of the more commonly used commands, take a look here.

 Previous Page
Managing Scripts
  Back to Top
Table of Contents
Next Page 
Editing Files


MoreInfo

Test Your Knowledge

User Comments:


You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.
  




Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
The Linux Tutorial can use your help.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.13 Seconds