How do display a warning message?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do display a warning message?
# 1  
Old 03-24-2010
How to 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!

Last edited by kris2010; 03-24-2010 at 09:09 PM..
# 2  
Old 03-24-2010
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:
Code:
/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:
Code:
cd /home/me

- Relative path is only available if the current directory is the immediate child of the /home/me directory:
Code:
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.

Last edited by dunkar70; 03-24-2010 at 10:42 PM.. Reason: Adding clarity
# 3  
Old 03-25-2010
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.
# 4  
Old 03-25-2010
Quote:
Originally Posted by kris2010
Also, for word count "wc" is there a way to use pipe to count the words in your working directory?
Code:
wc -w * 2>/dev/null | tail -1

# 5  
Old 03-25-2010
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:
Code:
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:
Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function to suppress warning message for specific text and display prompt

In the below bash function multiple variants are input and stored in a variable $variant, and each is written to an out file at c:/Users/cmccabe/Desktop/Python27/out.txt stored on a separate line. # enter variant phox2b() { printf "\n\n" printf "What is the id of the patient getting... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Search for a tag and display a message if not found.

Hi All, I am working with a XML file. Below is part for the file. <Emp:Profile> <Emp:Description>Admin</Emp:Description> <Emp:Id>12347</Emp:Id> </Emp:Profile> <Emp:Profile> ... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Solaris

Display Message Question

I'm have a script that I am creating and I want the dmesg command to only show output for the current day and the day before. What would be the command to make this work? Thanks (8 Replies)
Discussion started by: MattyJ2009
8 Replies

4. Shell Programming and Scripting

Refrain the Message of File Not exists display out

Hi All, Would like to ask on how to refrain the message file not exists from display out. if ; then When it execute, the OS will throw the error file does not exists ls: 0653-341 The file COL_*/*.CTL does not exist. Thanks. (3 Replies)
Discussion started by: ckwan
3 Replies

5. Shell Programming and Scripting

How to display a message if program hangs(takes too long)

I have a ksh script (script1) that calls another ksh script (script2). If script2.ksh hangs or takes too long to execute I want script1.ksh to kill the call to script2.ksh and instead just display "Script2 can't run right now". Could someone help me with coding this? (1 Reply)
Discussion started by: mrskittles99
1 Replies

6. Shell Programming and Scripting

Display a message if the server is prompting

Hello i have to perform a sftp from server "A" to server "B"(remote server). when i execute the sftp command it prompts for password. right now we haven't establish the ssh key exchange so we have to dispaly a error message if it prompts for password. how can i perform it please help (0 Replies)
Discussion started by: urfrnddpk
0 Replies

7. Shell Programming and Scripting

How to grep for message and if found display filename?

Hi i'm new to the forum and was hoping someone could help me with the following query. I do alot of testing and have hundreds of log files output. I have a script (someone else wrote) which finds all the passed and failed logs and puts a number in a column onto a webpage: e.g: Pass ... (4 Replies)
Discussion started by: defamer
4 Replies

8. UNIX for Dummies Questions & Answers

Display message on screen and flat file at same time

Hi guys, I have a script that call another, the other displays de message and I can print directly to the flat file, but in one command I am searchig that this message can be displayed in the screen and in the flat file in one command. I am doing something like this: var=$(./Example.sh)... (2 Replies)
Discussion started by: pipoca
2 Replies

9. UNIX Desktop Questions & Answers

Script that will display a short message

Can anyone point me to the right direction on how to write a simple script that will display a message on any terminal when implemented? Basically I need it so the script runs at a certain time, say April 30, 2010 and that the message will be displayed to me no matter which terminal I am logged... (2 Replies)
Discussion started by: jmack123
2 Replies

10. Shell Programming and Scripting

How to display message when starting a terminal

Hello all, I would like a message to be displayed on the shell when someone opens up the terminal - something like a welcome msg with date and time. I know how to do this by running the shell commands but dont know how to display it when a user opens up the terminal? Thanks in advance (27 Replies)
Discussion started by: mrudula009
27 Replies
Login or Register to Ask a Question