简体   繁体   English

solaris简单的bash脚本

[英]solaris simple bash script

I'm trying to execute this simple script in solaris. 我正在尝试在solaris中执行这个简单的脚本。 I want to sort(numeric) the filenames of the files in source directory and copy the file one by one to another directory. 我想对源目录中文件的文件名进行排序(数字化),并将文件逐个复制到另一个目录。 And, I want to print a message after copying every 100 files. 并且,我想在复制每100个文件后打印一条消息。

#!/bin/bash

count=0

for i in `ls | sort -n`
do 
 cp $i ../target
 count = $((count+1))
 if[ $count%100 -eq 0 ] 
 then
    echo $count files copied
    sleep 1
 fi

done

this is not working. 这不起作用。 I tried different things after searching in net. 我在网上搜索后尝试了不同的东西。
I get errors like these - 我得到这样的错误 -

syntax error at line 8: '(' unexpected. 第8行的语法错误:'('意外。
syntax error at line 10: 'then' unexpected. 第10行的语法错误:'然后'意外。
syntax error at line 13: 'fi' unexpected etc. 第13行的语法错误:'fi'意外等

What is the problem with this script? 这个脚本有什么问题?

bash version - GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10) bash版本 - GNU bash,版本3.00.16(1)-release(sparc-sun-solaris2.10)

The basic problem with the script is spacing. 脚本的基本问题是间距。 You have spaces where you shouldn't have them: 你有空间,你不应该有它们:

(wrong)  count = $((count+1))
(right)  count=$((count+1))
(better) ((count++))

and you're missing spaces where you need them: 你错过了你需要它们的空间:

(wrong)  if[ $count%100 -eq 0 ]
(right)  if [ $((count % 100)) -eq 0 ]
(better) if ((count % 100 == 0))

count = $((count+1)) tries to run the command count passing it two arguments, = and the value of count+1 . count = $((count+1))尝试运行命令count传递两个参数, =count+1的值。 if[ ... tries to run the command if[ because [ is a valid word character; if[ ...试图运行命令, if[因为[是一个有效的单词字符; it doesn't automatically start a new token. 它不会自动启动新令牌。

Having said all that, I'm puzzled by the unexpected ( error message. Could your bash be too old to recognize $(( syntax? Possibly. It's a very old bash. 说完这一切之后,我对这个unexpected (事情感到困惑unexpected (错误消息。你的bash可能太老而无法识别$((语法?可能。这是一个非常古老的bash。

count=$((count+1))

if [ `echo $count % 100 | bc` -eq 0 ]

Make these corrections. 进行这些更正。

Edit: Please try 编辑:请尝试

count=`expr $count + 1`

I see a few errors here. 我在这里看到一些错误。 First, you need double quotes around $i in case they have special characters. 首先,如果它们有特殊字符,你需要在$i附近$i双引号。

Second, you shouldn't ever use 其次,你不应该使用

for i in `ls | sort -n`

Instead, try the following 相反,请尝试以下方法

ls -1 | sort -n | while read i

Third, change your if statement to 第三,将你的if语句改为

if ((count%5 == 0))

The (( syntax is bash is made just for integer math. ((语法是bash只用于整数数学。

Fourth, change your counter increment to 第四,将你的计数器增量改为

((count++))

This is more concise. 这更简洁。 Also, the space in your version may break things. 此外,您的版本中的空间可能会破坏事物。 Remember, spaces matter. 请记住,空间很重要。

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

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