|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
Quote:
![]() In other words, yourscript filename should work, but yourscript will fail with errors similar to what you described above. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|