Inputs argument for sh -c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inputs argument for sh -c
# 1  
Old 11-26-2017
Inputs argument for sh -c

I have doubts with the following command:
Code:
% find "$1" -name "*.html" -print0 | sort -zn | xargs -r -0 -n 1 sh -c 'echo "Dumping file: $2" >> "$1"; w3m "$2" >> "$1" 2>&1 ' sh "$2"

I have doubts in the input arguments value i.e. $0, $1, $2...

Step by step:
1.- % find "$1" -name "*.html" -print0 | ...
where:
- $1 is the name of the directory to search on
2.-
Code:
% ...| ... sh -c 'echo "Dumping file: $2" >> "$1"; w3m "$2" >> "$1" 2>&1' sh "$2"

where:
- $0 is sh -c
- $1 is the same as $1 in the step 1 Smilie??? Why???
- $2 is the htmlA
- $3 is the htmlB and so on with $4, $5 etc.
3.- Finally, .. sh "$2" makes a shift
The value in $3 will be now $2

Is my understanding correct? How is the value of $1 taken?


Thanks.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by Neo; 11-26-2017 at 02:42 PM..
# 2  
Old 11-26-2017
Not sure I understand your doubts, so please correct me should my interpretation be wrong.
Generally, you need to differentiate between positional parameters ($1, $2, ...) of the parent shell, and those that the subshell will see. Whatever you see in you above command line is the parent shell. So -
1.- Yes if the parent shell's first positional parameter holds a starting directory.

2.- the subshell started by sh will have a concoction of parent and sub shell's pos. parameters, depending on where and how you put single and / or double quotes. As this is difficult to tell from here, I'd start with a small subset of the commands and parameters to find out.

3. Definitely NOT. shift ist the (builtin) command to shift positional parameters, sh will run a (bourne) shell as you did it in the xargs parameters.
# 3  
Old 11-26-2017
If you think you have to use shift on the first sh -c , then
Code:
# change sh -c [your_command arg1 arg2 ...]  to
/bin/bash -c  [ your_command arg1 arg2 ]

# 4  
Old 11-26-2017
Rudi -

Quote:
8.4.2 Invoking the shell from xargs. Normally, xargs will exec the command you specified directly, without invoking a shell. This is normally the behaviour one would want. It's somewhat more efficient and avoids problems with shell metacharacters, for example.
From: "xargs"
# 5  
Old 11-26-2017
@jim mcnamara: Yes, understood, but doesn't s/he have sh as the command to run from xargs?
# 6  
Old 12-01-2017
Quote:
Originally Posted by RudiC
I'd start with a small subset of the commands and parameters to find out.
Can someone give a source to start digging on this?

Furthermore. Can someone clarify the following:
$0 $1 $2 ... After the sh -c
$0: ?????
$1: name of the log file Why ??? From where is taken
$2: htmlA file
$3: htmlB file
...
Smilie
# 7  
Old 12-01-2017
Quote:
Originally Posted by puertas12
Can someone give a source to start digging on this?
Advanced Bash-Scripting Guide

Quote:
Furthermore. Can someone clarify the following:
$0 $1 $2 ... After the sh -c
They are not the same $0 $1 $2 variables as your own shell. They are in single quotes:
Code:
'echo "Dumping file: $2" >> "$1"; w3m "$2" >> "$1" 2>&1'

...so they are passed into sh -c unparsed, for it to handle.

I have my doubts about them too.
Quote:
$1: name of the log file Why ??? From where is taken
Nowhere, I think. There is a different form of shell code which I think they tried to adapt from:

Code:
echo 'echo arg1 $1 arg2 $2 arg3 $3 arg4 $4' | sh -s -- a b c d

This runs with the result:
Code:
arg1 a arg2 b arg3 c arg4 d

...But it runs code from standard input, not -c, so can't be used with xargs.

Someone tried to adapt that into a form suitable for xargs and didn't know what those arguments were for, so left them dangling.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read variable inputs

I am trying to make an interactive script. Only problem my inputs are not read and correctly channeled. Please help: Here is my code #!/bin/sh PATHSCRIPT=/home/pp/tmp #if ; then # echo "Syntax : $0 input off lat sample" # exit 1 # fi echo "Choice of Graph" echo "1 -- Type... (5 Replies)
Discussion started by: newkid.7955
5 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

6. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

7. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

8. Shell Programming and Scripting

Please give your inputs !!!!

I am trying to extract two fields from the output of ifconfig command on one of my sun server . The output looks like : root@e08k18:/tmp/test# ifconfig -a lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 ce0:... (9 Replies)
Discussion started by: kpatel786
9 Replies

9. Shell Programming and Scripting

Inputs from a file

Hi, I have a shell script that has to taken inputs from a file say "Inputs". Now I take 2 inputs at a time. Suppose the Inputs file contains numbers like 2 3 4 5 Now I have a written a script for adding 2 numbers. When I run the script for first time 2 and 3 must be the inputs. When i run the... (4 Replies)
Discussion started by: sendhil
4 Replies

10. Shell Programming and Scripting

Multiple Inputs

Have tried the search, but nothing resembles what I'd like to accomplish. I am attempting to write a script that will allow the user to input a list of data at the command prompt, then the data is used by another script for processing. I am allowing the user a list of 10 members in order to... (4 Replies)
Discussion started by: douknownam
4 Replies
Login or Register to Ask a Question