English 中文(简体)
采用双壳书写方式,通过理由清单进行检索?
原标题:Iterating through argument list using for loop in shell script?

我想通过辩论显示向我手稿提供的文件内容。 我需要利用正常的((a) lo和NOT(x) a。

This is my code. How should i put the file name correctly for that cat command?

for (( i=1;$i<=$#;i=$i+2 ))
do
    cat  $ $i   #display the contents of the file currently being traversed
done
最佳回答

你们可以使用这样的东西:

for (( i=1;$i<=$#;i=$i+1 ))
do
    cat ${!i}  #display the contents of the file currently being traversed
done

相应的手册部分:

If the first character of parameter is an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

问题回答

诉讼的正常行为是回避论点。 3. 对所有论点进行回顾:

#!/bin/sh -e
for x; do cat $x; done

你可以稍作修改,只能根据你在很多不同场合所做的一切其他论点来做。 一种可能性是:

for x; do expr $(( i++ )) % 2 > /dev/null || cat "$x"; done

如果你坚持使用非标准表格,你可以使用“i!var} bashism”,而后者在绝大多数炮弹中将失败,或者你可以使用<代码>eval<>。

for (( i = 1; i <= $#; i += 2 ))
do
    eval cat  "$ $i"
done




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签