我目前正在尝试编写一个小脚本来将所有 flac 文件转换为 mp3 文件。然而,当我尝试在所有音乐文件夹中设置递归时,我遇到了一些问题 - 脚本不断循环到当前目录 (.)
这是我目前拥有的:
#!/bin/bash
#---
# flacToMp3: Converts FLAC files in my originalFLAC folder into mp3 files
# and places them in an identical folder structure in my Music
# folder.
#---
function enterDIR {
for DIR in "$(find . -maxdepth 1 -type d)"; do #recurse into every directory below top-level directory
if [ "$DIR" == "." ]; then #avoid current directory infinite loop
continue
fi
cd "$DIR/"
enterDIR
done
createDirectory
convertFLAC
}
function …Run Code Online (Sandbox Code Playgroud) $ cat test15.sh
#!/bin/bash
# extracting command line options as parameters
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
$
$ ./test15.sh -a -b -c -d
Found the -a option
Found the -b option
Found the -c option
-d is not an option
$
Run Code Online (Sandbox Code Playgroud)
-d …
我正在尝试使用 cron 作业查看电池何时低于给定阈值,然后发送电池严重通知。但是,当我让 cron 作业每分钟执行一个脚本,并让脚本向我发送通知时,它不起作用。
为了确保这不是脚本的权限问题或导致 cron 作业无法运行的问题,我让脚本创建了一个文件,并且它起作用了。这是 crontab 条目:
* * * * * /home/aravk/test.sh
Run Code Online (Sandbox Code Playgroud)
而且,为了简化问题,这些是以下内容test.sh:
* * * * * /home/aravk/test.sh
Run Code Online (Sandbox Code Playgroud)
但是,没有显示通知。当我手动执行该脚本时,它确实有效。我还尝试通过将 crontab 条目更改为DISPLAY来设置环境变量,但它仍然不起作用。如何从 cron 作业执行的脚本发送通知?如果相关,我在 Arch Linux 上。:0* * * * * export DISPLAY=:0 && /home/aravk/test.sh
我已经编写了下面的脚本,但是当我从它所在的目录(以及文件)运行它时
bash xmlimport-magento2.sh
我收到此错误消息:
xmlimport-magento2.sh: line 7: syntax error near unexpected token $'do\r''
mlimport-magento2.sh: line 7: do
#!/bin/bash
if exists *.ZIP
then
# disable autoindexing
#php -f "$MAGEPATH"shell/indexer.php -- --mode-manual all
for file in `ls *.ZIP`
do
# Backup zip file
cp $file /backups
# Extract XML files
unzip -P web $file
# Delete zip file
rm -f $file
# rename xml files
# hier komt procedure die controleert of bestand xxx*basic.xml bestaat en zo ja dan wordt dit bestand …Run Code Online (Sandbox Code Playgroud) 要查看人类可读的输出,我可以使用以下命令 – du 命令 -h 选项:以人类可读的格式(例如,1K、234M、2G)显示大小。
$ du -hsx * | sort -rh | head -10
Run Code Online (Sandbox Code Playgroud)
示例输出看起来像
4.4G Desktop
3.8G Downloads
149M en-GB
146M Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
95M scala-2.11.4.deb
20M gawk-4.1.1
4.5M linux-dash
3.9M yii-1.1.13.e9e4a0.tar.gz.1
3.9M yii-1.1.13.e9e4a0.tar.gz
Run Code Online (Sandbox Code Playgroud)
如何使用Find命令获取可读文件大小?
示例:要跳过目录只显示文件,请使用以下命令
find . -type f -printf '%s %p\n'| sort -nr | head -4
Run Code Online (Sandbox Code Playgroud)
给我:
185016320 ./Desktop/gdb-7.9.tar
153300495 ./Downloads/apache-storm-1.0.0.tar.gz
152847886 ./Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
98756608 ./scala-2.11.4.deb
Run Code Online (Sandbox Code Playgroud)
我试图让文件大小显示为 185M、153M 等..(更具可读性)
根据字段拆分、双引号和for 循环的 POSIX 标准以及以下示例,我无法理解为什么echo "$s"在数字之间使用双引号显示新行,而在数字之间使用双引号for x in "$s"不显示新行. 有人可以澄清一下吗?
$ s=$(seq 1 4)
$ echo $s
1 2 3 4
$ echo "$s"
1
2
3
4
$ for x in $s; do echo $x; done
1
2
3
4
$ for x in "$s"; do echo $x; done
1 2 3 4
Run Code Online (Sandbox Code Playgroud) 如果我在命令行执行我的 find 命令,它工作正常,但如果我在脚本中尝试它,我会收到此错误。我运行命令
FILTER=" | grep -v \"test\""
files=`find . -type f -regex \".*$ext\" $FILTER`
Run Code Online (Sandbox Code Playgroud)
如果我做
echo "find . -type f -regex \".*$ext\" $FILTER"
Run Code Online (Sandbox Code Playgroud)
它输出
find . -type f -regex ".*.cpp" | grep -v "test"
Run Code Online (Sandbox Code Playgroud)
在命令行上工作正常。我怎样才能让它在脚本中工作?我也尝试转义 *,但得到相同的错误。
我也注意到
find . -type f -regex \".*$ext\"
Run Code Online (Sandbox Code Playgroud)
在我的 shell 脚本中运行时没有输出,我不知道为什么,因为就像上面一样,如果我在命令行运行它,我会得到一个 .cpp 文件列表。
我有这个简短的 if-then-else 脚本:
for INV in "$(ls np4178/*pdf)" ;\
do INVNUMB="$(pdfgrep -ho 'IN[0-9]{6,6}' $INV)" ; \
if [[ -z ${INVNUMB+x} ]]; \
then \
echo "\n$INVNUMB" ; \
else \
echo "wrong \n$INVNUMB" ; \
fi ; \
done
Run Code Online (Sandbox Code Playgroud)
产生这个:
wrong \nIN353886
IN353897
IN353905
IN353910
IN353902
IN353864
IN353875
IN353840
IN353862
IN353922
IN353739
IN353876
IN353920
Run Code Online (Sandbox Code Playgroud)
但是,如果我对else语句进行更改,则会得到以下信息:
for INV in "$(ls np4178/*pdf)" ;\
do INVNUMB="$(pdfgrep -ho 'IN[0-9]{6,6}' $INV)" ; \
if [[ -z ${INVNUMB+x} ]]; \
then \
echo "\n$INVNUMB" …Run Code Online (Sandbox Code Playgroud) 我怎样才能编写一个awk可以根据输入文件中的行所要求的内容进行加法或减法的脚本?\xc2\xa0\n我\xc2\xa0弄清楚如何仅进行加法或减法,\n但不是非此即彼在一个脚本中。
例如,我想要这个输入:
\nADD 5,10,20\nSUB 30,5,20\nRun Code Online (Sandbox Code Playgroud)\n产生这个输出:
\n35\n-55\nRun Code Online (Sandbox Code Playgroud)\n这是我到目前为止所写的:
\n#!/bin/awk \nBEGIN {\n FS=","\n}\n\n{\n for(i=1;i<=NF;i++) \n sum+=$i; \n print sum; sum=0}\nRun Code Online (Sandbox Code Playgroud)\n但是,显然,它只进行加法,甚至没有获得所有数字。
\n您好,我目前正在尝试执行以下代码。我在变量 DATE 中存储了一个字符串,其格式为 YYYY/MM/DD。我尝试使用 cut 命令提取年份。我收到一条错误消息,指出它不是文件或目录。我可以进行修改或采取不同的方法吗?
for file in ~/filesToSort/*
do
DATE=$(head -1 $file | tr "-" "/")
echo "${DATE}"
YYYY=$(cut -c1-4 accounts $DATE)
#echo "${YYYY}"
done
Run Code Online (Sandbox Code Playgroud)
谢谢
scripting ×10
bash ×5
shell ×3
find ×2
shell-script ×2
awk ×1
command-line ×1
cron ×1
disk-usage ×1
files ×1
freebsd ×1
linux ×1
posix ×1