LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 05: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, 05:57 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
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, 08:34 PM   #3
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
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, 10: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, 03:51 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 09:30 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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, 11:15 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05: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 01:30 PM
open source dvd/cd copy program? burning on the fly program? vieya Linux - Software 3 12-14-2009 10:16 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-15-2003 12:18 AM


All times are GMT -5. The time now is 04:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration