<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Challenging Script question in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974160#M102152</link>
    <description>I have a file named File1 with the following data:&lt;BR /&gt;Line1&lt;BR /&gt;Line2&lt;BR /&gt;Line3&lt;BR /&gt;Line4&lt;BR /&gt;Line5&lt;BR /&gt;&lt;BR /&gt;Im trying to write a script that takes two arguments. The first being a line of text and the second the name of the file above. How do I insert the first argument(line of text) into the first line of the file named above.&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;./scriptTest Hello this is a test File1&lt;BR /&gt;&lt;BR /&gt;Would append File1 as such:&lt;BR /&gt;Hello this is a test&lt;BR /&gt;Line1&lt;BR /&gt;Line2&lt;BR /&gt;Line3&lt;BR /&gt;Line4&lt;BR /&gt;Line5&lt;BR /&gt;</description>
    <pubDate>Tue, 18 Apr 2006 11:20:18 GMT</pubDate>
    <dc:creator>Unix or Linux?</dc:creator>
    <dc:date>2006-04-18T11:20:18Z</dc:date>
    <item>
      <title>Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974160#M102152</link>
      <description>I have a file named File1 with the following data:&lt;BR /&gt;Line1&lt;BR /&gt;Line2&lt;BR /&gt;Line3&lt;BR /&gt;Line4&lt;BR /&gt;Line5&lt;BR /&gt;&lt;BR /&gt;Im trying to write a script that takes two arguments. The first being a line of text and the second the name of the file above. How do I insert the first argument(line of text) into the first line of the file named above.&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;./scriptTest Hello this is a test File1&lt;BR /&gt;&lt;BR /&gt;Would append File1 as such:&lt;BR /&gt;Hello this is a test&lt;BR /&gt;Line1&lt;BR /&gt;Line2&lt;BR /&gt;Line3&lt;BR /&gt;Line4&lt;BR /&gt;Line5&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Apr 2006 11:20:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974160#M102152</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T11:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974161#M102153</link>
      <description>header=$1&lt;BR /&gt;filename=$2&lt;BR /&gt;(echo $header ;cat $filename) &amp;gt; /tmp/mytmpfile&lt;BR /&gt;mv /tmp/mytmpfile $filename&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;one of the ways how to do it..&lt;BR /&gt;&lt;BR /&gt;HTH</description>
      <pubDate>Tue, 18 Apr 2006 11:25:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974161#M102153</guid>
      <dc:creator>Mel Burslan</dc:creator>
      <dc:date>2006-04-18T11:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974162#M102154</link>
      <description>forgot to add, if your comment is multiple words containing spaces, make sure to enclose it in quotes.</description>
      <pubDate>Tue, 18 Apr 2006 11:26:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974162#M102154</guid>
      <dc:creator>Mel Burslan</dc:creator>
      <dc:date>2006-04-18T11:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974163#M102155</link>
      <description>You're going to have to put quotes around the "Hello this is a test", then your script will look like:&lt;BR /&gt;&lt;BR /&gt;echo $1 &amp;gt; /tmp/file&lt;BR /&gt;cat $2 &amp;gt;&amp;gt; /tmp/file&lt;BR /&gt;cp /tmp/file $2&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Apr 2006 11:26:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974163#M102155</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2006-04-18T11:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974164#M102156</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Using the shell only:&lt;BR /&gt;&lt;BR /&gt;cat ./insertit&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;echo "${1}" &amp;gt;&amp;gt; "${2}.new"&lt;BR /&gt;cat   ${2}  &amp;gt;&amp;gt; "${2}.new"&lt;BR /&gt;mv   "${2}.new" "${2}"&lt;BR /&gt;&lt;BR /&gt;# ./insert it ok yourfile&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Apr 2006 11:32:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974164#M102156</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-04-18T11:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974165#M102157</link>
      <description>Hi is there any other way to do it without having to use quotes. I cant say to the user if you have more than one word then put quotes around it.&lt;BR /&gt;&lt;BR /&gt;So far I have managed the following.&lt;BR /&gt;&lt;BR /&gt;sed -e '1i\' -e "$*" &amp;gt; File1&lt;BR /&gt;&lt;BR /&gt;But this appends the whole line including the second argument.</description>
      <pubDate>Tue, 18 Apr 2006 11:47:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974165#M102157</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T11:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974166#M102158</link>
      <description>Can you have the users put the filename first, then-&lt;BR /&gt; &lt;BR /&gt;f=$1&lt;BR /&gt;t=/tmp/hold$$&lt;BR /&gt;shift&lt;BR /&gt;echo $* | cat - $f &amp;gt;$t&lt;BR /&gt;mv $t $f&lt;BR /&gt; &lt;BR /&gt;HTH&lt;BR /&gt; &lt;BR /&gt;-- Rod Hills</description>
      <pubDate>Tue, 18 Apr 2006 12:00:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974166#M102158</guid>
      <dc:creator>Rodney Hills</dc:creator>
      <dc:date>2006-04-18T12:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974167#M102159</link>
      <description>How about the following:&lt;BR /&gt;&lt;BR /&gt;echo hello this is a test file1 | awk '{&lt;BR /&gt;for(i=1;i&lt;NF&gt; $NF&lt;BR /&gt;}'&lt;BR /&gt;&lt;BR /&gt;cheers!&lt;/NF&gt;</description>
      <pubDate>Tue, 18 Apr 2006 12:01:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974167#M102159</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-04-18T12:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974168#M102160</link>
      <description>Hi&lt;BR /&gt;&lt;BR /&gt;The line of text has to come first then the file name.&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
      <pubDate>Tue, 18 Apr 2006 12:03:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974168#M102160</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T12:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974169#M102161</link>
      <description>Hi Sandman&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Im not sure that will work. How does the script know to echo the line of text: Hello this is a test, unless you manually type it in the script.&lt;BR /&gt;&lt;BR /&gt;It could be any line of text.</description>
      <pubDate>Tue, 18 Apr 2006 12:10:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974169#M102161</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T12:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974170#M102162</link>
      <description>Infact to build a shell script do the following:&lt;BR /&gt;&lt;BR /&gt;=================myshellscript=================&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;echo $* | awk '{&lt;BR /&gt;for(i=1;i&lt;NF&gt; $NF&lt;BR /&gt;}'&lt;BR /&gt;=============================================&lt;BR /&gt;&lt;BR /&gt;Give your script a name "myshellscript" make it executable "chmod" and execute it at the command line as...&lt;BR /&gt;&lt;BR /&gt;# ./myshellscript&lt;BR /&gt;&lt;BR /&gt;hope it helps!&lt;/NF&gt;</description>
      <pubDate>Tue, 18 Apr 2006 12:10:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974170#M102162</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-04-18T12:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974171#M102163</link>
      <description>You can do it without quotes like this:&lt;BR /&gt;&lt;BR /&gt;given file1&lt;BR /&gt;Line1&lt;BR /&gt;Line2&lt;BR /&gt;Line3&lt;BR /&gt;Line4&lt;BR /&gt;Line5&lt;BR /&gt;&lt;BR /&gt;./script Hello world file1&lt;BR /&gt;&lt;BR /&gt;script:&lt;BR /&gt; touch .mytempx&lt;BR /&gt;rm .mytemp?*&lt;BR /&gt;echo $@ | awk '{print "cp ", $NF, " .mytempfile2" }'  &amp;gt; .mytempx&lt;BR /&gt;echo $@ | awk '{print "cat ", " .mytempfile2 &amp;gt;&amp;gt; .mytempfile3"; print " mv .mytempfile3 ",$NF }'  &amp;gt; .mytempx2&lt;BR /&gt;chmod +x .mytempx .mytempx2&lt;BR /&gt;./.mytempx&lt;BR /&gt;echo $@ |  awk '{$1==$NF="";print $0}' &amp;gt;  .mytempfile3&lt;BR /&gt;./.mytempx2&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Apr 2006 12:12:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974171#M102163</guid>
      <dc:creator>Kent Ostby</dc:creator>
      <dc:date>2006-04-18T12:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974172#M102164</link>
      <description>In my last post I forgot to add that run the shell script you created as follows:&lt;BR /&gt;&lt;BR /&gt;# myshellscript Hello this is a test file1&lt;BR /&gt;&lt;BR /&gt;...passing command line arguments to the script instead of running it w/o any.&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Tue, 18 Apr 2006 12:20:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974172#M102164</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-04-18T12:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974173#M102165</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Here's yet another way to offer the user the ability to specify the name of the file *last* and the string to insert *first* without the need to quote anything:&lt;BR /&gt;&lt;BR /&gt;# cat .inject.pl&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;my $file = pop;&lt;BR /&gt;open(FH,"&amp;lt;",$file) or die "Can't open $file\n";&lt;BR /&gt;print "@ARGV\n";&lt;BR /&gt;print while (&lt;FH&gt;);&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;# ./inject.pl Hello this is a test FileName&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;/FH&gt;</description>
      <pubDate>Tue, 18 Apr 2006 12:29:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974173#M102165</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-04-18T12:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974174#M102166</link>
      <description>Here is a method that uses ksh arrays to pick off the last argument.&lt;BR /&gt; &lt;BR /&gt;set -A a $*&lt;BR /&gt;(( n = ${#a[*]} - 1 ))&lt;BR /&gt;f=${a[$n]}&lt;BR /&gt;a[$n]=""&lt;BR /&gt;t=/tmp/hold$$&lt;BR /&gt;echo ${a[*]} | cat - $f &amp;gt;$t&lt;BR /&gt;mv $t $f&lt;BR /&gt; &lt;BR /&gt;HTH&lt;BR /&gt; &lt;BR /&gt;-- Rod Hills</description>
      <pubDate>Tue, 18 Apr 2006 12:29:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974174#M102166</guid>
      <dc:creator>Rodney Hills</dc:creator>
      <dc:date>2006-04-18T12:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974175#M102167</link>
      <description>Hi Sandman&lt;BR /&gt;&lt;BR /&gt;Just tried your code but it seems to delete everything in the file and then write the line of text to the file.&lt;BR /&gt;&lt;BR /&gt;Any ideas?</description>
      <pubDate>Tue, 18 Apr 2006 12:59:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974175#M102167</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T12:59:51Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974176#M102168</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;If you prefer not to preserve your original file, but use the Perl script I suggested, then use this version:&lt;BR /&gt;&lt;BR /&gt;# cat .inject.pl&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;my $oldfile =  pop;&lt;BR /&gt;my $newfile = "$oldfile.new";&lt;BR /&gt;open(FIN, "&amp;lt;",$oldfile) or die "Can't open '$oldfile': $!\n";&lt;BR /&gt;open(FOUT,"&amp;gt;",$newfile) or die "Can't open '$newfile': $!\n";&lt;BR /&gt;print FOUT "@ARGV\n";&lt;BR /&gt;print FOUT while (&lt;FIN&gt;);&lt;BR /&gt;close FOUT;&lt;BR /&gt;close FIN;&lt;BR /&gt;rename($newfile, $oldfile);&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;# ./inject.pl Hello this is a test FileName&lt;BR /&gt;&lt;BR /&gt;In this variation, the modified file ("FileName") is updated, "in-place".&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;/FIN&gt;</description>
      <pubDate>Tue, 18 Apr 2006 13:27:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974176#M102168</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-04-18T13:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974177#M102169</link>
      <description>&amp;gt;Just tried your code but it seems to delete &amp;gt;everything in the file and then write the &amp;gt;line of text to the file.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Any ideas?&lt;BR /&gt;&lt;BR /&gt;Here's the updated version of "myshellscript" so that file1 doesn't get clobbered and the text gets written above the first line in file1:&lt;BR /&gt;&lt;BR /&gt;=============================================&lt;BR /&gt;#!/bin/sh -x&lt;BR /&gt;&lt;BR /&gt;echo $@ | awk '{&lt;BR /&gt;system("mv "$NF" tmpfile1")&lt;BR /&gt;for(i=1;i&lt;NF&gt; $NF&lt;BR /&gt;system("cat tmpfile1 &amp;gt;&amp;gt;"$NF)&lt;BR /&gt;}'&lt;BR /&gt;=============================================&lt;BR /&gt;&lt;BR /&gt;hope it helps!&lt;/NF&gt;</description>
      <pubDate>Tue, 18 Apr 2006 13:35:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974177#M102169</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-04-18T13:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974178#M102170</link>
      <description>Hi Sandman&lt;BR /&gt;&lt;BR /&gt;It worked perfectly. Thank you.&lt;BR /&gt;Any chance that you could explain the code as it is a bit advanced for me.</description>
      <pubDate>Tue, 18 Apr 2006 13:47:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974178#M102170</guid>
      <dc:creator>Unix or Linux?</dc:creator>
      <dc:date>2006-04-18T13:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Challenging Script question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974179#M102171</link>
      <description>The shell script is attached so that its clearer to follow. Moreover you can use $* and $@ interchangeably.&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Tue, 18 Apr 2006 13:50:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/challenging-script-question/m-p/4974179#M102171</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-04-18T13:50:54Z</dc:date>
    </item>
  </channel>
</rss>

