我很乐意将分号、换行符和与号放在需要的地方。但什么是“正确”的方法呢?这只是风格/个人喜好的问题,还是其中一个在某些方面比另一个更好?
while true; do
cmd1
cmd2
cmd3
done
while true; do
cmd1;
cmd2;
cmd3;
done
第二个使用更多的墨水/电子,但你可以简单地连接线,它仍然可以:
while true; do cmd1; cmd2; cmd3; done
我很乐意将分号、换行符和与号放在需要的地方。但什么是“正确”的方法呢?这只是风格/个人喜好的问题,还是其中一个在某些方面比另一个更好?
while true; do
cmd1
cmd2
cmd3
done
while true; do
cmd1;
cmd2;
cmd3;
done
第二个使用更多的墨水/电子,但你可以简单地连接线,它仍然可以:
while true; do cmd1; cmd2; cmd3; done
我正在寻找一个tmux
命令来获取最后一个焦点窗格的当前目录 ( #{pane_current_path}
),不需要切换到它并切换回来。
以下作品:
tmux last-pane
echo "$(tmux display-message -pF "#{pane_current_path}")"
tmux last-pane
但是我想知道是否有一种方法可以做到这一点而不必调用tmux last-pane
两次(也许是一个包含最后一个窗格的 ID 的变量?)。
与此相关:
https://unix.stackexchange.com/a/754252/582781
解决方案一:
free -g -s2 | sed -u -n 's/^Mem:\s\+[0-9]\+\s\+\([0-9]\+\)\s.\+/\1/p' >> memory.log
有没有办法将 Swap 添加到其中,以便我记录已用 Mem 和 Swap 的总和?
在zsh中
echo 'a string' > test.txt
echo $?
0
和
[[ $(echo 'a string') ]]
echo $?
0
然而
[[ $(echo 'a string' > test.txt) ]]
echo $?
1
另一个例子
curl -so 'curl-8.2.1.tar.gz' https://curl.se/download/curl-8.2.1.tar.gz
echo $?
0
或者
[[ $(curl -so 'curl-8.2.1.tar.gz' https://curl.se/download/curl-8.2.1.tar.gz) ]]
echo $?
1
我的问题:
a string
出现在test.txt
并且curl将文件下载到我指定的输出文件中,为什么评估结果为false
?false
仍然返回),应该如何执行?我可以添加第二次检查来查看该行是否出现或文件是否已下载,但首先就不需要评估命令是否成功执行。更多完整性示例(可读性与“正确性”?):
if ( $(echo 'a string' > text.txt) ); then echo yes; else echo no;fi
yes
if (( $(echo 'a string' > text.txt) )); then echo yes; else echo no;fi
no
if $(echo 'a string' > text.txt); then echo yes; else echo no;fi
yes
if echo 'a string' > text.txt; then echo yes; else echo no;fi
yes
您好,我有一个 englishStrings.json 文件,当我对其进行新的编辑/添加时,我需要将新的更改复制到所有其他语言的 string.json 文件。
在对 englishStrings 进行任何更改之前,englishStrings 和 otherLanguageStrings 具有相同的键和相同的行数,但值不同。
我正在尝试做这样的事情:
git diff -U0 —-word-diff-regex=. —-word-diff=porcelain englishStrings.json > changes.diff
patch frenchStrings.json < changes.diff
但它不适用于编辑。当我在 englishString 的键值对末尾插入逗号并需要将其传输到另一个语言字符串文件时,我想应用字符差异。
示例:EnglishString.json
{
"a": "one",
"b": "two",
"test": "test",
"c": "three",
"edit": "edit"
}
法语字符串.json
{
“a”: “un”,
“b”: “deux”,
“c”: “trois”
}
git diff -U0 —-word-diff-regex=. —-word-diff=porcelain EnglishString.json > changes.diff
@@ -4 +4,3 @@
"
+test": "test",
~
+ "
c": "three"
+,
~
+ "edit": "edit"
~
补丁后预计:
{
"a": "un",
"b": "deux",
"test": "test",
"c": "trois",
"edit": "edit"
}
但补丁目前告诉我补丁文件格式错误。补丁可能无法提供帮助,因为英语和法语字符串之间的行长度可能不同,但我愿意接受建议来更改我的解决方案
我想rofi
根据可用选项的数量自动调整菜单的高度。有一个选项-lines
,但有了它我只能传递常数。因此,对于每次调用,我都被迫执行两次命令——计算行数并显示菜单:
#!/usr/bin/sh
lines=$(my_command | wc -l)
my_command | rofi -dmenu -lines "$lines"
是否可以告诉rofi
将其菜单缩小到可用选项的数量?
我已经制作了一个可以在 rhel 发行版和 fork 上运行的脚本。它供个人使用,可以自动下载我使用的存储库和软件。当我使脚本在主机上可执行时,我可以右键单击脚本并选择作为程序运行。当我将脚本复制到闪存驱动器,然后将其从闪存驱动器复制到另一台运行相同操作系统的计算机时,我必须再次使其可执行以返回右键单击并作为程序运行的功能。有明显的解决方法可以继续使用该脚本,但能够右键单击并作为程序运行是最简化和最有用的我的脚本正在做的事情。那么,当我通过 USB 将脚本传输到另一台电脑时,如何让我的脚本保持该功能?
我正在尝试运行包含命令的 shell 脚本
$locate /etc/*.conf
查找 /etc/ 中以 .conf 结尾的所有文件,但每当我运行脚本时,命令行都会显示/etc/chrony.conf: Permission denied
. 每当我在命令行中单独运行命令时,它执行得很好,但从脚本中它失败了。我已经用 chmod 更新了脚本权限,尝试使用 sudo 权限运行脚本,在脚本本身内部赋予 sudo 权限,甚至以 root 身份登录,但它仍然说权限被拒绝。
任何帮助表示赞赏,谢谢!
我有 3 个脚本来构建 OpenWrt,但我无法将其设置为最后有时间。这是脚本给出的内容和脚本。
openwrt-snapshot-r22256+29-14334c222e-ipq806x-generic-linksys_ea8500-sysupgrade.bin
但我想要那种随着时间的描述:
openwrt-snapshot-r22256+29-14334c222e-20230310-ipq806x-generic-linksys_ea8500-sysupgrade.bin
剧本:
#!/bin/bash
# createbuildinfo
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source ${SCRIPT_DIR}/shared
STATUSFILE="$BINDIR/$BUILTBASENAME.status"
DIFFFILE="$BINDIR/$BUILTBASENAME.patch"
DIFFCONFIGFILE="$BINDIR/$BUILTBASENAME.diffconfig"
DATE=$(date +%Y%m%d-%H%M)
cd $BUILDROOT
echo "Creating build info for"
echo " $BUILTBASENAME in"
echo " $BINDIR "
getGitInfo() {
#params: directory
echo "###########################################################################"
(cd $1
echo -n "Repo : "
git remote -v show | grep fetch
echo -n "Branch : "
git rev-parse --abbrev-ref HEAD
echo -n "Commit : "
git show --format="%cd %h %s" --abbrev=7 --date=short | head -n 1 | cut -b1-60
echo "Status : "
git status --porcelain
)
}
getGitDiff() {
(cd $1
git --no-pager diff HEAD
)
}
echo "Generating git statusfile $STATUSFILE"
echo "Status for $BUILTBASENAME created $DATE" >$STATUSFILE
echo " " >>$STATUSFILE
getGitInfo $BUILDROOT >>$STATUSFILE
for feed in `cat feeds.conf | grep -v "^#" | awk '{print $2}'`
do
getGitInfo $BUILDROOT/feeds/$feed >>${STATUSFILE}
done
echo "Generating diff file $DIFFFILE"
getGitDiff $BUILDROOT >$DIFFFILE
echo "Generating diffconfile $DIFFCONFIGFILE"
$BUILDROOT/scripts/diffconfig.sh > $DIFFCONFIGFILE
#共享
#!/bin/bash
BRANCH="snapshot"
BUILDROOT="/run/media/james/D/ipq806x_builds/NSS_test/"
BUILTDIR="$BUILDROOT/../builds"
VERSION_NUMBER=$($BUILDROOT/scripts/getver.sh)
VERSION_DIST=$(grep CONFIG_VERSION_DIST $BUILDROOT/.config | awk -F\" '{print $2}')
VERSION_DIST="${VERSION_DIST:=OpenWRT}"
TARGET_PROFILE=$(grep CONFIG_TARGET_PROFILE $BUILDROOT/.config | awk -F\" '{print $2}' | sed 's/DEVICE_//')
TARGET_SUBTARGET=$(grep CONFIG_TARGET_SUBTARGET $BUILDROOT/.config | awk -F\" '{print $2}')
TARGET_BOARD=$(grep CONFIG_TARGET_BOARD $BUILDROOT/.config | awk -F\" '{print $2}')
BUILTBASENAME="${VERSION_DIST}-${BRANCH}-${VERSION_NUMBER}-${TARGET_BOARD}-${TARGET_SUBTARGET}-${TARGET_PROFILE}"
BUILTBASENAME=$(echo $BUILTBASENAME | sed 's/ /-/g' | awk '{print tolower($0)}')
BINDIR="$BUILDROOT/bin/targets/${TARGET_BOARD}/${TARGET_SUBTARGET}"
#createbuild
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source ${SCRIPT_DIR}/shared
echo "#### `date`: Starting build of $VERSION_DIST $VERSION_NUMBER ####"
cd $BUILDROOT
time make -j4 V=s 2>&1 $1 | tee build.log | grep -i -E "^make.*(error|[12345]...Entering dir)"
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
echo -en "\007"
echo "#### `date`: Build failed, bailing out... "
exit 1
fi
echo -en "\007"
echo "#### `date`: Build of $VERSION_DIST $VERSION_NUMBER complete ###"
if [ -z $1 ]; then
${SCRIPT_DIR}/createbuildinfo
echo "#### `date`: Moving completed build to $BUILTDIR/$BUILTBASENAME ####"
mkdir -p $BUILTDIR/$BUILTBASENAME
#mv -v $BINDIR/$BUILTBASENAME* $BUILTDIR/$BUILTBASENAME
mv -v /run/media/james/D/ipq806x_builds/NSS_test/bin/targets/ipq806x/generic/*.bin /run/media/james/D/ipq806x_builds/builds/latest/
rm $BUILTDIR/latest
ln -s $BUILTDIR/$BUILTBASENAME $BUILTDIR/latest
echo "All done! Sysupgrade file can be found in"
echo $BUILTDIR/latest/$BUILTBASENAME-squashfs-sysupgrade.bin
fi
递归文件重命名器 - 非常有用的 python 脚本在升级 Python(Pyt2 到 Pyt3)后停止工作。任何人都可以找到并解决问题吗?谢谢!
例如,那些文件:
'Linux příručka českého uživatele.pdf' 'LINUXTERO příkazy které se vždy hodí.pdf' 'Práce s archívy příkazové řádka.pdf' 'Rekurzivní grep.txt'
更改为这些文件:
linux_prirucka_ceskeho_uzivatele.pdf' linuxtero_prikazy_ktere_se_vzdy_hodi.pdf prace_s_archivy_prikazove_radka.pdf rekurzivni_grep.txt
程序员是捷克人,所以我必须通过谷歌翻译成英语......
当我启动脚本时,按摩失败如下:
linq@legion5:~/test-renamer/ask_stackexchange_folder$ vycisti.py 文件“/home/linq/bin/vycisti.py”,第 62 行 打印“警告:'%s' 仍然不是有效名称” % fileName ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ 语法错误:调用“打印”时缺少括号。你是说 print(...) 吗?
这是脚本:
#!/usr/bin/python
# -*- coding: utf-8
'''Renames all files and directories in the current directory to
to have some culture. It also works recursively in nested ones
directories. The changes are as follows:
Czech characters -> English
cancels all dashes and spaces at the beginning of the name (damn, what kind
the names make up would deserve a few slaps)
special characters are replaced by '_', I take the hyphen at mercy, but not at the beginning
the (unnecessary) '_' around the hyphen is discarded
multiple '_'s are replaced by a single occurrence
everything will be converted to lowercase
The result should be a title where there are only letters, underscores,
numbers, periods and dashes (but not at the beginning).
It writes exactly what it does to standard output
Usage: vycisti.py [ > logfile ]
'''
import re
import os
import sys
import unicodedata
class RenameFiles:
def __init__(self):
if os.environ.get('OS','') == 'Windows_NT':
self.locale = "cp1250"
else: #probably linux
local = os.environ.get('LANG', '')
if '.' in local: #Fedora way
self.locale = local.split('.')[1]
else: #Debian
self.locale = 'iso-8859-2'
def cleanString(self, what):
'''Gets rid of letters which are not in English alphabet'''
assert type(what) == unicode
normalized = unicodedata.normalize('NFKD', what)
output = ''
for c in normalized:
if not unicodedata.combining(c):
output += c
return output
def cleanName(self, fileName):
'''Convert the givne string into a form which is suitable for a file name'''
assert type(fileName) == str
fileName = self.cleanString(fileName.decode(self.locale))
fileName = re.sub("^[-\ ]+", "", fileName) #delete space or dash at the beginning
invalid_stuff = re.compile(r"[^a-zA-Z0-9_\.-]+") #forbidden characters (non-alfanumerical)
fileName = invalid_stuff.sub("_", fileName.strip()) #replace invalid stuff and spaces by _,
fileName = re.sub("_+", "_", fileName) #squeeze continuous underscores to one _
fileName = re.sub("-+", "-", fileName) #squeeze continuous dashes to one _
fileName = re.sub("_*-_*", "-", fileName) #removes useless '_' round the dash
fileName = re.sub("_*\._*", ".", fileName) #removes useless '_' round the dot
fileName = re.sub("-*\.-*", ".", fileName) #removes useless '-' round the dot
fileName = fileName.lower() #lower case
valid_name=re.compile(r"^[a-z0-9_\.][a-z0-9_\.-]+$") #regular expression for feasible name
if not valid_name.match(fileName):
print "Warning: '%s' is still not valid name" % fileName
return fileName.encode(self.locale)
def renameFile(self, dir, fileName):
'''Public: Renames the file fileName in the directory'''
assert type(fileName) == str
assert type(dir) == str
try:
new = self.cleanName(fileName)
except:
print "Problem: %s %s " % (fileName, sys.exc_info()[0] )
new = ""
if (new != "" and new != fileName):
print "Renaming %s: %s -> %s" % (dir, fileName, new) #kontrolní výpis
os.rename(dir+os.sep+fileName, dir+os.sep+'tmp')
os.rename(dir+os.sep+'tmp', dir+os.sep+new)
return
def process_dir(self, dir):
"""process all files in the folder"""
assert type(dir) == str
for f in os.listdir(dir):
fileName = dir + os.sep + f
if os.path.isdir(fileName) and os.access(fileName, os.W_OK): #if it is directory
self.process_dir(fileName) #process the elements in the directory
self.renameFile(dir, f) #rename the directory itself
else:
self.renameFile(dir, f) #if it is a file
return
if __name__=='__main__':
renamer = RenameFiles()
renamer.process_dir('.')