bash shell循环的疑问

2019-03-25 13:31|来源: 网路

下面两段代码
第一段,无法正确统计文件行数,count在循环中的计算,出了循环就无效了
第二段,count2出了循环依然有效
初学shell,不太明白这是为什么,希望大家指点一下
ps:第一段代码应该如何修改,才能达到第二段的效果
#!/bin/bash
count=0

cat "$1" | while read line
do
    count=$[ $count + 1 ]
done

echo "$count line(s) in all."
#-----------------------------------------
count2=0;

for var in a b c
do
    count2=$[ $count2 + 1 ]
done
echo "$count2 line(s) in all."

相关问答

更多
  • 1.是,其他的shell还有csh、zsh、fish等。bash用得比较多,shell编程很强,不过fish和zsh的用户交互貌似做的很好。 2.一个标准的组成部分吧……就像python。套件一般用来指Linux发行版。
  • 1.是 2.区别大,你会写bash shell不代表你会写c shell,不过我觉得你会写一个shell脚本 另一种你上手也很快,起码读懂应该问题不大。你可以先学bash shell因为用的比较多 而且可移植性强,bash 是shell的一种。 3.属于shell,只安装了bash shell就可以运行bash shell脚本,并不是针对某个系统或者发行版本。 shell这种东西可以随意安装的。你用习惯哪个shell,就算初装系统里没有,你也可以单独安装一个 4.给你推荐两本书 《Bash shell高级脚 ...
  • #!/bin/bash for ((i=1;i<10;i++));do for((j=1;j<$i;j++));do echo "hello world" done done
  • :是一个shell内置的Null command 。 您可以在终端输入help : :: :: : Null command. No effect; the command does nothing. Exit Status: Always succeeds. 它看起来像/bin/true 。 : is a shell builtin Null command. You can type help : in the terminal: :: : Null com ...
  • 我对你的脚本有一些建议。 为什么不将数组用于文件并将显示逻辑移动到函数中? 使用数组的好处是你可以轻松地将它扩展到多个文件(在当前情况下超过2个) 将显示逻辑分离为函数将帮助您轻松调试和更改逻辑 #!/bin/bash #First param is the file to read to check for codes #Second param is the file to read the codes from function display_info() { #Your dis ...
  • 你看到的不是shell脚本,而是Makefile。 SHELL是Makefile变量,用于设置要使用的首选shell。 默认设置为/bin/sh ,因此如果您需要Bash功能,则可能必须设置此变量。 文档在这里 。 如果它是shell脚本,SHELL将是一个指向用户默认shell的环境变量。 What you saw isn't a shell script, but a Makefile. SHELL is the Makefile variable that sets the preferred she ...
  • 你不能嵌套[[那样(你想要分组( ... ) )。 你需要[[和第一个参数之间以及最后一个参数和]]之间的空间]] 。 尝试: [[ -f ${OFSR_CFG}/LoadVariables_SMITH.bash && ("${SMITH_SCRIPT}" = "MAIN" || "${SMITH_SCRIPT}" = "RELOAD") && !(-f ${OFSR_CFG}/LoadVariables_MI.bash) ]] You can't nest [[ like that (you want ...
  • 哇,有一个更简单,更简洁的方法来实现这一点,而不必弄乱IFS变量或使用数组。 您可以使用“for”来执行此操作: 首先,我创建了一个与您的结构相同的文件: $ cat file id|lastName|Douglas|gender|birthday|joinDate|IP|browser id|lastName|Tim|gender|birthday|joinDate|IP|browser id|lastName|Andrew|gender|birthday|joinDate|IP|browser id|l ...
  • 转到bash循环,因为shell_exec函数只被调用一次。 它会比多次调用shell_exec更快。 启用exec,shell_exec等功能本身就是一个巨大的安全问题。 如果有人设法在您的服务器中上传PHP shell,那么他可以破解您的服务器。 Go for bash loop, because shell_exec function is called only once. It will be faster than calling shell_exec multiple times. Enabl ...
  • 在bash使用typeset -f 。 在zsh , functions只是同一命令的同义词。 Use typeset -f in bash. In zsh, functions is just a synonym for the same command.