简体   繁体   English

Bash shell脚本

[英]Bash shell scripting

Recently I started working with shell scripting in Linux (newbie). 最近我开始在Linux(新手)中使用shell脚本。 I wanted to know the commands or the procedure to change from one folder to another within the loop? 我想知道在循环中从一个文件夹更改为另一个文件夹的命令或过程? I mean how can I call different folders inside the executing loop? 我的意思是如何在执行循环中调用不同的文件夹? Presently, I'm renaming all the folders with no's say 1,2,3..... and using these inside the loop. 目前,我正在重命名所有的文件夹,没有说1,2,3 .....并在循环中使用这些。 I know this is not a good way to use scripting. 我知道这不是使用脚本的好方法。

set k=1
while ( $k <= 17)

## initial path set
    cd /home/naren/Documents/BASELINE_INSP/FreeSurfer/$k/RSFC

Each time 'k' updates, move to different folders since I named all folders as no's. 每次'k'更新,移动到不同的文件夹,因为我将所有文件夹命名为no。

Try this: 尝试这个:

#!/bin/bash
for ((i=1; i <= 17; i++)); do
    #Changing folder inside the loop
    cd "/home/naren/Documents/BASELINE_INSP/FreeSurfer/${i}/RSFC"
    #Here you can execute something more if you want
done
#
# Start with ID equal to zero and increment
#
ID=0

#
# then use a say a while loop
#
while [ ${ID} -lt 5 ]; do 
    echo "my dir is /here/${ID}/there"; 
    ((ID++)); 
done

I mean how can I call different folders inside the executing loop? 我的意思是如何在执行循环中调用不同的文件夹?

Preferably, if you want to loop for all folders matching a certain pattern (which needn't contain numbers), just specify the pattern in a for loop: 优选地,如果要循环匹配特定模式(不需要包含数字)的所有文件夹,只需在for循环中指定模式:

for folder in /home/naren/Documents/BASELINE_INSP/FreeSurfer/*/RSFC
do  cd "$folder"
    # do something there
done

In order to write your code without numeration, you can use ls command which lists the files in the current directory or use ls [folder_name] to list the files in a folder, In order to assign the result to file use $ operator 为了编写没有编号的代码,可以使用ls命令列出当前目录中的文件或使用ls [folder_name]列出文件夹中的文件,为了将结果分配给文件使用$ operator
Edit: 编辑:
since ls has some pitfalls as @randomir mentioned it is better to use iteration as following: 因为ls有一些陷阱,因为@randomir提到最好使用迭代如下:

 for f in ./* do //do something with $f done 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM