LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What does the following program do? (https://www.linuxquestions.org/questions/linux-newbie-8/what-does-the-following-program-do-877471/)

alpha_b_gamma 04-27-2011 04:47 PM

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

vikas027 04-27-2011 04:57 PM

Quote:

Originally Posted by alpha_b_gamma (Post 4338514)
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

w1k0 04-27-2011 07:34 PM

Quote:

Originally Posted by vikas027 (Post 4338523)
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.

alpha_b_gamma 04-27-2011 09:53 PM

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.

colucix 04-28-2011 02:51 AM

Quote:

Originally Posted by alpha_b_gamma (Post 4338724)
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! :)

MTK358 04-28-2011 08:30 AM

Quote:

Originally Posted by colucix (Post 4338973)
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).

colucix 04-28-2011 10:15 AM

Quote:

Originally Posted by MTK358 (Post 4339328)
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.


All times are GMT -5. The time now is 12:06 AM.