[Solved] How to refer to input file in code? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[Solved] How to refer to input file in code?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-14-2012
legato22 legato22 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 2 December 2012, 4:27 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
[Solved] How to refer to input file in code?

This may be a dumb question, but googling is not giving me an answer. I'm trying to figure out how to refer to an input file in my code.

Lets say i run a script in bash:

"sh shellscript.sh inputfile"
(Inputfile will be variable...whatever file i run the script on)

I wanted to make variables like "DIR=dirname inputfile" and "BASE = basename inputfile" so that I can use those to make subsequent files based off of the basename and put them in directories based on the dirname rather than having to cd to the directory to run the script and keep the output files in that folder. The problem is that I don't know how to refer to the inputfile to initially set these variables.

Also, if I set these variables at the beginning of the whole shell script, they will stay throughout for all of the other commands I am performing, right?
Sponsored Links
    #2  
Old 11-14-2012
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Positional parameters is the term you need to look for when searching. They should be documented in your shell's manual page. They are shell variables (aka parameters) of the form $1, $2 ... $N, where N is the number of arguments with which the script was called. You'll also want to learn about the related special variables $* and $@.

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
legato22 (11-14-2012)
Sponsored Links
    #3  
Old 11-14-2012
legato22 legato22 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 2 December 2012, 4:27 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by alister View Post
Positional parameters is the term you need to look for when searching. They should be documented in your shell's manual page. They are shell variables (aka parameters) of the form $1, $2 ... $N, where N is the number of arguments with which the script was called. You'll also want to learn about the related special variables $* and $@.

Regards,
Alister
Thanks! Finally got the right search term. After a little more searching, fyi for posterity, i set the variables with:

DIR=`dirname $1`
INPUT=`basename $1`


I was having problems before too because I didn't realize that there could be no spaces when setting variables!
Works now, thanks again.

EDIT: Not working... sort of pissing me off. I did this the first time, and it returned the dirname and basename of the file the script was running on with $1, but now it won't anymore. dirname is empty and it just returns "usage:dirname path"

EDIT: And it works again...I realized that getting the dirname from $1 or $0 the way I am doing it gets problems if any of the folders in the path have spaces in them because i assume that it takes the space for a new "field"... Might be using improper terminology, but $1 might become $2 for example. My solution was to remove spaces from folder names... Anyone know how to do this otherwise so that there won't be a problem?

Last edited by legato22; 11-14-2012 at 01:44 PM..
    #4  
Old 11-14-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 August 2014, 12:26 AM EDT
Location: San Jose, CA, USA
Posts: 4,480
Thanks: 176
Thanked 1,503 Times in 1,275 Posts
Quote:
Originally Posted by legato22 View Post
Thanks! Finally got the right search term. After a little more searching, fyi for posterity, i set the variables with:

DIR=`dirname $1`
INPUT=`basename $1`


I was having problems before too because I didn't realize that there could be no spaces when setting variables!
Works now, thanks again.

EDIT: Not working... sort of pissing me off. I did this the first time, and it returned the dirname and basename of the file the script was running on with $1, but now it won't anymore. dirname is empty and it just returns "usage:dirname path"

EDIT: And somehow it randomly works again.
As a wild guess, I'd say that the times it didn't work, you invoked your shell script with no operands.

In other words, yourscript filename should work, but yourscript will fail with errors similar to what you described above.
Sponsored Links
    #5  
Old 11-14-2012
legato22 legato22 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 2 December 2012, 4:27 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Don Cragun View Post
As a wild guess, I'd say that the times it didn't work, you invoked your shell script with no operands.

In other words, yourscript filename should work, but yourscript will fail with errors similar to what you described above.
I checked, I did use operands those times. Repeated mistakes when there are spaces in directory paths reveal that to be the problem.
Sponsored Links
    #6  
Old 11-14-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 August 2014, 12:26 AM EDT
Location: San Jose, CA, USA
Posts: 4,480
Thanks: 176
Thanked 1,503 Times in 1,275 Posts
Quote:
Originally Posted by legato22 View Post
I checked, I did use operands those times. Repeated mistakes when there are spaces in directory paths reveal that to be the problem.
OK. So, I assume you've figured out this can be fixed by using appropriate quoting. In this case:

Code:
DIR=`dirname "$1"`
INPUT=`basename "$1"`

or, preferably:

Code:
DIR=$(dirname "$1")
INPUT=$(basenamae "$1")

The Following User Says Thank You to Don Cragun For This Useful Post:
legato22 (11-14-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Hot to retrieve *.sql file names which we refer in .sh file. lakshmanrk811 UNIX for Dummies Questions & Answers 3 04-04-2012 07:30 AM
[Solved] problem - connecting code with external file Telis Shell Programming and Scripting 4 03-15-2012 02:14 PM
12. If an 88 Record with BAI Code 902 was found on input file and not written to Output file, re sgoud UNIX for Dummies Questions & Answers 0 07-06-2011 07:30 AM
Append file based upon user input-- solved DC Slick Shell Programming and Scripting 0 02-16-2011 06:07 AM
Refer a remote file blackeyed UNIX for Dummies Questions & Answers 1 09-25-2009 03:50 AM



All times are GMT -4. The time now is 04:17 AM.