Solved

Convert Batch Script to Bash

Posted on 2014-01-15
6
481 Views
Last Modified: 2014-01-17
Hello Experts,

I would like to get this batch code converted to bash,

for /f "tokens=1,2" %i in ('ucommand -list ^| find /i "_ud"') do @command -f %j -user %i -delete

Thanks,
0
Comment
Question by:TunerML
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39783711
Assuming you already converted the "ucommand" and "command" programs, try this:

for line in `ucommand -list | grep -i "_ud"`
do
  i=`echo $line | cut -d" " -f1`
  j=`echo $line | cut -d" " -f2`
  command -f $j -user $i -delete
done  

Open in new window

0
 
LVL 9

Author Comment

by:TunerML
ID: 39783826
Any possible way for it to be a one-liner? or would I need to put this in a. sh file and set chmod +x?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 39783929
ucommand -list | grep -i "_ud" | xargs -n2 -sh -c 'command -f $2 -user $1 -delete' argv0

Open in new window


or

ucommand -list | grep -i "_ud" | while read i j ; do command -f $j -user $i -delete; done

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Author Comment

by:TunerML
ID: 39785136
Tried all of the above scripts and encountered errors even with some tweaking though my scripting knowledge in Unix is severely limited.

Okay so a sample output of the ./command -list would be:

"Update information...

Users:
user1 ???_???_UD year.mmdd
user2 ???_???_UD year.mmdd
user3 ???_???_UD year.mmdd
user4 ???_???_UD year.mmdd
user5 ???_???_UD year.mmdd
user6 ???_???_UD year.mmdd
user7 ???_???_UD year.mmdd

Devices:
device1 ???_???_UD year.mmdd
device2 ???_???_UD year.mmdd

..."

What I'm trying to achieve is a script that allows me to run this command for each user;

./command -f ???_???_UD -user user1 -delete
./command -f ???_???_UD -user user2 -delete
./command -f ???_???_UD -user user3 -delete

and so fourth.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 300 total points
ID: 39785568
Works for me:
==> ucommand -list | grep -i "_ud" | while read u f x;do echo "./command -f $f -user $u -delete"; done
./command -f ???_???_UD -user user1 -delete
./command -f ???_???_UD -user user2 -delete
./command -f ???_???_UD -user user3 -delete
./command -f ???_???_UD -user user4 -delete
./command -f ???_???_UD -user user5 -delete
./command -f ???_???_UD -user user6 -delete
./command -f ???_???_UD -user user7 -delete
./command -f ???_???_UD -user device1 -delete
./command -f ???_???_UD -user device2 -delete

Open in new window

PS: Remove the echo and quotes to execute the commands...
0
 
LVL 9

Author Closing Comment

by:TunerML
ID: 39788423
Wouldn't have thought to remove the echo or quotes, Thanks guys.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question