Sponsored Content
Operating Systems Linux How to create file on Linux using environment variable in the dirpath Post 302251662 by patricia1of5 on Monday 27th of October 2008 04:43:46 PM
Old 10-27-2008
How to create file on Linux using environment variable in the dirpath

Hi all,

I am running a Java program on a Linux server in which I read in a base directory path from the *.properties file. During processing, I build a unique file name and create a file to save data, concatenating the directory path and the file name. Works fine, except that I now need to modify this code to accept an environment variable as part of the directory path (previously it was uniquely specified). I've experimented with various ways of quoting - no luck. I'm sure there's a simple syntax to permit this, but so far I've been unable to find it.

File creation statement looks like this:

File uploadFile = new File(baseDir + newFileName);

where baseDir has been read in from the properties file and is equal to

$MYFILEDIR/reports/

The file creation is failing.

Can anyone tell me what syntax can I use in the input that would allow me to create the file in the expected directory? Thanks so much.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

create a new environment variable??

Hi, I have say two programs File1: echo Enter A's Value read A export A File2: while do echo $A sleep 5 done (1 Reply)
Discussion started by: pbsrinivas
1 Replies

2. IP Networking

regarding tracking file updates on linux networking environment

could anyone Explain me with how the linux o/s keeps track of file updates in network environment? for example, assume that more than one computer is connected to network (with linux in all of them). if an user say 'X' logged into terminal 1 and created a file with the name of 'temp.txt'. The... (1 Reply)
Discussion started by: pjchoudary
1 Replies

3. Filesystems, Disks and Memory

regarding tracking file updates on linux networking environment

could anyone Explain me with how the linux o/s keeps track of file updates in network environment? for example, assume that more than one computer is connected to network (with linux in all of them). if an user say 'X' logged into terminal 1 and created a file with the name of 'temp.txt'. The... (1 Reply)
Discussion started by: pjchoudary
1 Replies

4. Shell Programming and Scripting

Setting Environment variable from value in file

I've searched Google and now this forum. Best guess is my search fu is not good (and it probably isn't). The Google search did bring me here. Background I have a number of Korn Shell scripts who all use one of 3 values for an environment variable used in the backup system. On occasion one or... (8 Replies)
Discussion started by: WolfBrother
8 Replies

5. Shell Programming and Scripting

Set Environment variable from another file

Hi, i have the following env variable. currently i am exporting variable in the same script file. but i need this is in a text file and the scripts need to export this variable from the text file. can you please suggest me. is it possible. export... (6 Replies)
Discussion started by: rsivasan
6 Replies

6. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

7. UNIX for Dummies Questions & Answers

setting a environment variable on linux

I want to set a enviroment variable VDC_DIR to a particular directory. I am doing it as export VDC_DIR=/abc it gets set but when i logout and do relogin than its not there. one way could be setting it in .profile file. but i have seen it on another box where it is not present in... (2 Replies)
Discussion started by: Jcpratap
2 Replies

8. Shell Programming and Scripting

Resolving the environment variable from a file

Hi, I an having some environment variables exported and the variable name is present in the file. But its not resolving with following commands. Can someone throw some light. db $ grep -v "^#" TD_FWK_NUCLEUS.dbc | grep -v "^$" | xargs -i echo {} wb_bin: ${TD_FWK_NUCLEUS_DBC_WB_BIN_TAG} I... (3 Replies)
Discussion started by: Mohammed Rafi
3 Replies

9. Shell Programming and Scripting

How to change Linux Terminal environment variable in a perl or bash script?

Hi, I meet an problem that it cannot change Terminal environment variable in a perl or bash script. This change can only exist and become effective in script lifetime. But I want to make this change take effect in current opened Terminal. In our view, the thought seems to be impossible, As... (9 Replies)
Discussion started by: weichanghe2000
9 Replies

10. UNIX for Dummies Questions & Answers

Environment variable substitution in Linux make

I know that I can do this in bash ver=${VERSION:-$DEFVERSION} so ver is $VERSION if it's set but $DEFVERSION if $VERSION isn't set I want to do the same thing as a macro in a Makefile and can't get it to work - maybe something like... VER=$(shell ${$(VERSION):-$(DEFVERSION)}) Any help... (1 Reply)
Discussion started by: JerryHone
1 Replies
File::Spec::Unix(3)					User Contributed Perl Documentation				       File::Spec::Unix(3)

NAME
File::Spec::Unix - File::Spec for Unix, base for other File::Spec modules SYNOPSIS
require File::Spec::Unix; # Done automatically by File::Spec DESCRIPTION
Methods for manipulating file specifications. Other File::Spec modules, such as File::Spec::Mac, inherit from File::Spec::Unix and override specific methods. METHODS
canonpath() No physical check on the filesystem, but a logical cleanup of a path. On UNIX eliminates successive slashes and successive "/.". $cpath = File::Spec->canonpath( $path ) ; Note that this does *not* collapse x/../y sections into y. This is by design. If /foo on your system is a symlink to /bar/baz, then /foo/../quux is actually /bar/quux, not /quux as a naive ../-removal would give you. If you want to do this kind of processing, you probably want "Cwd"'s "realpath()" function to actually traverse the filesystem cleaning up paths like this. catdir() Concatenate two or more directory names to form a complete path ending with a directory. But remove the trailing slash from the resulting string, because it doesn't look good, isn't necessary and confuses OS2. Of course, if this is the root directory, don't cut off the trailing slash :-) catfile Concatenate one or more directory names and a filename to form a complete path ending with a filename curdir Returns a string representation of the current directory. "." on UNIX. devnull Returns a string representation of the null device. "/dev/null" on UNIX. rootdir Returns a string representation of the root directory. "/" on UNIX. tmpdir Returns a string representation of the first writable directory from the following list or the current directory if none from the list are writable: $ENV{TMPDIR} /tmp If running under taint mode, and if $ENV{TMPDIR} is tainted, it is not used. updir Returns a string representation of the parent directory. ".." on UNIX. no_upwards Given a list of file names, strip out those that refer to a parent directory. (Does not strip symlinks, only '.', '..', and equivalents.) case_tolerant Returns a true or false value indicating, respectively, that alphabetic is not or is significant when comparing file specifications. file_name_is_absolute Takes as argument a path and returns true if it is an absolute path. This does not consult the local filesystem on Unix, Win32, OS/2 or Mac OS (Classic). It does consult the working environment for VMS (see "file_name_is_absolute" in File::Spec::VMS). path Takes no argument, returns the environment variable PATH as an array. join join is the same as catfile. splitpath ($volume,$directories,$file) = File::Spec->splitpath( $path ); ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file ); Splits a path into volume, directory, and filename portions. On systems with no concept of volume, returns '' for volume. For systems with no syntax differentiating filenames from directories, assumes that the last file is a path unless $no_file is true or a trailing separator or /. or /.. is present. On Unix this means that $no_file true makes this return ( '', $path, '' ). The directory portion may or may not be returned with a trailing '/'. The results can be passed to "catpath()" to get back a path equivalent to (usually identical to) the original path. splitdir The opposite of "catdir()". @dirs = File::Spec->splitdir( $directories ); $directories must be only the directory portion of the path on systems that have the concept of a volume or that have path syntax that differentiates files from directories. Unlike just splitting the directories on the separator, empty directory names ('') can be returned, because these are significant on some OSs. On Unix, File::Spec->splitdir( "/a/b//c/" ); Yields: ( '', 'a', 'b', '', 'c', '' ) catpath() Takes volume, directory and file portions and returns an entire path. Under Unix, $volume is ignored, and directory and file are concatenated. A '/' is inserted if needed (though if the directory portion doesn't start with '/' it is not added). On other OSs, $volume is significant. abs2rel Takes a destination path and an optional base path returns a relative path from the base path to the destination path: $rel_path = File::Spec->abs2rel( $path ) ; $rel_path = File::Spec->abs2rel( $path, $base ) ; If $base is not present or '', then cwd() is used. If $base is relative, then it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to cwd(). On systems that have a grammar that indicates filenames, this ignores the $base filename. Otherwise all path components are assumed to be directories. If $path is relative, it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to cwd(). No checks against the filesystem are made, so the result may not be correct if $base contains symbolic links. (Apply Cwd::abs_path() beforehand if that is a concern.) On VMS, there is interaction with the working environment, as logicals and macros are expanded. Based on code written by Shigio Yamaguchi. rel2abs() Converts a relative path to an absolute path. $abs_path = File::Spec->rel2abs( $path ) ; $abs_path = File::Spec->rel2abs( $path, $base ) ; If $base is not present or '', then cwd() is used. If $base is relative, then it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to cwd(). On systems that have a grammar that indicates filenames, this ignores the $base filename. Otherwise all path components are assumed to be directories. If $path is absolute, it is cleaned up and returned using "canonpath()". No checks against the filesystem are made. On VMS, there is interaction with the working environment, as logicals and macros are expanded. Based on code written by Shigio Yamaguchi. COPYRIGHT
Copyright (c) 2004 by the Perl 5 Porters. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Please submit bug reports and patches to perlbug@perl.org. SEE ALSO
File::Spec perl v5.16.3 2013-01-16 File::Spec::Unix(3)
All times are GMT -4. The time now is 09:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy