Script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script question
# 1  
Old 10-01-2008
Script question

I am trying to write a script to check the STIG our unix boxes. I want to put in checks to determine if certain files or directories are there. For example:

If /opt/SUNWexplo exists
echo "Sun Explorer exists"
else
echo "Sun Explorer is not installed"

I am sure I could write something like

ls -ld /opt/SUNWexplo
if $? = 0
echo "Sun Explorer exists"
else
echo "Sun Explorer is not installed".

however I am trying to find a more efficient way to do this. Any help is appreciated. Looking for something that works on both directories or files.

Thanks,
Robert
# 2  
Old 10-01-2008
File:
Code:
if [ -e /path/to/file ]		# be sure the file exists
then
   echo "File EXISTS!"
fi

Directory
Code:
if [ -d /path/to/dir ]		# be sure the directory exists
then
   echo "Directory EXISTS!"
fi

The tests below are test conditions provided by the shell:
Code:
-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files' "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
file1 –nt file2 = True if file1 is newer, by modification date, than file2. 
file1 ot file2 = True if file1 is older than file2. 
file1 ef file2 = True if file1 and file2 have the same device and inode numbers. 
-z string = True if the length of the string is 0. 
-n string = True if the length of the string is non-zero. 
string1 = string2 = True if the strings are equal. 
string1 != string2 = True if the strings are not equal. 
!expr = True if the expr evaluates to false. 
expr1 –a expr2 = True if both expr1 and expr2 are true. 
expr1 –o expr2 = True is either expr1 or expr2 is true.

# 3  
Old 10-01-2008
Thanks,

I knew there had to be some conditional syntax for files and directories. This was very helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C first Script Question

Gents for 2013 I have embarked on learning C and just wrote my first script and need some help. In the learning process, I am trying to understand where I went wrong with my script. The flow of the script is as such: Enter a number --> check if it is a number --> if it is not a number,print what... (4 Replies)
Discussion started by: metallica1973
4 Replies

2. Shell Programming and Scripting

Script question

I know some tricks like this : echo " E"; sleep 0.1; clear; echo " Er"; sleep 0.1; clear; echo " Err"; sleep 0.1; clear; echo " Erro"; sleep 0.1; clear; echo " Error ";... (2 Replies)
Discussion started by: hakermania
2 Replies

3. Shell Programming and Scripting

script question

How can I include a counter in this if statement so only sends out 5 notifications. # the if statement will check for the lines status, if status is down sends email if then LIST="user@email.com" mail -s "rje_lines_down" $LIST < ${tmpfile} #sends an email to list fi thanks... (2 Replies)
Discussion started by: d4n3lu
2 Replies

4. UNIX for Dummies Questions & Answers

Script Question

I have a script that will calculate some information about the current directory that I run the script in. How can I have it where it gets an argument from the user(such as a directory) and the script calculate the information for the given directory? Any help is greatly appreciated. (8 Replies)
Discussion started by: tr32
8 Replies

5. Shell Programming and Scripting

Script Question

Here is my problem. I have a txt file with a list of user names in both upper case and lower case. I need to remove the names from the passwd file. I want to be able to run a script that looks at my txt list and then removes those names from the passwd file. It would be nice if it backed up the... (3 Replies)
Discussion started by: wgartin
3 Replies

6. Shell Programming and Scripting

script question

Anyone know why this won't work? #!/usr/bin/ksh for db in `cat /etc/oratab|egrep ':N|:Y' | grep -v \* | cut -f1 -d":"` do echo "************************" echo "database is $db" echo "************************" done I am getting an error on the line... (7 Replies)
Discussion started by: ace2000
7 Replies

7. UNIX for Dummies Questions & Answers

Script question

Can anyone help with these scripts? Im new to this and struggling. Thank you for your help. Pre-requisites Create a file with x amount of lines in it, the content of your choice. (Have already done this) Script 1 Write a script that takes two arguments. The first being a line of text,... (3 Replies)
Discussion started by: beginner1
3 Replies

8. Shell Programming and Scripting

script question

I'm using solaris and i have an old script that an ex employee wrote but i can't seem to get it working. well is just one part of the script if ($?MC_PROD_DIR == 0) setenv MC_PROD_DIR $HOME/PRODUCTION source $MC_PROD_DIR/scripts/localenv ** i understand what this part of the script is... (1 Reply)
Discussion started by: fusionjd
1 Replies

9. UNIX for Dummies Questions & Answers

script question

Correct me if i'm wrong, but by adding "#!/bin/ksh" at the start of a script will force it to run in the korn shell no matter which shell you are currently using?? (2 Replies)
Discussion started by: shad0w75
2 Replies

10. UNIX for Dummies Questions & Answers

Another script question.

Hi, First off I usually script in the bash shell. Ok, in my script I am checking to see if the filename has a .txt extension. So I was trying: if then echo "Must contain a valid .txt extension" fiandif ] then echo "Must contain a valid .txt extension" fiBut no go the first... (5 Replies)
Discussion started by: Astudent
5 Replies
Login or Register to Ask a Question