How to execute a command on each line of output from another command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute a command on each line of output from another command?
# 1  
Old 02-10-2016
How to execute a command on each line of output from another command?

Hello Smilie

new to bash not to programming.

I have an on-going need to change the owning group on sets of files and directories from the one they were created with or changed to on update to the one they need to have going forward.

Code:
find {target_root} -group wrong_group

gets me a newline delimited list of files that associate with the wrong group

Code:
chgrp correct_group {file}

makes the correct change to an arbitrary file

I need the way execute the latter command on each line of output from the former command?

Please help me out?
# 2  
Old 02-10-2016
Try:
Code:
find {target_root} -group wrong_group -exec chgrp correct_group {} +

This command is more efficient, since it changes group ownership at once for as many pathnames as possible
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-10-2016
Wouldnt the easiest way be find's -exec action?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-10-2016
Yes perfect, thank you. What a useful option. Now to mark this solved...
# 5  
Old 02-10-2016
Up at the top right of the thread there's an "Edit Tags" link. Click and enter "solved".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. UNIX for Beginners Questions & Answers

One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum, I'm looking to expand the following command: INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server") Currently this command will check if my server is RedHat server using the grep -qi... (6 Replies)
Discussion started by: greavette
6 Replies

3. Shell Programming and Scripting

Modifying bash script to take each line in a file and execute command

I need to modify a bash script to to take each line in a file and execute command. I currently have this: #!/bin/bash if ; then echo "Lipsa IP"; exit; fi i=1 ip=$1 while ; do if ; then rand=`head -$i pass_file | tail -1` user=`echo $rand | awk '{print $1}'` pass=`echo $rand | awk '{print $2}'`... (3 Replies)
Discussion started by: galford
3 Replies

4. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

5. Shell Programming and Scripting

How to execute command remotely as sudo and save the output locally?

Hello , I am trying to run a NetBackup command in remote server. Also this command can only be run by root so I am using sudo . Also I want the output of the command locally in a file. The below command asked for password , ran successfully and showed Output on my local server screen ... (2 Replies)
Discussion started by: rahul2662
2 Replies

6. Shell Programming and Scripting

Read line by line and execute command

Hi ALL, I have a requirement like this. 1.GET ALL TABLE NAME (just table name) keep in file 2.Read line by line and get the count of table from tablename files. tablename detail has a sql statement "db2 select tabname from syscat.tables" (1 Reply)
Discussion started by: netdbaind
1 Replies

7. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

How to take input from the user from the command line and execute commands basedon that?

Hi, I am using solaris 10 and bash shell.Script execution follows below.Initially it will check whether a directory exists or not if does not exist it will create it.(This I have completed) Second step:I have four users say user1,user2,user3,user4.Script should prompt for the user id and... (11 Replies)
Discussion started by: muraliinfy04
11 Replies

9. Shell Programming and Scripting

Net::SSH::Perl->Execute any unix command & display the output in a proper form

Net::SSH::Perl ...... how to print the output in a proper format my $cmd = "ls -l"; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd("$cmd"); print $stdout; the script works fine, but i am unable to see the output... (2 Replies)
Discussion started by: gsprasanna
2 Replies

10. Shell Programming and Scripting

Execute command created from sql output

Hi, I've would like to create kill statement from sqlplus output. So, I've modified somoene's script : mud_kill_stmt=`sqlplus -s / as sysdba <<EOF select 'kill -9 ' || p.SPID || ';' statement from $process_view p, $session_ ......... and so on exit; EOF` $mud_kill_stmt | tr ... (1 Reply)
Discussion started by: Arkadiusz Masny
1 Replies
Login or Register to Ask a Question