LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2010, 01:00 AM   #1
etika
Member
 
Registered: Dec 2010
Posts: 38

Rep: Reputation: 0
Question shell script


i have to write a shell script that takes name of the file as command line argument and the script should use path envt variable to look for the specified file and specify where are the different instance of the filename specified exist

firstly enter the filename
echo "enter the filename"
read filename
by using ls command we can get to know how many links are there for that file

but i don't know how to get the pathname of the file and its links

after knowing the pathnames of the file i can set the path variable using export command

can any one tell me how to get the pathname of the file i entered as a command line argument

Last edited by etika; 12-21-2010 at 01:23 AM.
 
Old 12-21-2010, 01:26 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

LQ users are not very eager to do your homework for you. You should put in some work of your own, post what you already have and specify where it's failing. Remember that your teacher might be a member here too. That being said, look into the commands dirname and basename and what they do. Some reading:
Bash Guide for Beginners
Advanced Bash-Scripting Guide

Kind regards,

Eric
 
1 members found this post helpful.
Old 12-21-2010, 03:34 AM   #3
etika
Member
 
Registered: Dec 2010
Posts: 38

Original Poster
Rep: Reputation: 0
This is not my homework for your information
 
Old 12-21-2010, 05:17 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So we will take your word (this time) that it is not homework. So show us what you have done and where you are stuck?
 
Old 12-21-2010, 07:14 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
(1)Split the path into various parts using : as the delimiter.
(2) For each, if given file name exists, then print

OK
 
Old 12-21-2010, 12:36 PM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by etika View Post
This is not my homework for your information
Hi,

If it's not your homework then I apologize sincerely. The structure of your post made me think it was. As pointed out by grail, show us what you have and where you encounter problems or errors and we'll be glad to help you out.

Kind regards,

Eric
 
Old 12-21-2010, 01:37 PM   #7
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by etika View Post
This is not my homework for your information
For the benefit of EricTRA, all of this poster's posts look like homework; if you made a mistake in assuming that this post is homework, it is a mistake that I would have made, too. Maybe, if the OP were to explain in which non-homework context this question could arise, that would help.

Additionally, the poster has been asked before to show what he has done so far and explain where he is stuck, and that hasn't yet happened.

The original statement is also confusing:

Quote:
i have to write a shell script that takes name of the file as command line argument
that part seems clear

Quote:
and the script should use path envt variable to look for the specified file
So, you are only interested in instances that are files and that have exactly the filename specified as a parameter on the invocation of your script. You are not interested in directories or links and you are not interested in files of that name that may exist anywhere else in the filesystem and behaviour is undefined when you find zero files which have this filename. Is this correct?

Quote:
by using ls command we can get to know how many links are there for that file

but i don't know how to get the pathname of the file and its links
Now links have crept in and I'm not sure that I have understood what you mean. Do you expect there to be links of the same name as the file, or do you mean that there could be links pointing to the named file? You could mean either, but neither seem to relate to the problem stated in the first part of the question. And both seem a bit odd when considered in the light of plausible interpretations of the last part, which doesn't seem, superficially, to be about links.

Quote:
after knowing the pathnames of the file i can set the path variable using export command
So, after having found a file (or a filesystem object) that you know is in the path, you are going to set the path so that it includes the file that you have just found. This seems to have no purpose unless you are deleting something from the path, as you know that the file is already in the path.
 
Old 12-21-2010, 04:32 PM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Am I being silly or is this just duplicating the functionality of "whereis"?
 
Old 12-21-2010, 11:16 PM   #9
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Snark1994 View Post
Am I being silly or is this just duplicating the functionality of "whereis"?
It is not a silly comment, in any case but I have a strong suspicion that 'use whereis' will not be an acceptable answer to the OP. We'll see, maybe.
 
Old 12-22-2010, 03:45 AM   #10
etika
Member
 
Registered: Dec 2010
Posts: 38

Original Poster
Rep: Reputation: 0
this was not my homework but was a question in previous year exam of my college of linux and i wanted to solve it thats it and the question was

write a shell script that takes as a command line parameter the name of a file.The script should use the PATH envt variable to look for the specified file and specify where are the different instance of the filename specified exist....


its ok with me i will not post anything from now on if this is not allowed
 
Old 12-22-2010, 05:40 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well you can probably understand our skepticism then if it sounds exactly like homework then (seeing as exams are just the next evolution of homework).

The fact still remains that you have not shown any interest in showing us what you have so far and just like homework, no one here is willing to do all the work for you.

So again I will ask (which by the way you never replied to), what have you done so far and where are you stuck?
If you went to a college to study linux then this seems like a fairly trivial question.
 
Old 12-22-2010, 11:23 AM   #12
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
whereis is for special files. I thought first `which`. But ithe OP could be really need where-all-in-the-path does the exe occurs?
Quote:
and the script should use path envt variable to look for the specified file and specify where are the different instance of the filename specified exist
and
 
Old 12-22-2010, 11:42 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by AnanthaP View Post
whereis is for special files. I thought first `which`. But ithe OP could be really need where-all-in-the-path does the exe occurs? and


And?
 
Old 12-22-2010, 07:08 PM   #14
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
echo $PATH | awk -F: -v myprog=$1 '{for (i=1;i<=NF;i++) print "find " $myprog " " $i " -print"}'

would split the path into various entries ready to be searched.

I had mentioned it earlier (#5).
 
Old 12-22-2010, 07:47 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by AnanthaP View Post
echo $PATH | awk -F: -v myprog=$1 '{for (i=1;i<=NF;i++) print "find " $myprog " " $i " -print"}'

would split the path into various entries ready to be searched.

I had mentioned it earlier (#5).

Almost ...
Code:
echo $PATH | awk -F: -v myprog=$1 '{for (i=1;i<=NF;i++) print "find "$i" -name "myprog" -print"}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration