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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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:

Code:
$ 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: See below:

Code:
$ more check.sh
str=`echo "$1" | sed -e 's#\\\\n# #g'`
echo $str>check.now

Code:
$ bash -x ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
++ echo '/fin/app/01/scripts\\n/fin/app/01/sql'
++ sed -e 's#\\n# #g'
+ str='/fin/app/01/scripts\ /fin/app/01/sql'
+ echo '/fin/app/01/scripts\' /fin/app/01/sql

Code:
$ more check.now 
/fin/app/01/scripts\ /fin/app/01/INFENG/sql

We see the issue with "/fin/app/01/scripts\" the trailing \ at the end of the first string.

Note: I have to pass the argument to check.sh script strictly in single quotes only.

Requesting suggestions and explanations for this issue.

Last edited by mohtashims; 1 Week Ago at 09:20 AM..
# 2  
Code:
$ cat check.sh
str=`echo $1  | sed -e 's#[\][\]n# #g'`
echo $str>check.now
$ bash -x ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
++ echo '/fin/app/01/scripts\\n/fin/app/01/sql'
++ sed -e 's#[\][\]n# #g'
+ str='/fin/app/01/scripts /fin/app/01/sql'
+ echo /fin/app/01/scripts /fin/app/01/sql
$ more check.now
/fin/app/01/scripts /fin/app/01/sql
$

# 3  
I think you suffer from the \ handling within `backticks`
Demonstration:
Code:
cat check.sh
str=`echo "$1" | sed -e 's#\\\\n# #g'`
echo "$str"
str=$(echo "$1" | sed -e 's#\\\\n# #g')
echo "$str"

Code:
bash ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
/fin/app/01/scripts\ /fin/app/01/sql
/fin/app/01/scripts /fin/app/01/sql

# 4  
You could try something like this:
Longhand OSX 10.14.3, default bash terminal using 'dash'...
Code:
Last login: Tue Sep  3 18:18:39 on ttys000
AMIGA:amiga~> dash
AMIGA:\u\w> text='oiweroiweroi oieurowir lkj
> asdkjd pqowepom,xcm909184 ;lk
> 0912830980n kjhksjdfkj oizxc
> 
> '
AMIGA:\u\w> echo "${text}"       
oiweroiweroi oieurowir lkj
asdkjd pqowepom,xcm909184 ;lk
0912830980n kjhksjdfkj oizxc


AMIGA:\u\w> text1=$( echo $text )
AMIGA:\u\w> echo "${text1}"
oiweroiweroi oieurowir lkj asdkjd pqowepom,xcm909184 ;lk 0912830980n kjhksjdfkj oizxc
AMIGA:\u\w> exit
AMIGA:amiga~> _

# 5  
Why sed? Try "parameter expansion":
Code:
echo ${1//\\\\n/ }
/fin/app/01/scripts /fin/app/01/sql

# 6  
Fully POSIX compliant using OP's parameters:
Code:
#!/usr/local/bin/dash
# check.sh
# $1 is '/fin/app/01/scripts\\n/fin/app/01/sql'

text=$( echo $( echo $1 ) )
echo $text

Results using as before...
Code:
Last login: Tue Sep  3 23:00:12 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
/fin/app/01/scripts /fin/app/01/sql
AMIGA:amiga~/Desktop/Code/Shell> _

# 7  
Quote:
Originally Posted by RudiC
Why sed? Try "parameter expansion":
Code:
echo ${1//\\\\n/ }
/fin/app/01/scripts /fin/app/01/sql

Sorry, but it does not give correct output. See below:

Quote:
bash -x ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
++ echo '/fin/app/01/scripts\' /fin/app/01/sql
+ str='/fin/app/01/scripts\ /fin/app/01/sql'
+ echo '/fin/app/01/scripts\' /fin/app/01/sql
Look at the trailing \ after the first string /fin/app/01/scripts\

Code:
cat check.sh
str=`echo ${1//\\\\n/ }`
echo $str>check.row

--- Post updated at 05:11 AM ---

Quote:
Originally Posted by wisecracker
Fully POSIX compliant using OP's parameters:
Code:
#!/usr/local/bin/dash
# check.sh
# $1 is '/fin/app/01/scripts\\n/fin/app/01/sql'

text=$( echo $( echo $1 ) )
echo $text

Results using as before...
Code:
Last login: Tue Sep  3 23:00:12 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
/fin/app/01/scripts /fin/app/01/sql
AMIGA:amiga~/Desktop/Code/Shell> _

This does not work for me. See my test below.

Code:
cat check.sh
str=$( echo $( echo $1 ) )
echo $str>check.row

Quote:
Results:
bash -x ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
+++ echo '/fin/app/01/scripts\\n/fin/app/01/sql'
++ echo '/fin/app/01/scripts\\n/fin/app/01/sql'
+ str='/fin/app/01/scripts\\n/fin/app/01/sql'
+ echo '/fin/app/01/scripts\\n/fin/app/01/sql'
Also, I intend to run this on ksh shell as well.

--- Post updated at 05:13 AM ---

Quote:
Originally Posted by wisecracker
You could try something like this:
Longhand OSX 10.14.3, default bash terminal using 'dash'...
Code:
Last login: Tue Sep  3 18:18:39 on ttys000
AMIGA:amiga~> dash
AMIGA:\u\w> text='oiweroiweroi oieurowir lkj
> asdkjd pqowepom,xcm909184 ;lk
> 0912830980n kjhksjdfkj oizxc
> 
> '
AMIGA:\u\w> echo "${text}"       
oiweroiweroi oieurowir lkj
asdkjd pqowepom,xcm909184 ;lk
0912830980n kjhksjdfkj oizxc


AMIGA:\u\w> text1=$( echo $text )
AMIGA:\u\w> echo "${text1}"
oiweroiweroi oieurowir lkj asdkjd pqowepom,xcm909184 ;lk 0912830980n kjhksjdfkj oizxc
AMIGA:\u\w> exit
AMIGA:amiga~> _

This is not a new line but delimiter of type string "\\n" i guess.

--- Post updated at 05:26 AM ---

Quote:
Originally Posted by MadeInGermany
I think you suffer from the \ handling within `backticks`
Demonstration:
Code:
cat check.sh
str=`echo "$1" | sed -e 's#\\\\n# #g'`
echo "$str"
str=$(echo "$1" | sed -e 's#\\\\n# #g')
echo "$str"

Code:
bash ./check.sh '/fin/app/01/scripts\\n/fin/app/01/sql'
/fin/app/01/scripts\ /fin/app/01/sql
/fin/app/01/scripts /fin/app/01/sql

@MadeInGermany you seem to have highlighted the core point of this issue.

However, I wanted to know if there is a solution for ksh shell on AiX with backticks ? A non-sed solution would also do. Thank you!!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command works on Linux but fails on Solaris.

Hi, I am looking for a generic find command that works on both Linux and Solaris. I have the below command that works fine on Linux but fails on solaris.find /web/config -type f '(' -name '*.txt' -or -name '*.xml' -name '*.pro' ')' Fails on SunOS mysolaris 5.10 Generic_150400-61 sun4v sparc... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. 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

3. Shell Programming and Scripting

Why my git command has no output in crontab but works well run this script manually?

cat /home/lyang001/update.sh #!/bin/sh #shopt -s expand_aliases HOME_DIR=/home/lyang001/updates UPDATE_MAIL=${HOME_DIR}/updates.mail rm $UPDATE_MAIL -rf cd $HOME_DIR/wr-kernel git log --no-merges --since="20 day ago" --name-status --pretty=format:"%an %h %s %cd" origin/WRLINUX_5_0_1_HEAD >>... (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. 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

5. 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

6. UNIX for Dummies Questions & Answers

Works on command line but not in script

Hey guys. Hopefully this is an easy one but having reference similar problems on the web I still can't fix it. I am doing a recursive find and replace from a script. Of course I could just run the damn thing from the command line but it's bugging me now and want to get it working. grep -rl... (4 Replies)
Discussion started by: anthonyjstewart
4 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Cron job fails, but works fine from command line

I have a very basic script that essentially sends a log file, via FTP, to a backup server. My cron entry to run this every night is: 55 23 * * * /usr/bin/archive_logs The script runs perfectly when executed manually, and actually worked via cron for about three weeks. However, it mysteriously... (3 Replies)
Discussion started by: cdunavent
3 Replies

10. 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

Featured Tech Videos