Sponsored Content
Full Discussion: A little help please
Top Forums Shell Programming and Scripting A little help please Post 302145626 by porter on Wednesday 14th of November 2007 09:36:26 PM
Old 11-14-2007
Quote:
Originally Posted by immyakram
I believe this can done by using the echo function by getting the line from $1 and then inserting it into $2 bu cant quite put my finger on it.
That is the essential nub of the problem. For instance, lastline would be a piece of cake

Code:
#!/bin/sh
echo "$1" >>"$2"

because the >> operator appends the data. But this problem says we have to put the data as a new first line.

So my solution says, okay I know what the contents of the new file should look like

Code:
echo "$1" | cat - "$2"

but that output just dissappears, so we need to put it somewhere, like a temporary file, here $$ will be used as a temporary file as it's a unique number, ie the pid of the shell.

Code:
echo "$1" | cat - "$2" >$$

but the problem says that this new output should be in the original file, so what we do is rename this temporary file to the original file name, which as a byproduct is (a) atomic (b) unlinks the original file

Code:
mv $$ $2

Does that help?

Now tell me any down sides of my solution... Smilie
 
ATF-SH(1)						    BSD General Commands Manual 						 ATF-SH(1)

NAME
atf-sh [-s shell] -- interpreter for shell-based test programs SYNOPSIS
atf-sh script DESCRIPTION
atf-sh is an interpreter that runs the test program given in script after loading the atf-sh(3) library. atf-sh is not a real interpreter though: it is just a wrapper around the system-wide shell defined by ATF_SHELL. atf-sh executes the inter- preter, loads the atf-sh(3) library and then runs the script. You must consider atf-sh to be a POSIX shell by default and thus should not use any non-standard extensions. The following options are available: -s shell Specifies the shell to use instead of the value provided by ATF_SHELL. ENVIRONMENT
ATF_LIBEXECDIR Overrides the builtin directory where atf-sh is located. Should not be overridden other than for testing purposes. ATF_PKGDATADIR Overrides the builtin directory where libatf-sh.subr is located. Should not be overridden other than for testing purposes. ATF_SHELL Path to the system shell to be used in the generated scripts. Scripts must not rely on this variable being set to select a specific interpreter. EXAMPLES
Scripts using atf-sh(3) should start with: #! /usr/bin/env atf-sh Alternatively, if you want to explicitly choose a shell interpreter, you cannot rely on env(1) to find atf-sh. Instead, you have to hardcode the path to atf-sh in the script and then use the -s option afterwards as a single parameter: #! /path/to/bin/atf-sh -s/bin/bash ENVIRONMENT
ATF_SHELL Path to the system shell to be used in the generated scripts. SEE ALSO
atf-sh(3) BSD
September 27, 2014 BSD
All times are GMT -4. The time now is 09:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy