LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2011, 04:47 PM   #1
alpha_b_gamma
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
What does the following program do?


for file in *nii.gz; do for loop

file_stem=`basename $file .nii.gz`
mv $file $file_stem.old.nii.gz

done
 
Old 04-27-2011, 04:57 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Lightbulb

Quote:
Originally Posted by alpha_b_gamma View Post
for file in *nii.gz; do for loop

file_stem=`basename $file .nii.gz`
mv $file $file_stem.old.nii.gz

done
First Line
It searches for files which has *.nii.gz (like abcnii.gz, 1a232nii.gz, e.t.c.) or in other words files ending with nii.gz

Second Line
It searches for text after "/" in file names. Though I think this is not required.

For E.g.
[root@vikas027 ~]# basename /usr/sbin/lsof
lsof

Third Line
Rename all files to .old.nii.gz

For e.g.
1a232.nii.gz 1a232.old.nii.gz
 
Old 04-27-2011, 07:34 PM   #3
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by vikas027 View Post
Second Line
It searches for text after "/" in file names. Though I think this is not required.
Second line is required because it removes .nii.gz strings from the file names allowing to put .old strings before .nii.gz strings in the new files names.
 
Old 04-27-2011, 09:53 PM   #4
alpha_b_gamma
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
I understood the first line

But what is done by:

file_stem=`basename $file .nii.gz`



could you explain part by part

what does file_stem do?
what does basename $file .nii.gz do?

about this
mv $file $file_stem.old.nii.gz

should it not rename a file ( for eg: asdf.nii.gz) to asdg_stem.old.nii.gz

?

Thank you for the help.
 
Old 04-28-2011, 02:51 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by alpha_b_gamma View Post
I understood the first line

But what is done by:

file_stem=`basename $file .nii.gz`
Here is a brief explanation. This line sets a variable called file_stem and assigns a value which is the output of a command. Note the backticks around the basename command: they bring to command substitution that is the output of the command inside backticks is substituted and assigned to the file_stem variable.

Regarding the basename command, the syntax is
Code:
basename NAME [SUFFIX]
and it prints NAME with any leading directory components removed. If specified (as in your example) also remove a trailing SUFFIX.

Let me give you a little advice: you should definitively read some good tutorial about Shell Scripting, since your questions are very basic and they demonstrate you did not even consider to open and read a piece of documentation. Please, believe me: it's worth the effort, since shell scripting can spare a lot of time if used with awareness. This one should be a good starting point: http://www.linuxcommand.org/tlcl.php. Hope this really helps!
 
Old 04-28-2011, 08:30 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by colucix View Post
the output of the command inside backticks is substituted and assigned to the file_stem variable.
Unless the script will be run in shells other than bash, it's strongly recommended to use $(command) instead of `command`.

The new syntax allows easier nesting, cannot be confused with single quotes, and backslashes inside are treated as they normally would outside of the $() (the backtick syntax uses backslashes for nesting).
 
Old 04-28-2011, 10:15 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by MTK358 View Post
Unless the script will be run in shells other than bash, it's strongly recommended to use $(command) instead of `command`.
I agree. The backticks syntax is used only for compatibility reasons, nowadays.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone program a SH / Bash program with Zenity to play radio based on a site? frenchn00b Programming 2 02-25-2011 04:16 PM
LXer: Can't Program, won't Program? Then Mash the Web with Mozilla's Ubiquity LXer Syndicated Linux News 0 05-01-2010 12:30 PM
open source dvd/cd copy program? burning on the fly program? vieya Linux - Software 3 12-14-2009 09:16 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration