Problem determining file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem determining file
# 1  
Old 10-01-2008
Question Problem determining file

I got the following code, it partially works. Can someone tell me why it partially doenst work?
Code:
#!/bin/sh
file=$1

if [ `ls -l $file | cut -c1` = 'd' ]
then
    echo "File is a directory"
else
    echo "File is not a directory!"
fi

heres the output:
Code:
philip@philip-laptop:~/Desktop$ sh exFive.sh test.java
File is not a directory!
philip@philip-laptop:~/Desktop$ sh exFive.sh ndiswrapper-1.51/
[: 9: -: unexpected operator
File is not a directory!

# 2  
Old 10-01-2008
ok found problem, it was displaying all the contents of the directory, so i put a -d in the command
# 3  
Old 10-01-2008
Ok, nvm^^
# 4  
Old 10-01-2008
But a new problem arose, how do i deal with numbers in Bourne Shell (sh)?

numFiles=ls $file | wc -l
echo "Number of files is $numFiles"

The above doesnt work, always displays 0 even though when I test just using
ls . | wc -l

it shows 26
# 5  
Old 10-01-2008
Code:
if [ -f /path/to/file ]		# be sure the file exists
then
   echo "It is a file and exists!"
fi

Code:
if [ -d /path/to/file ]		# be sure the file exists
then
   echo "It is a directory and exists!"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Determining Values for NIce and Priority items in limits.conf file

I've been looking online trying to find the correct value nice and priority can take in the limits.conf file. ON the man page it says; Does this mean priority can be any negative number and any positive? Then Does this mean any number between -20 and 19 also what does the definition of nice... (13 Replies)
Discussion started by: matthewfs
13 Replies

2. Shell Programming and Scripting

Efficient method of determining if a string is in a file.

Hi, I was hoping someone could suggest an alternative to code I currently have as mine takes up far too much processor time and it to slow. The situation: I have a programme that runs on some files just before they are zipped up and archived, the program appends a one line summary of the... (4 Replies)
Discussion started by: RECrerar
4 Replies

3. UNIX for Dummies Questions & Answers

Determining file size for a list of files with paths

Hello, I have a flat file with a list of files with the path to the file and I am attempting to calculate the filesize for each one; however xargs isn't playing nicely and I am sure there is probably a better way of doing this. What I envisioned is this: cat filename|xargs -i ls -l {} |awk... (4 Replies)
Discussion started by: joe8mofo
4 Replies

4. UNIX for Advanced & Expert Users

Determining typing latency

Hi all, When I use an editor (vi) that is spawned in a remote server, visually I could see the latency between typing a character/word and being displayed on the terminal. I could see this visually but how do I get a metric on this or how to quantify this? As expected, when I type in a editor... (6 Replies)
Discussion started by: matrixmadhan
6 Replies

5. Programming

Problem in determining runlevel through a C program

Hi, I am trying with the following code to retrieve the runlevel of my Linux Ubuntu 8.04 system by reading the "utmp" database. But I am getting blank output. May I know what correction I should do inorder to make this program to work? #include <stdio.h> #include <stdlib.h> #include... (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Programming

determining the IP of a function

Is there a way to determine the "Instruction Pointer" of a function in c++, and if so can someone tell me? (5 Replies)
Discussion started by: neur0n
5 Replies

7. Programming

Determining file access perms for current process

Stupid question, but is there an ANSI C stdlib function that will do this for me? I want to pass the function a path and determine if the current process can read/write/execute on the path. I suppose I can whip something up using fstat and then determining the current process's user/group IDs and... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

8. Shell Programming and Scripting

Determining position in a tab delimited file

hi, I want to determine the position of specific values over a cutoff. So I have a string of values that are mainly negative in number and I want to print the rare few that are positive. Specifically I want to know the position of the value along the string. The position is based from right to... (11 Replies)
Discussion started by: phil_heath
11 Replies

9. UNIX for Dummies Questions & Answers

Determining type of file

Hello, I'm attempting to modify a script so it can be executed via a batch scheduler. Part of the script calls a program called direct (which I believe may have something to do with Connect Direct). I have tried cat and vi on the file; cat returns absolute gibberish, vi states the file is... (2 Replies)
Discussion started by: JWilliams
2 Replies

10. UNIX for Dummies Questions & Answers

Determining file length

How can I determine what UNIX thinks the record size of any given file is?? (1 Reply)
Discussion started by: jbrubaker
1 Replies
Login or Register to Ask a Question