File Name needs to display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Name needs to display
# 1  
Old 03-21-2012
File Name needs to display

I am new to shell scripting plz help me to get solution for below requirement -

If name.sh file is present, message will be displayed as “name.sh is present” else "name.sh is not present"

I have tested below script but getting error.

Code:
#!/bin/ksh
file_found=`ls name.sh`
found=`echo $?`
if $found -eq 0
then
echo "$file_found is present"
else
echo "name.sh is not present"
fi

Error -

Code:
bash-2.05$ ./filename.sh
./filename.sh[4]: 0: cannot execute
name.sh is not present
bash-2.05$

Thankx,
Rahul
# 2  
Old 03-21-2012
Code:
 
ls name.sh > /dev/null 2>&1 && echo "name.sh is present" || echo "name.sh not present"

# 3  
Old 03-21-2012
Hi


Code:
#!/bin/ksh
file_found=`ls name.sh 2> /dev/null`
found=`echo $?`
if [ $found -eq 0 ]
then
echo "$file_found is present"
else
echo "name.sh is not present"
fi

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 03-21-2012
Thankx!!

Code is working fine!!

could you plz help me to understand what 2> /dev/null does

Thankx,
Rahul.
# 5  
Old 03-21-2012
Hi
without that part, when your file does not exist, along with the intended echo message, you will get one more system error message for file not present

Code:
ls: name.sh: No such file or directory

we try to re-direct the error to a terminal. just do a small test by removing it, it will clarify more.

Guru.
# 6  
Old 03-21-2012
got it -

Thank you so much!!
# 7  
Old 03-21-2012
The Shell contains a simple test for the presence of a file. There is no need to run "ls".
Code:
#!/bin/ksh
filename="name.sh"
if [ -f "$[filename}" ]
then
       echo "${filename} is present"
else
       echo "${filename} is not present"
fi

This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

2. Homework & Coursework Questions

adding rows of a file and display in another file

How to add these all ( 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25) in the script and How to calculate the GPA?? :wall: For example a course record file can be like this: COURSE NAME: Operating Systems CREDITS: 4 123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25 243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15... (1 Reply)
Discussion started by: poonam29
1 Replies

3. Shell Programming and Scripting

adding rows of a file and display in another file

How to add these all ( 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25) in the script and How to calculate the GPA?? :wall: For example a course record file can be like this: COURSE NAME: Operating Systems CREDITS: 4 123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25 243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15... (1 Reply)
Discussion started by: poonam29
1 Replies

4. Homework & Coursework Questions

Display file name

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have a directory called "Month" which has 12 files named on months. /home/months> touch Jan Feb Mar Apr May... (6 Replies)
Discussion started by: manishdivs
6 Replies

5. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. Programming

Display file

I am making a program in c. I want the progrm to be able to display file names ( like in unix we do long lisiting) what should i do? Any help will be appreciated (3 Replies)
Discussion started by: programlover
3 Replies

8. Shell Programming and Scripting

Display File

I have a file xx like this abc abcd abc abcd abcd efgh ijkl mnop qrst uv wxyz Now I want to search for "abcd". After the last found, it will display the remaining of the file. So here I am expecting something like abcd efgh ijkl mnop qrst uv wxyz (5 Replies)
Discussion started by: joy_deep
5 Replies

9. UNIX for Dummies Questions & Answers

Display File

I have a file xx like this abc abcd abc abcd abcd efgh ijkl mnop qrst uv wxyz Now I want to search for "abcd". After the last found, it will display the remaining of the file. So here I am expecting something like abcd efgh ijkl mnop qrst uv wxyz (2 Replies)
Discussion started by: joy_deep
2 Replies
Login or Register to Ask a Question