Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Works on command line but not in script Post 302501687 by anthonyjstewart on Friday 4th of March 2011 09:13:39 AM
Old 03-04-2011
EXAMPLE 1: COMMAND LINE STRING

Working command line command: grep -rl POP * | xargs sed -i 's/POP/MOM/g'

This command should access all files in the current directory that have POP in them. It should then replace all instances of POP with MOM.

There is no output except that the files that have the string POP in them have bene changed so they now have MOM in them.

EXAMPLE 2: SCRIPT BASED CHANGE

Below is the command line inside a script. The script runs, takes two parameters POP and MOM so you would run it like this:

Code:
bash findandreplace.sh POP MOM

./findandreplace.sh POP MOM

The contents of the script are below

Code:
#!/bin/bash
SHELL=/bin/bash
MAIL=/var/mail/root
PATH=/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

if [ -z "$1" ] || [ -z "$2" ]
then
  echo " "
  echo " "
  echo "Usage: findandreplace.sh string1 string2"
  echo " "
  echo "Example:  findandreplace.sh  begin  end"
  echo " "
  echo "This will find all instances of begin and replace with end"
  echo "within the current directory."
  echo " "
 exit -1
fi
grep -rl $1 * | xargs sed -i 's/$1/$2/g'



When I echo the command line above inside the script the command line is perfect. But it doesn't actually make the change.

Last edited by pludi; 03-04-2011 at 10:53 AM.. Reason: code tags please
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

script works on command line, not in cron job

Hey there, I'm a total newbie unix guy here and just picking this stuff up. Have a very small script I put together that works fine from the command line but not once I put it in a cron job. Searched and found this thread and am wondering it it has something to do with setting variables, though the... (7 Replies)
Discussion started by: JackTheTripper
7 Replies

2. Shell Programming and Scripting

works step by step on command line but not in script

Hi all, The following script is fine when I work via command line m=1 c=0 while do echo $m gnokii --getsms IN $m > out.txt; m=`expr $m + 1`; cat out.txt >> message_log; ############ read first crap< <(sed -n '/Text:/{n;p;}' out.txt); read message< <(sed -n '/Text:/{n;p;}'... (2 Replies)
Discussion started by: whamchaxed
2 Replies

3. Shell Programming and Scripting

Zgrep works at command line but not in script?

Hi all- I'm trying to search through some .gz log files to verify certain feeds have passed through our app. I have a small script that I wrote in hopes that I could automate the checking but haven't been able to get the zgrep to work. When I copy it to the command line directly it works... (2 Replies)
Discussion started by: Cailet
2 Replies

4. Shell Programming and Scripting

This code works in the command line but not in a shl script

When I run this code from the command line works spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ ls ef* eftseq.dat spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ file_seq=$( < eftseq.dat) ... (1 Reply)
Discussion started by: rechever
1 Replies

5. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

6. Shell Programming and Scripting

simple php/expect script works from command line but not from web

I have a really basic expect script which I call from php. I works fine when I run the php from the shell, but from the web it appears as if the output buffer gets chopped and never gets all of the contents. php script: (runexpect.php) <?php... (7 Replies)
Discussion started by: jacksona2
7 Replies

7. UNIX for Dummies Questions & Answers

Works on command line but not in script; vncserver on solaris 10

Hi guys. My first post, so be gentle... On my Solaris 10 machine vnc server is running. I need a command to extract most recent client session number (screen). So with: Code: bash-3.2# ps -ef | grep vnc | grep Xaut root 19805 19797 0 15:41:44 ? 0:01 Xvnc :4 -inetd -once... (5 Replies)
Discussion started by: cp6uja
5 Replies

8. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

9. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

10. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies
Regexp::Common::URI::pop(3)				User Contributed Perl Documentation			       Regexp::Common::URI::pop(3)

NAME
Regexp::Common::URI::pop -- Returns a pattern for POP URIs. SYNOPSIS
use Regexp::Common qw /URI/; while (<>) { /$RE{URI}{POP}/ and print "Contains a POP URI. "; } DESCRIPTION
$RE{URI}{POP} Returns a pattern that matches POP URIs, as defined by RFC 2384. POP URIs have the form: "pop:" "//" [ user [ ";AUTH" ( "*" | auth_type ) ] "@" ] host [ ":" port ] Under "{-keep}", the following are returned: $1 The complete URI. $2 The scheme. $3 The user, if given. $4 The authentication type, if given (could be a *). $5 The host. $6 The port, if given. REFERENCES
[RFC 2384] Gellens, R.: POP URL Scheme. August 1998. SEE ALSO
Regexp::Common::URI for other supported URIs. AUTHOR
Abigail. (regexp-common@abigail.be). BUGS AND IRRITATIONS
Bound to be plenty. LICENSE and COPYRIGHT This software is Copyright (c) 2001 - 2009, Damian Conway and Abigail. This module is free software, and maybe used under any of the following licenses: 1) The Perl Artistic License. See the file COPYRIGHT.AL. 2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2. 3) The BSD Licence. See the file COPYRIGHT.BSD. 4) The MIT Licence. See the file COPYRIGHT.MIT. perl v5.18.2 2013-03-08 Regexp::Common::URI::pop(3)
All times are GMT -4. The time now is 03:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy