我正在尝试运行包含字符串变量中的字符串变量的命令:
$ X="bash -c 'echo OK'" ; $X
OK': -c: line 1: unexpected EOF while looking for matching `''
为什么不起作用?如何正确修复/做它?
请注意,我需要将命令运行为$X
.
请注意,我需要echo OK
通过运行bash -c
。
我正在尝试运行包含字符串变量中的字符串变量的命令:
$ X="bash -c 'echo OK'" ; $X
OK': -c: line 1: unexpected EOF while looking for matching `''
为什么不起作用?如何正确修复/做它?
请注意,我需要将命令运行为$X
.
请注意,我需要echo OK
通过运行bash -c
。
我正在尝试找到一种方法,一个进度需要多长时间,所以我不会硬编码设定的时间长度。我试图在不添加任何新库的情况下做到这一点。pv 命令似乎很流行,但我的系统由于某种原因不支持它。我假设有一种方法 SIGINFO 可以获取此信息。我尝试做 awhile [ ! -z pgrep command ] do \\logic
让它在任务存在时运行。这是针对以下代码的 load%:
if [ "$selected_choice" == "Go Back" ]
then gsettings set org.gnome.desktop.peripherals.keyboard repeat false && echo -en "\e" #This is the escape sequence in the first menu.
else cd && cd $search_dir && jar cf $file $fileclass & bash ~/Bash_Files/Bash_Animations/load3.sh
fi
这是load3.sh的代码
#!/bin/bash
progressBarWidth=20
# Function to draw progress bar
progressBar () {
# Calculate number of fill/empty slots in the bar
progress=$(echo "$progressBarWidth/$taskCount*$tasksDone" | bc -l)
fill=$(printf "%.0f\n" $progress)
if [ $fill -gt $progressBarWidth ]; then
fill=$progressBarWidth
fi
empty=$(($fill-$progressBarWidth))
# Percentage Calculation
percent=$(echo "100/$taskCount*$tasksDone" | bc -l)
percent=$(printf "%0.2f\n" $percent)
if [ $(echo "$percent>100" | bc) -gt 0 ]; then
percent="100.00"
fi
# Output to screen
## https://unix.stackexchange.com/questions/664055/how-to-print-utf-8-symbols this is why %s and %b are used
## ORGINAL
##printf "\r["
##printf "%${fill}s" '' | tr ' ' ▉
##printf "%${empty}s" '' | tr ' ' ░
##printf "] $percent%% - $text "
#This is for GNU users as UNICODE charaters do not work on their terminal
printf "\r["
printf "%${fill}s" '' | sed 's/ /\o342\o226\o210/g'
printf "%${empty}s" '' | sed 's/ /\o342\o226\o221/g'
printf "] $percent%% - $text "
}
## Collect task count
taskCount=33
tasksDone=0
while [ $tasksDone -le $taskCount ]; do
# Do your task
(( tasksDone += 1 ))
# Can add some friendly output
# text=$(echo "somefile-$tasksDone.dat")
# Draw the progress bar
progressBar $taskCount $taskDone $text
sleep 0.01
done
echo
代码工作的逻辑是我只是尝试动态分配值taskCount
或taskDone
找到不同的方法来计算任务开始到完成。
我正在运行以下命令来为整个脚本文件夹添加别名:
ls | grep '\.sh$' | sed 's/\.sh$//' | awk '{print "alias "$1"=\"./"$0".sh\""}' | xargs -L1 bash
似乎有问题,bash
或者可能xargs
处理这个问题。别名不起作用。
使用 运行命令时echo
,我得到了预期的输出(或者几乎不确定为什么缺少双引号)
ls | grep '\.sh$' | sed 's/\.sh$//' | awk '{print "alias "$1"=\"./"$0".sh\""}' | xargs -L1 echo
输出:
alias test_script1=./test_script1.sh
alias test_script2=./test_script2.sh
单独运行时,这些命令按预期工作。
抱歉,如果我仍然是一个 bash 菜鸟。尝试通过一个命令来解决这个问题,我可以将其放入我的.zshrc
脚本目录中并引用它。
我对 FFMPEG 不太熟悉,我自己也尝试过多次这样做,但我什至无法弄清楚语法。所以我要向外询问比我聪明得多的人。我希望使用 FFMPEG 删除所有非英语选项,然后删除它们。之后,删除除第一个索引选项之外的所有英文选项。无论我怎么尝试,我都无法弄清楚这一点,我用头撞墙尝试了大约 3 个小时,最后一次尝试是ffmpeg -i "Episode 01 - Yaha-kui zaShunina.mkv" -map 0:v -map 0:s -map 0:m:language:eng -map:0:a:1? -codec copy "copy Episode 01 - Yaha-kui zaShunina.mkv"
这是我获得正确语法的唯一一个......但它仍然不太有效。有比我更聪明的人可以帮忙吗?另外,我更愿意保留所有视频,并确保它们是软字幕而不是烧录的 - 只是为了澄清。
我有一个脚本,假设是这样(显然,我真正的脚本要复杂得多):
echo A
echo a
echo B
echo b
echo C
echo c
echo D
echo d
有没有一种简单的方法可以让线条并排?
A a
B b
C c
D d
或者如果我将其分成两个文件然后合并它们会更好吗?我该怎么办呢?
我在文本文件中记录了以下格式的记录:
Record1
Record1
Record1
Record1
Record2
Record2
Record2
Record2
...
我需要将此文件转换为 CSV,方法是用逗号替换前 3 个换行符并删除第 4 个换行符,结果如下所示:
Record1,Record1,Record1,Record1
Record2,Record2,Record2,Record2
...
在 Bash 中执行此操作的最佳方法是什么,其中最好的方法更多的是健壮性而不是性能 - 该任务不会经常完成,但确实需要允许值包含除空格和换行符之外的几乎任何字符。
我正在尝试在终端中制作一个更好的菜单来提高我的 bash 技能。如果我对所有值进行硬编码,我就能够做到这一点。但是,如果选项数量不同,那么我不确定最佳实践是什么。例如,文件夹中可能有 *.java 程序的数量。我应该有一个功能来构建菜单中选项的功能,还是有更好的对话框方法?
#/bin/bash
E='echo -e';e='echo -en';trap "R;exit" 2
ESC=$( $e "\e")
TPUT(){ $e "\e[${1};${2}H";}
CLEAR(){ $e "\ec";}
CIVIS(){ $e "\e[?25l";}
DRAW(){ $e "\e%@\e(0";}
WRITE(){ $e "\e(B";}
MARK(){ $e "\e[7m";}
UNMARK(){ $e "\e[27m";}
R(){ CLEAR ;stty sane;$e "\ec\e[37;44m\e[J";};
HEAD(){ DRAW
for each in $(seq 1 13);do
$E " x x"
done
WRITE;MARK;TPUT 1 5
$E "BASH SELECTION MENU ";UNMARK;}
i=0; CLEAR; CIVIS;NULL=/dev/null
FOOT(){ MARK;TPUT 13 5
printf "ENTER - SELECT,NEXT ";UNMARK;}
ARROW(){ read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]];then echo up;fi
if [[ $key = $ESC[B ]];then echo dn;fi;}
M0(){ TPUT 4 20; $e "Run";}
M1(){ TPUT 5 20; $e "Compile and Run";}
M2(){ TPUT 6 20; $e "Make .Jar File";}
M3(){ TPUT 7 20; $e "-Xlint";}
M4(){ TPUT 8 20; $e "";}
LM=4
MENU(){ for each in $(seq 0 $LM);do M${each};done;}
POS(){ if [[ $cur == up ]];then ((i--));fi
if [[ $cur == dn ]];then ((i++));fi
if [[ $i -lt 0 ]];then i=$LM;fi
if [[ $i -gt $LM ]];then i=0;fi;}
REFRESH(){ after=$((i+1)); before=$((i-1))
if [[ $before -lt 0 ]];then before=$LM;fi
if [[ $after -gt $LM ]];then after=0;fi
if [[ $j -lt $i ]];then UNMARK;M$before;else UNMARK;M$after;fi
if [[ $after -eq 0 ]] || [ $before -eq $LM ];then
UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;}
INIT(){ R;HEAD;FOOT;MENU;}
SC(){ REFRESH;MARK;$S;$b;cur=`ARROW`;}
ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT
while [[ "$O" != " " ]]; do case $i in
0) S=M0;SC;if [[ $cur == "" ]];then R;~/Home/Bash_Menu/./jrun.sh;ES;fi;;
1) S=M1;SC;if [[ $cur == "" ]];then R;~/Home/Bash_Menu/./jcompile.sh;ES;fi;;
2) S=M2;SC;if [[ $cur == "" ]];then R;~/Home/Bash_Menu/./jjar.sh;ES;fi;;
4) S=M6;SC;if [[ $cur == "" ]];then R;exit 0;fi;;
esac;POS;done
上面是我所拥有的硬编码示例。每个选项都基于 M#(){ TPUT 4 20; 存在 $e“运行”;}。我尝试创建
##Files to be added as menu options
options=( $(cd Java_Files && find . -maxdepth 1 -type f -name "*.java") )
##code that sorts
readarray -t sorted_options < <(IFS=$'\n'; sort <<<"${options[*]}")
...(skipping irreverent code)...
#Where the M0(){} part exists i replaced it with this but it does not seem to quiet work
for(( x=0; x<${#sorted_options[@]}; x++ )); do
M$x+"()"{ TPUT x+4 20; $e "$opt" }
LM=${#sorted_options[@]}
done
我将这一更改与“for every in $(M#()); do”下方的代码结合起来,其中对每个选项进行了硬编码,如“ 1) S=M1;SC;if [[ $cur == "" ]];然后 R;$e "\n$(ifconfig )\n";ES;fi;;"
for each in $(M#()); do
S=M#;SC;if [[ $cur == "" ]];then R;$e "${#sorted_options[@]}";ES;fi;;
esac;POS;done
我有一个国际会议视频,其中包含两种口语,即视频中混有英语和汉语句子。我想通过命令行删除中文部分。
首先,使用生成字幕文件whisper
whisper myvideo --model large --language en
字幕文件包含语言和时间
1
00:00:00,000 --> 00:00:04,220
if you are not concerned and doing the work of the Lord.
2
00:00:09,120 --> 00:00:13,880
如果你不愿意去遵行耶稣基督的话语的话,
3
00:00:14,220 --> 00:00:18,220
就没有必要昼夜去默想神的话。
4
00:00:18,220 --> 00:00:22,200
Take more of me, give me more of you.
....
问题是如何使用命令行并根据字幕中的时间ffmpeg
删除所有中文视频部分?视频很长,目的是使用命令行来完成任务,而不是手动。
步骤1)所以我需要识别字幕每一行的语言:
#!/bin/bash
while IFS= read -r line
do
echo "text: $line"
lan= trans -id $line |awk '/^Code/ {print $2}'
echo "lan: $lan"
done < "$1"
那么上面的 bash 还不能正常工作,该怎么办呢?
我正在安装 Homebrew,收到一条消息:
后续步骤:
‐ 将 Homebrew 添加到您的 PATH,brew shellenv
在 shell 配置文件中运行(例如~/.bash_profile
或~/.zprofile
)”
在我的 shell 配置文件中运行某些内容意味着什么?brew shellenv
如果它不在我的路径中,我该如何运行?
bash 运算符=~
相当于perl
调用吗?
filename="test-33.csv"
regex="([^.]+)(-\d{1,5})(\.csv)"
通过 bash 测试:
if [[ "$filename" =~ $regex ]]; then echo "it matches"; else echo "doesn't match"; fi
# doesn't match
if [[ "$filename" =~ ([^.]+)(-\d{1,5})(\.csv) ]]; then echo "matches"; else echo "doesn't match"; fi
# doesn't match
和perl
:
result="$(perl -e "if ('$filename' =~ /$regex/) { exit 0;} else { exit 1;} ")"
if [[ result ]]; then echo "it matches"; else echo "doesn't match"; fi
# it matches
我对 bash 操作符有什么遗漏吗=~
?这与贪婪与非贪婪迭代器 ( ) 有关系吗[^.]+
?