Issue handling single quoted argument in shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue handling single quoted argument in shell script.
# 1  
Old 10-01-2019
Issue handling single quoted argument in shell script.

Below is my script that works fine and prints the desired output:

Code:
#!/bin/ksh
echo "$1" |
  while IFS= read -r dirpath
do
  echo "DIRR_PATH:$dirpath"
  install_dir=$install_dir" "$dirpath
done
echo "Desired Output:$install_dir"

Output:
Code:
./loopissue.sh "/tmp/scripts\\n/tmp/mrt\\n/tmp/exe"
DIRR_PATH:/tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output:/tmp/scripts /tmp/mrt /tmp/exe

The problem is i'm not getting a clean way to seperate the arguments with a single white space as seen in the "desired output:" above when the parameters are passed in single qoutes like below:

Code:
./loopissue.sh '/tmp/scripts\\n/tmp/mrt\\n/tmp/exe'

I'm on Aix6.1 and ksh shell.

Can you please suggest how can I get similar output when the argument is passed in single quotes ?

I tried removing the single quotes using the below two approaches but that did not help.
Code:
sed "s/'//g"
tr -d"'"

In Summary: All \\n should be replaced by single white space and single quotes should be removed from the argument and stored in a variable.

Last edited by mohtashims; 10-01-2019 at 08:51 AM..
# 2  
Old 10-01-2019
In single quotes you must not \escape the \escape:
Code:
./loopissue.sh '/tmp/scripts\n/tmp/mrt\n/tmp/exe'

The shell removes the 'single quotes' and the "double quotes" before it invokes the loopissue.sh. The quotes are only the hint to pass it as one string - to $1 in the loopissue.sh.
Also the following works (and is even better portable, because the \n must be interpreted by the echo command):
Code:
./loopissue.sh '/tmp/scripts
/tmp/mrt
/tmp/exe'

Is there a reason to squeeze all arguments into one $1?
Maybe you can simply use
Code:
./loopissue.sh /tmp/scripts /tmp/mrt /tmp/exe

This maps to $1 $2 $3 in the loopissue.sh
And it's simple to print them in different formats:
Code:
#!/bin/ksh
for dirpath
do
  echo "DIRR_PATH:$dirpath"
done
echo "Desired Output:$@"

Note that for dirpath is short for for dirpath in "$@". In other words, a for loop defaults to loop thru all arguments.
# 3  
Old 10-01-2019
Quote:
Originally Posted by MadeInGermany
In single quotes you must not \escape the \escape:
Code:
./loopissue.sh '/tmp/scripts\n/tmp/mrt\n/tmp/exe'

The shell removes the 'single quotes' and the "double quotes" before it invokes the loopissue.sh. The quotes are only the hint to pass it as one string - to $1 in the loopissue.sh.
Also the following works (and is even better portable, because the \n must be interpreted by the echo command):
Code:
./loopissue.sh '/tmp/scripts
/tmp/mrt
/tmp/exe'

Is there a reason to squeeze all arguments into one $1?
Maybe you can simply use
Code:
./loopissue.sh /tmp/scripts /tmp/mrt /tmp/exe

This maps to $1 $2 $3 in the loopissue.sh
And it's simple to print them in different formats:
Code:
#!/bin/ksh
for dirpath
do
  echo "DIRR_PATH:$dirpath"
done
echo "Desired Output:$@"

Note that for dirpath is short for for dirpath in "$@". In other words, a for loop defaults to loop thru all arguments.
The arguments passed to the script come from the database in the format I shared and cannot be changed hence the proposed solution would not work.

Is there a way to replace all \\n to single whitespace as requested in the original post?
# 4  
Old 10-01-2019
Hi,

Is this what you are expecting;

Code:
#!/bin/ksh
VAR1=`echo -e "${1}" | sed -e "s/'//g"`
echo -e "${VAR1}" |
  while IFS=' '
read -r dirpath
do
  echo "DIRR_PATH:$dirpath"
  install_dir=$install_dir" "$dirpath
done
echo "Desired Output:$install_dir"

Where it is run as;

Code:
./tester.sh '/tmp/scripts\\n/tmp/mrt\\n/tmp/exe'

With this as the Output;

Code:
DIRR_PATH:/tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output: /tmp/scripts /tmp/mrt /tmp/exe

Regards

Gull04

Last edited by gull04; 10-01-2019 at 12:21 PM.. Reason: Posted Wrong Code
# 5  
Old 10-01-2019
Why not simply
Code:
installdir=${1//\\\\n/ }

?
These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 10-01-2019
Nice RudiC, if OP needs support for both double and single quotes on call:

Code:
#!/bin/ksh
install_dir=${1//\\\\n/ }
install_dir=${install_dir//\\n/ }
printf "DIRR_PATH:%s\n" $install_dir
echo "Desired Output:$install_dir"

Code:
$ ./loopissue.sh '/tmp/scripts\\n/tmp/mrt\\n/tmp/exe'
DIRR_PATH:/tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output:/tmp/scripts /tmp/mrt /tmp/exe

$ ./loopissue.sh "/tmp/scripts\\n/tmp/mrt\\n/tmp/exe"
DIRR_PATH:/tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output:/tmp/scripts /tmp/mrt /tmp/exe

# 7  
Old 10-02-2019
Quote:
Originally Posted by RudiC
Why not simply
Code:
installdir=${1//\\\\n/ }

?
Getting error:
Quote:
loopissue.sh[55]: install_dir=${1//\\\\n/ }: bad substitution
--- Post updated at 02:53 AM ---

Quote:
Originally Posted by gull04
Hi,

Is this what you are expecting;

Code:
#!/bin/ksh
VAR1=`echo -e "${1}" | sed -e "s/'//g"`
echo -e "${VAR1}" |
  while IFS=' '
read -r dirpath
do
  echo "DIRR_PATH:$dirpath"
  install_dir=$install_dir" "$dirpath
done
echo "Desired Output:$install_dir"

Where it is run as;

Code:
./tester.sh '/tmp/scripts\\n/tmp/mrt\\n/tmp/exe'

With this as the Output;

Code:
DIRR_PATH:/tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output: /tmp/scripts /tmp/mrt /tmp/exe

Regards

Gull04
@Gull04: I tried your option but i get -e -e before the first element. Please see output below:

Quote:
DIRR_PATH:-e -e /tmp/scripts
DIRR_PATH:/tmp/mrt
DIRR_PATH:/tmp/exe
Desired Output: -e -e /tmp/scripts /tmp/mrt /tmp/exe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

2. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

3. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

4. Shell Programming and Scripting

Issue while executing C executable with argument in shell script

Hi All, I am new to this forum and also shell script :) My task is I have to read CSV file get the data from the file and use the data to call c executable with data as argument.And the output from c executable should be stored to new CSV file Please find below my code testfunction() {... (14 Replies)
Discussion started by: ravjot28
14 Replies

5. Shell Programming and Scripting

Help with Handling multiple argument in shell script

Hi i have written a shell script that takes only single ip address from the user and calculates its latency and reliability, can you please tell me that what should be done if i want that user should enter 100 or 1000 ip address (5 Replies)
Discussion started by: Preeti_17
5 Replies

6. Shell Programming and Scripting

Take quoted output from one script as quoted input for another script

Hi, I have a script output.sh which produces the following output (as an example): "abc def" "ghi jkl" This output should be handled from script input.sh as input and the quotes should be treated as variable delimiters but not as regular characters. input.sh (processing positional... (2 Replies)
Discussion started by: stresing
2 Replies

7. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

8. Shell Programming and Scripting

Handling values with space while passing commandline argument from wrapper script in KSH

Hi there, I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Discussion started by: kans
2 Replies

9. Shell Programming and Scripting

Shell script file handling

Hi ! /bin/sh set logdir1 "logDir/local/logname" #write the filename into a file echo $logdir1 >> logname.txt how do i exec the above echo command (1 Reply)
Discussion started by: nathgopi214
1 Replies

10. UNIX for Advanced & Expert Users

signal handling in shell script

Hi can any please tell me is it possible to catch the signal in a shell script like we do in C. if yes please give me some idea or a link. (4 Replies)
Discussion started by: Raom
4 Replies
Login or Register to Ask a Question