Run command without over-writing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run command without over-writing
# 1  
Old 04-21-2013
Run command without over-writing

I have a script called script.sh that has the following in it.

cat /home/auto/script.sh

Code:
#!/bin/bash

ARGA=$1

if [ "${ARGA}" = "jsmith" ] ; then
        echo "${ARGA}:Confirmed"
else
        echo "${ARGA}:Unconfirmed"
fi

I need a way to run this script, and then, after script has confirmed the argument given to it, i'd like to comment out the section of the script that confirmed the argument (lines 2-9). in other words, i basically want to add a "#" to the beginning of lines 2 to 9. Then, after those lines are commented out, id like to append the word "date" to the end of the script. then run the script again.

I'm trying to do this all in one go. But i'm not sure how to go about it.

The logic should be something like this:

Code:
/home/auto/script.sh jsmith | sed -n 2-9,s~^*~#^*~g /home/auto/script.sh > /home/auto/script.sh | echo "date" >> /home/auto/script.sh ; /home/auto/script.sh

I'm aware this command is 100% wrong, but the idea of it is what i'm after. i'm hoping someone can re-write it for me. I want to be able to do this with a one-liner. or at least something close to it.

i would prefer not to have to create several little temp scripts which are to be deleted later (unless they can be created in memory, not hard disk). i want all this to be done within the "script.sh" script. but if there are no other solutions, i wouldn't mind it creating temps.

Last edited by SkySmart; 04-21-2013 at 04:36 PM..
# 2  
Old 04-21-2013
That's a very interesting idea, having shell scripts that more or less edit themselves. I do see a possible practical problem here. If the script comments out the test lines and adds 'date' at the end, what use is the script in the future? Just commenting those lines and adding 'date' doesn't seem useful, but there might be some other variation that is perhaps quite useful.
# 3  
Old 04-21-2013
Quote:
Originally Posted by hanson44
That's a very interesting idea, having shell scripts that more or less edit themselves. I do see a possible practical problem here. If the script comments out the test lines and adds 'date' at the end, what use is the script in the future? Just commenting those lines and adding 'date' doesn't seem useful, but there might be some other variation that is perhaps quite useful.
there is a use. but explaining the use will take forever. so i just want to get this small (seemingly small), section out of the way first before even attempting to go into deeper details. but if someone can accomplish the situation i laid out above here, everything else is simple.

i'm sure (at least i hope) someone on here knows of a way to do this.
# 4  
Old 04-21-2013
Code:
$ /tmp/script.sh jsmith; sed "2,9 s/^/#/" /tmp/script.sh > /tmp/temp.x; mv /tmp/temp.x /tmp/script.sh; chmod 755 /tmp/script.sh; echo "date" >> /tmp/script.sh; /tmp/script.sh
jsmith:Confirmed
Sun Apr 21 13:35:52 PDT 2013

This User Gave Thanks to hanson44 For This Post:
# 5  
Old 04-21-2013
Quote:
Originally Posted by hanson44
Code:
$ /tmp/script.sh jsmith; sed "2,9 s/^/#/" /tmp/script.sh > /tmp/temp.x; mv /tmp/temp.x /tmp/script.sh; chmod 755 /tmp/script.sh; echo "date" >> /tmp/script.sh; /tmp/script.sh
jsmith:Confirmed
Sun Apr 21 13:35:52 PDT 2013

interesting!
is there a way to avoid the part i bolded in red? basically i want to (if i can), avoid creating these temp files.
# 6  
Old 04-21-2013
If your sed allows sed -i then here is a simpler way:
Code:
$ /tmp/script.sh jsmith; sed -i "2,9 s/^/#/" /tmp/script.sh; echo "date" >> /tmp/script.sh; /tmp/script.sh
jsmith:Confirmed
Sun Apr 21 15:34:28 PDT 2013

This User Gave Thanks to hanson44 For This Post:
# 7  
Old 04-21-2013
sed (and perl) -i still create temp files. It just saves you the trouble of having to do it yourself.

A different approach:
Code:
sed '2,9 s/^/#/; $a\
date' script.sh | bash

Regards,
Alister

Last edited by alister; 04-21-2013 at 09:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Writing a REST server to run on FreeBSD -- how to structure for concurrency?

Hi All, I want to write a domain specific REST/HTTP server to run on FreeBSD. I have control over both the server side and the primary client intended to consume the service. My question is: how do you think it should be designed to support multiple connections, given: It will run on a... (6 Replies)
Discussion started by: LittleCookieMon
6 Replies

2. Shell Programming and Scripting

Help with writing a script to run java commands in sequence in UNIX

Hi, Brand new to these forums, and I hope that someone can help me out. I'm trying to run the following command in UNIX java -jar GenomeAnalysisTK.jar -T SplitSamFile -dt NONE -R reference.fa -I my.bam --outputRoot /my/path/SampleFiles/Sample_ It executes the SplitSamFile from GATK, but I... (3 Replies)
Discussion started by: Wixaros
3 Replies

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

Writing a script to run weekly/monthly - check for weekday or day-of-the-month

Hi all, I currently have a UNIX file maintenance script that runs daily as a cron job. Now I want to change the script and create functions/sub inside it that runs on a weekly or monthly basis. To run all the scripts' daily maintenance, I want to schedule it in cron as simply maint.sh... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

6. OS X (Apple)

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In the... (2 Replies)
Discussion started by: JFraser1
2 Replies

7. Shell Programming and Scripting

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In... (3 Replies)
Discussion started by: JFraser1
3 Replies

8. Shell Programming and Scripting

Is command line invocation of gnome-terminal to run more than one command possible?

Hello, I am trying to learn how to pass something more than a one-command startup for gnome-terminal. I will give an example of what I'm trying to do here: #! /bin/bash # #TODO write this for gnome and xterm USAGE=" ______________________________________________ ${0##*/} run... (0 Replies)
Discussion started by: Narnie
0 Replies

9. Programming

writing your own command in unix/linux

Hi I am very new to Linux programming,otherwise I have exposure to Linux. Was thinking about something like writing my own commands for Linux. Any ideas where to start, any useful links and what I need to know before I start with this. Thanks :) Sidhu (3 Replies)
Discussion started by: Amardeep
3 Replies

10. Programming

Re-writing ls -F command

As the subject states, I am re-writing the ls -F command in C++, but I am having trouble adding some of the Formatting characters to the files names. Here is my code: if (strcmp(buff, "-F")==0) { struct stat s; stat(direntp->d_name,&s); cout<<direntp->d_name; ... (3 Replies)
Discussion started by: jj1814
3 Replies
Login or Register to Ask a Question