在以下脚本中,在将文件分配给名为' byteNuevo'的变量后,我得到文件的大小(以字节为单位)。然后进行比较,如果' byteAnterior'> 0减去' byteNuevo - byteAnterior'否则赋值为0.
问题在于' byteAnterior'没有价值,如果你之前的价值是“my_text.txt”,并希望我解释过这个脚本的作用是显示它之前的价值' my_text.txt'那现在......
我的例子:
echo -e "\n- Starting 1.sh script--\n"
if [[ ! -f my_text.txt ]]; then > my_text.txt; fi
byteOLD="$(cat somefile.storing.byteOLD.value.txt)"
echo "-- OK: so far byteOLD is: $byteOLD";
byteModified=$(stat -c'%s' my_text.txt)
echo "-- Now after modifying my_text.txt on the fly, bytes in variable byteModified are: $byteModified";
((byteDiferencia = (byteOLD > 0) ? byteModified - byteOLD : 0))
echo " -- byteDiferencia is set to = '$byteDiferencia'"
答案 0 :(得分:0)
看看这有帮助吗?
案例1-我正在检查byteAnterior永远不会小于0 -OR我将错误地输出消息。
[jenkins@giga_akssb12 tomcat]$ cat 1.sh ; cat my_text.txt; ./1.sh ;
#!/bin/bash
echo -e "\n- Starting 1.sh script--\n"
#As you didn't mention how u r setting byteAnterior, im assuming user will pass a parameter to this script.
byteAnterior="$1"
if [[ $byteAnterior -lt 0 || -z "$byteAnterior" ]]; then echo -e "\n -- Pass parameter value >= 0.\n"; exit 111; fi
byteNuevo=$(stat -c'%s' my_text.txt)
echo "-- byteAnterior : $byteAnterior"
echo "-- byteNuevo : $byteNuevo"
echo
#Ternary operator use
((byteDiferencia = (byteAnterior > 0) ? byteNuevo - byteAnterior : 0))
echo " -- byteDiferencia is set to = '$byteDiferencia'"
Hi shenzi.
ed?
- Starting 1.sh script--
-- Pass parameter value >= 0.
[jenkins@giga_akssb12 tomcat]$
[jenkins@giga_akssb12 tomcat]$ ./1.sh 0;
- Starting 1.sh script--
-- byteAnterior : 0
-- byteNuevo : 16
-- byteDiferencia is set to = '0'
[jenkins@giga_akssb12 tomcat]$
[jenkins@giga_akssb12 tomcat]$
[jenkins@giga_akssb12 tomcat]$ ./1.sh 12;
- Starting 1.sh script--
-- byteAnterior : 12
-- byteNuevo : 16
-- byteDiferencia is set to = '4'
[jenkins@giga_akssb12 tomcat]$
情况2.如果你想将byteAnterior设置为0或小于0的东西或除了> = 1之外的有效数字以外的任何东西。然后COMMENT out check(if语句),现在byteDifferntia将被设置为0三元运算符。运行1.sh并传递-1作为第一个参数,你会看到:
#!/bin/bash
echo -e "\n- Starting 1.sh script--\n"
#As you didn't mention how u r setting byteAnterior, im assuming user will pass a parameter to this script.
byteAnterior="$1"
#if [[ $byteAnterior -lt 0 || -z "$byteAnterior" ]]; then echo -e "\n -- Pass parameter value >= 0.\n"; exit 111; fi
byteNuevo=$(stat -c'%s' my_text.txt)
echo "-- byteAnterior : $byteAnterior"
echo "-- byteNuevo : $byteNuevo"
echo
#Ternary operator use
((byteDiferencia = (byteAnterior > 0) ? byteNuevo - byteAnterior : 0))
echo " -- byteDiferencia is set to = '$byteDiferencia'"
Hi shenzi.
ed?
- Starting 1.sh script--
-- byteAnterior : -1
-- byteNuevo : 16
-- byteDiferencia is set to = '0'
答案 1 :(得分:0)
[giga@aks tomcat]$ rm my_text.txt
[giga@aks tomcat]$ ls -l my_text.txt
ls: my_text.txt: No such file or directory
[giga@aks tomcat]$
[giga@aks tomcat]$ cat 1.sh
#!/bin/bash
echo -e "\n- Starting 1.sh script--\n"
## Create a blank file if it doesn't exist.
## This will make byteOLD to hold 0 value. In future runs, it'll be append instead of overwriting.
if [[ ! -f my_text.txt ]]; then > my_text.txt; fi
byteOLD=$(stat -c'%s' my_text.txt)
echo "-- OK: so far byteOLD is: $byteOLD";
## Add something to the file in append mode >>.
## This will make byte2 to hold how much bytes you put there including last time if file exists.
echo -e "\n1\n2\n3\n4\n5\n6\n7\n8\n9\10\n11111111\n22222222222\n" >> my_text.txt
byteModified=$(stat -c'%s' my_text.txt)
echo "-- Now after modifying my_text.txt on the fly, bytes in variable byteModified are: $byteModified";
#Ternary operator use
((byteDiferencia = (byteOLD > 0) ? byteModified - byteOLD : 0))
echo " -- byteDiferencia is set to = '$byteDiferencia'"
[giga@aks tomcat]$
[giga@aks tomcat]$ ./1.sh
- Starting 1.sh script--
-- OK: so far byteOLD is: 0
-- Now after modifying my_text.txt on the fly, bytes in variable byteModified are: 44
-- byteDiferencia is set to = '0'
[giga@aks tomcat]$
[giga@aks tomcat]$ ls -l my_text.txt
-rw-r--r-- 1 jenkins dmadmin 44 Jun 12 15:17 my_text.txt
[giga@aks tomcat]$
[giga@aks tomcat]$ ./1.sh
- Starting 1.sh script--
-- OK: so far byteOLD is: 44
-- Now after modifying my_text.txt on the fly, bytes in variable byteModified are: 88
-- byteDiferencia is set to = '44'
[giga@aks tomcat]$
[giga@aks tomcat]$ ls -l my_text.txt
-rw-r--r-- 1 jenkins dmadmin 88 Jun 12 15:17 my_text.txt
[giga@aks tomcat]$
[giga@aks tomcat]$
[giga@aks tomcat]$ ./1.sh
- Starting 1.sh script--
-- OK: so far byteOLD is: 88
-- Now after modifying my_text.txt on the fly, bytes in variable byteModified are: 132
-- byteDiferencia is set to = '44'
[giga@aks tomcat]$