Sponsored Content
Top Forums Shell Programming and Scripting use some part of variable value to replace ../../ values in a file Post 302645795 by rbatte1 on Thursday 24th of May 2012 05:50:52 AM
Old 05-24-2012
I always worry about marking relative references in scripts. If something doesn't exist, a cd will just refuse and unless you carefully manage errors, then you could end up damaging important files. Consider:-
Code:
cd /etc
grep -v bankhost hosts > dr_test_hosts
cd /disaster
rm *
cp -pR /etc/* .

This would (probably in a bad way anyway) remove a sensitive remote host address from /etc/hosts in preparation for a DR test to reduce the risk of actually sending data to the bank. Of course, if you run this and /disaster does not exist then you empty /etc, which could be a little awkward, to say the least.

Could it be a better plan to have a variable made of several others, such as:
Code:
maindir=/dir1/dir2
val=${maindir}/dir3/dir4/file1
...do something...
val=${maindir}/dir3/newdir4/file2
...do something...
val=${maindir}/dir3/bigdir4/file3
...do something...

You would still have to check that these exist, of course, but it reduces the risk of a typing error and one action compounding on another.

Maybe I'm paranoid, but unless you consider every possible action and error, then it's a dangerous game. I've been bitten in the past, so I can't claim perfection, just learning from experience.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies

2. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

3. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

4. Shell Programming and Scripting

Perl command to replace path part of variable

I'm trying to replace path which is part of variable inside script file: FROM: ABC_HOME=$ABC_ROOT/abc/1.0 TO: ABC_HOME=$ABC_ROOT/abc/1.5 I'm using this: perl -pi -e 's\ABC_HOME=$ABC_ROOT/abc/1.0\ABC_HOME=$ABC_ROOT/abc/1.5\g' /apps/scripts/test.sh This command is not working because... (2 Replies)
Discussion started by: djanu
2 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

7. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

8. Shell Programming and Scripting

Replace values using other file

Gents, Please can you help me. I need to update file1 using file2 values file1 S 44519.00 49349.00 1V1 0.0 0 0.0 0.0 0.0 0.0289091513 S 44513.00 48581.00 1V1 0.0 0 0.0 0.0 0.0 0.0289094319 S 44511.00 48605.00 1V1 0.0 0 0.0... (1 Reply)
Discussion started by: jiam912
1 Replies

9. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

10. Shell Programming and Scripting

Replace values on file

Gents, Please i need your help. Using the file2.txt i will like to replace values in file3.txt. Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt). Example file2.txt 21 1209 22 1210file3.txt SCI TB Timestamp Local : 8/30/17... (2 Replies)
Discussion started by: jiam912
2 Replies
rdist(1)						      General Commands Manual							  rdist(1)

Name
       rdist - remote file distribution program

Syntax
       rdist [ -nqbRhivwy ] [ -f distfile ] [ -d var=value ] [ -m host ] [ name ...  ]

       rdist [ -nqbRhivwy ] [ -c name ...  [login@]host[:dest]

Description
       The  program  maintains identical copies of files over multiple hosts.  It preserves the owner, group, mode, and mtime of files if possible
       and can update programs that are executing.  reads commands from distfile to direct the updating of files and/or directories.  If  distfile
       is  `-',  the  standard	input  is used.  If no -f option is present, the program looks first for `distfile', then `Distfile' to use as the
       input.  If no names are specified on the command line, will update all of the files and directories listed  in  distfile.   Otherwise,  the
       argument  is  taken  to	be  the name of a file to be updated or the label of a command to execute. If label and file names conflict, it is
       assumed to be a label.  These may be used together to update specific files using specific commands.

Options
       -c   Forces to interpret the remaining arguments as a small distfile.  The equivalent distfile is as follows.

		 ( name ... ) -> [login@]host
		      install	[dest] ;

       -d   Defines var to have value.	The -d option is used to define or override variable definitions in the distfile.  Value can be the  empty
	    string, one name, or a list of names surrounded by parentheses and separated by tabs and/or spaces.

       -m   Limit  which machines are to be updated. Multiple -m arguments can be given to limit updates to a subset of the hosts listed the dist-
	    file.

       -n   Print the commands without executing them. This option is useful for debugging distfile.

       -q   Quiet mode. Files that are being modified are normally printed on standard output. The -q option suppresses this.

       -R   Remove extraneous files. If a directory is being updated, any files that exist on the remote host that do  not  exist  in  the  master
	    directory are removed.  This is useful for maintaining truly identical copies of directories.

       -h   Follow symbolic links. Copy the file that the link points to rather than the link itself.

       -i   Ignore  unresolved	links.	Rdist will normally try to maintain the link structure of files being transferred and warn the user if all
	    the links cannot be found.

       -v   Verify that the files are up to date on all the hosts. Any files that are out of date will be displayed but no files will  be  changed
	    nor any mail sent.

       -w   Whole  mode.  The  whole  file name is appended to the destination directory name. Normally, only the last component of a name is used
	    when renaming files.  This will preserve the directory structure of the files being copied instead of flattening the directory  struc-
	    ture.  For	example,  renaming  a  list  of files such as ( dir1/f1 dir2/f2 ) to dir3 would create files dir3/dir1/f1 and dir3/dir2/f2
	    instead of dir3/f1 and dir3/f2.

       -y   Younger mode. Files are normally updated if their mtime and size (see disagree. The -y option causes rdist not to  update  files  that
	    are  younger than the master copy.	This can be used to prevent newer copies on other hosts from being replaced.  A warning message is
	    printed for files which are newer than the master copy.

       -b   Binary comparison. Perform a binary comparison and update files if they differ rather than comparing dates and sizes.

       Distfile contains a sequence of entries that specify the files to be copied, the destination hosts, and what operations to  perform  to	do
       the updating. Each entry has one of the following formats.

	    <variable name> `=' <name list>
	    [ label: ] <source list> `->' <destination list> <command list>
	    [ label: ] <source list> `::' <time_stamp file> <command list>

       The  first  format  is  used for defining variables.  The second format is used for distributing files to other hosts.  The third format is
       used for making lists of files that have been changed since some given date.  The source list specifies a list of files and/or  directories
       on the local host which are to be used as the master copy for distribution.  The destination list is the list of hosts to which these files
       are to be copied.  Each file in the source list is added to a list of changes if the file is out of date on the host which is being updated
       (second format) or the file is newer than the time stamp file (third format).

       Labels are optional. They are used to identify a command for partial updates.

       Newlines,  tabs, and blanks are only used as separators and are otherwise ignored. Comments begin with a sharp sign (#) and end with a new-
       line.

       Variables to be expanded begin with dollar sign ($) followed by one character or a name enclosed in curly braces (see the examples  at  the
       end).

       The source list and destination list have the following format:

	    <name>
       or
	    `(' <zero or more names separated by white-space> `)'

       The shell meta-characters [, ], {, }, *, and ?  are recognized and expanded (on the local host only) in the same way as They can be escaped
       with a backslash (.  The tilde character (~) is also expanded in the same way as but is expanded separately on the  local  and  destination
       hosts.	When  the  -w  option is used with a file name that begins with tilde (~), everything except the home directory is appended to the
       destination name.  File names which do not begin with / or ~ use the destination user's home directory as the root directory for  the  rest
       of the file name.

       The command list consists of zero or more commands of the following format.

	    `install'  <options>    opt_dest_name `;'
	    `notify'   <name list>  `;'
	    `except'   <name list>  `;'
	    `except_pat'	    <pattern list>`;'
	    `special'  <name list>  string `;'

       The install command is used to copy out of date files and/or directories.  Each source file is copied to each host in the destination list.
       Directories are recursively copied in the same way.  opt_dest_name is an optional parameter to rename files.  If no install command appears
       in  the	command list or the destination name is not specified, the source file name is used.  Directories in the path name will be created
       if they do not exist on the remote host.  To help prevent disasters, a non-empty directory on a target host will never be replaced  with  a
       regular	file or a symbolic link.  However, under the -R option a non-empty directory will be removed if the corresponding filename is com-
       pletely absent on the master host.  The options are -R, -h, -i, -v, -w, -y, and -b and have the same semantics as options  on  the  command
       line  except  they  only  apply to the files in the source list.  The login name used on the destination host is the same as the local host
       unless the destination name is of the format login@host.

       The notify command is used to mail the list of files updated (and any errors that may have occurred) to the listed names.  If  no  at  sign
       (@) appears in the name, the destination host is appended to the name (for example, name1@host, name2@host, ...).

       The except command is used to update all of the files in the source list except for the files listed in name list.  This is usually used to
       copy everything in a directory except certain files.

       The except_pat command is like the except command except that pattern list is a list of regular expressions (see for details).  If  one	of
       the  patterns  matches some string within a file name, that file will be ignored.  Note that since e is a quote character, it must be dou-
       bled to become part of the regular expression.  Variables are expanded in pattern list but not shell file pattern matching characters.	To
       include a dollar sign ($), it must be escaped with e.

       The  special  command  is  used	to  specify  commands that are to be executed on the remote host after the file in name list is updated or
       installed.  If the name list is omitted then the shell commands will be executed for every file updated or installed.  The  shell  variable
       FILE  is  set to the current filename before executing the commands in string.  String starts and ends with double quotes (") and can cross
       multiple lines in distfile.  Multiple commands to the shell should be separated by semi-colons (;).  Commands are executed  in  the  user's
       home  directory	on  the  host being updated.  The special command can be used to rebuild private databases, etc.  after a program has been
       updated.

       The following is a small example.

	    HOSTS = ( matisse root@arpa)

	    FILES = ( /bin /lib /usr/bin /usr/games
		       /usr/include/{*.h,{stand,sys,vax*,pascal,machine}/*.h}
		       /usr/lib /usr/man/man? /usr/ucb /usr/local/rdist )

	    EXLIB = ( Mail.rc aliases aliases.dir aliases.pag crontab dshrc
		       sendmail.cf sendmail.fc sendmail.hf sendmail.st uucp vfont )

	    ${FILES} -> ${HOSTS}
		       install -R ;
		       except /usr/lib/${EXLIB} ;
		       except /usr/games/lib ;
		       special /usr/lib/sendmail "/usr/lib/sendmail -bz" ;

	    srcs:
	    /usr/src/bin -> arpa
		       except_pat ( \.o$ /SCCS$ ) ;

	    IMAGEN = (ips dviimp catdvi)

	    imagen:
	    /usr/local/${IMAGEN} -> arpa
		       install /usr/local/lib ;
		       notify ralph ;

	    ${FILES} :: stamp.cory
		       notify root@cory ;

Restrictions
       Source files must reside on the local host where is executed.

       There is no easy way to have a special command executed after all files in a directory have been updated.

       Variable expansion only works for name lists; there should be a general macro facility.

       aborts on files which have a negative mtime (before Jan 1, 1970).

Diagnostics
       A complaint about mismatch of version numbers may really stem from some problem with starting your shell (that is,  you	are  in  too  many
       groups).

Files
       distfile       input command file
       /tmp/rdist*    temporary file for update lists

See Also
       sh(1), csh(1), stat(2)

																	  rdist(1)
All times are GMT -4. The time now is 08:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy