How do display a warning message?

Hello,

I am teaching myself shell scripting and I was wondering if there was a way to rename a file and display a warning or prompt message? And if you had a file like
/home/me/blah/ for example, what are the ways to use the CD to get to /me?
Would it be ../home/me? Are there other ways to using CD to move to different directories?

Thanks!

First, UNIX is case sensitive so the command is cd, not CD. You can type "man cd" to get the manual page for the command. Second, the decision to include a slash in your path is important. Starting a path with it denotes the root as the starting point and it denotes a full path. Ending the path with it denotes a directory, which is assumed when using the cd command. Trying to use the cd command with a file generate an error.

In short, cd can use relative paths or full paths.

Starting from the directory you listed:

/home/me/blah

you can type either of two commands to get to the "me" directory:

  • Full path is always available regardless of the current working directory:
cd /home/me
  • Relative path is only available if the current directory is the immediate child of the /home/me directory:
cd ../me

Both forms of the command have advantages and disadvantages. The decision to use one over the other depends on 1) if you know the full path, 2) if the script must be portable within a set of files and directories, and 3) if it makes sense.

Error/warning messages will automatically be displayed if you 1) try to cd into a file or 2) into a directory that does not exist.

Thanks for your help.

I was looking for a way to actually have a customizable message to popup if a error occurs. Also, for word count "wc" is there a way to use pipe to count the words in your working directory?

like ls | wc -c ?

It doesn't seem to work out as well as I would like.

wc -w * 2>/dev/null | tail -1

To create a custom error message, you may need to test for the error situation and catch it before the system does. Someone else may have a trick to catch system errors and redirect the output. For example,

You can wrap the test in a function (see example 1 below) within an existing script or you can create a separate script (see example 2 below). In the latter case, you can call the script (i.e. mycd) instead of the cd function. Don't forget to set the execute bit on the file.

Example 1. Function:

function checkDirectory()
{
_directory=$1
if [ -d $_directory ]; then
  cd $_directory
else
  echo "The directory you entered (${_directory}) does not exist."
  echo "Please check your directory and try again."
fi
}

Example 2. Separate Script:

#!/bin/bash
# Script Name: mycd
_directory=$1
if [ -d $_directory ]; then
  cd $_directory
else
  echo "The directory you entered (${_directory}) does not exist."
  echo "Please check your directory and try again."
fi

As for the word count, perhaps if you explain the problem (what do you need to see) rather than the solution (that is not working) we can provide a better way. Word counts of directory listings seems strange to me, but there may be a valid reason.