The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help how replace stardard keyboard inputs by arguments at run time of a script Dingrong UNIX for Dummies Questions & Answers 6 10-14-2008 04:46 AM
Help in passing array of inputs to C program using script? ahjiefreak Shell Programming and Scripting 1 03-20-2008 07:36 AM
Passing blank arguments to a script rm-r UNIX for Advanced & Expert Users 7 01-08-2008 08:56 PM
Shell script with arguments sankar6254 Shell Programming and Scripting 3 12-22-2003 09:21 AM
Passing arguments to a script Kevin Pryke Shell Programming and Scripting 3 06-14-2002 10:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-17-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Question use several inputs as arguments in my script

Hi there,
It's pretty hard for me to explain my problem because I'm affraid I'm not using the correct vocabulary. So let me describe the situation. I wrote a script that has one argument. It works like this:

Code:
~$ cat /usr/local/bin/squote
echo "$@" | sed 's/'\''/'\''\\'\'\''/g; s/.*/'\''&'\''/g'
~$ squote It\'s great
'It'\''s great'

What do I need to do if I want my script to work in the following situation?

Code:
~$ echo It\'s great | squote
''
~$ cat file | squote
''
~$ squote < file
''

Thanks in advance
Santiago

Update:
Now I found that I can read from /dev/stdin. I wrote the following script:

Code:
~$ cat /usr/local/bin/squote
cat /dev/stdin | sed 's/'\''/'\''\\'\'\''/g; s/.*/'\''&'\''/g'
echo "$@" | sed 's/'\''/'\''\\'\'\''/g; s/.*/'\''&'\''/g'
~$ echo It\'s great | squote
'It'\''s great'
''
~$ squote It\'s great
# I need to press Ctrl+D
'It'\''s great'

How can I know from which input the argument is coming?

Last edited by chebarbudo; 11-17-2008 at 10:15 PM.. Reason: found part of the answer
  #2 (permalink)  
Old 11-18-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
You can test, if the script was called with an argument:

[[ -z "$@" ]] && printf "No input\n" || printf "Input: %q\n" "$@"

Which means: if the length of the arguments ( $@) is zero (-z) then print "No input" else print the string in escaped form. I don't not, what you want to achieve, but if you try to escape strings, check the %q option of printf.

If you call the testscript this way:

testscript "h's m"

it will give you:

Input: h\'s\ m
  #3 (permalink)  
Old 11-22-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Thx Christoph,
Although printf is a much better function for my script, you don't answer at all to my problem. What I need is a way to make the script work in the following situation:

Code:
~# cat /usr/local/bin/squote
[[ -z "$@" ]] && printf "No input\n" || printf "Input: %q\n" "$@"
~# echo "h's m" | squote
No input

Closed Thread

Bookmarks

Tags
arguments, input

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0