最新記事

forループを使用してtxtファイルを解析するときの進行状況インジケータ
bash
shell-script
scripting

forループを使用してtxtファイルを解析するときの進行状況インジケータ

DNSサーバーをテストし、結果をcsvファイルに返すために非常に単純なスクリプトを作成しようとしています。うまくいきますが、大規模なドメインソースファイルの場合は時間がかかります。 pvまたはawkを使用して進行状況インジケータを作成する方法はありますか? #!/bin/bash # File name of domain list: One FQDN per line in file. domain_list='domains.txt' # # IP address of the nameserver used for lookups: ns1_ip='1.1.1.1' # Cloudflare ns2_ip='9.9.9.9' # Quad9 # # Seconds to wait between lookups: loop_wait='1' # echo "Domain name, $ns1_ip,$ns2_ip" > dns-test-results.csv; for domain in `cat $domain_list` do ip1=`dig @$ns1_ip +short $domain |tail -n1`; ip2=`dig @$ns2_ip +short $domain |tail -n1`; echo -en "$domain,$ip1,$ip2\n" >> dns-test-results.csv; # done;

Admin

変数になり、コマンド置換を含むファイル名にリダイレクト
bash
shell-script
files
scripting
variable

変数になり、コマンド置換を含むファイル名にリダイレクト

私はnmapを使用してネットワークをスキャンし、スキャン情報を含むXMLファイルを作成します。各ファイルが一意でスキャン時間と日付を表示できるように、XMLファイルにスキャン日を含めたいと思います。定期的に同じネットワークに対して多くのスキャンを実行します。私はそれを生成するためにdateコマンドを使用します。 nmap -sSV -oX scan_dmz_$(date +'%H'h'%M'm'%S's_'%d'-'%m'-'%Y').xml 10.0.0.0/24 10.1.0.0/24 しかし、後でXMLを便利なHTMLファイルに変換するスクリプトの他のコマンド(xsltproc)で使用できるように、XMLファイル名を変数に入れたいと思います。デフォルトでは、xsltprocコマンドは変換できるようにnmapによって生成されたXMLファイルの名前を知る必要があります。 xsltproc scan_dmz_12h00m00s_01-01-2023.XML -o DMZ_HTML_report.html xsltprocで使用できるように、このXMLファイル名を変数に入れようとします。私はそれがどのように機能するかを調べるために非常に基本的なbashスクリプトを試しましたが、うまく動作しませんでした。 tail /var/log/auth.log > tailout=tail_$(date +'%H'h'%M'm'%S's_'%d'-'%m'-'%Y').xml ; xsltproc $tailout -o DMZ_HTML_report.html 本質的に、将来のコマンドがファイルを処理できるように同時に変数でなければならない変数ファイル名にリダイレクトする方法は?よろしくお願いします。

Admin

rofiメニュー高さ自動調整
scripting
rofi

rofiメニュー高さ自動調整

rofi利用可能なオプションの数に合わせてメニューの高さを自動的に調整したいと思います。オプションがありますが、-linesこのオプションでは定数のみを渡すことができます。したがって、呼び出すたびにコマンドを2回実行する必要があります。行数を数えてメニューを表示します。 #!/usr/bin/sh lines=$(my_command | wc -l) my_command | rofi -dmenu -lines "$lines" rofiメニューを利用可能なオプションの数に絞り込むように指示できますか?

Admin

シェルスクリプトを実行しようとしていますが、If:式の構文は引き続き表示されます。どうやって取り除きますか?
shell
files
scripting

シェルスクリプトを実行しようとしていますが、If:式の構文は引き続き表示されます。どうやって取り除きますか?

こんにちは。タスク4のコードを実行しようとしていますが、式構文エラーが発生し続けます。理由を教えてもらえますか? #!/bin/sh if [ "$#" -ne 1 ] then echo "Usage: assignment4.sh " exit 1 fi if [ ! -d "$1" ] then echo "$1: No such directory" exit 1 fi num_dir=0 num_file=0 num_readable=0 num_writable=0 num_executable=0 for item in "$1"/* do #Question1 if [-d "$item" ] then num_dir=$((num_dir+1)) fi #Question 2 elif [ -f "$item" ] then num_file=$((num_file+1)) fi #Question 3 if [-r "$item" ] then num_readable=$((num_readable+1)) fi #Question 4 if [ -w "$item" ] then num_writable=$((num_writable+1)) fi #Question 5 if [ -x "$item" ] then num_executable=$((num_executable+1)) fi done echo "Number of directories: $num_dir" echo "Number of files: $num_file" echo "Number of readable items: $num_readable" echo "Number of writable items: $num_writable" echo "Number of executable items: $num_executable"

Admin

テキスト処理 - 2つの異なるファイルに基づく修正
linux
shell-script
text-processing
scripting

テキスト処理 - 2つの異なるファイルに基づく修正

one.txtという2つのファイルがあります。 "297","237","14",NULL,"51.195.14.201","02:00:00:1a:1c:06","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0" "303","242","14",NULL,"51.195.14.207","02:00:00:a3:03:10","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0" "323","249","14",NULL,"51.195.14.209","02:00:00:dd:1f:11","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0" そして、以下を含む他の2つの.txt 51.195.14.201, 193.138.35.127, 02:00:00:20:a2:27 51.195.14.209, 193.138.35.128, 02:00:00:20:a2:28 Two.txtの最初のフィールドにone.txtのフィールド5と同じ入力を含む行が含まれている場合(正確でなければならず、例えば衝突する場合51.195.14.1)、51.195.14.115番目のフィールドはTwoの2番目のフィールドに置き換える。 .txt、6番目のフィールドはTwo.txtの3番目のフィールドに置き換える必要があります。変更されたすべての行には、3番目のフィールド19がに変わり255.255.255.0、7番目のフィールドがに変わり、8番目のフィールドがに変わり193.138.35.1、9番目のフィールドがに変わります。24 変更は次のようにする必要があります "297","237","19",NULL,"193.138.35.127","02:00:00:20:a2:27","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0" "303","242","14",NULL,"51.195.14.207","02:00:00:a3:03:10","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0" "323","249","19",NULL,"193.138.35.128","02:00:00:20:a2:28","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0" 一部の列にはNULL引用符が含まれていません。

Admin

アップグレード後、名前変更スクリプトは機能しません。新しいものが必要です。
scripting
python

アップグレード後、名前変更スクリプトは機能しません。新しいものが必要です。

再帰的なファイル名の変更 - Python(Pyt2からPyt3へ)をアップグレードした後、非常に便利なPythonスクリプトが機能しなくなりました。誰でも問題を見つけて解決できますか?ありがとうございます! たとえば、次のファイルは次のようになります。 「Linux príručka českého user.pdf」 'LINUXTERO príkazy které se vždy hodí.pdf' "Práce s Archivy prí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 プログラマーはチェコ人であるため、Googleから英語に翻訳する必要があります... スクリプトを起動すると、マッサージは次のように失敗します。 linq@legion5:~/test-renamer/ask_stackexchange_folder$ vycisti.py ファイル "/home/linq/bin/vycisti.py"、62行 print "警告: '%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('.')

Admin

AWK はテキストファイルからパスを読み込みます。
linux
text-processing
awk
scripting

AWK はテキストファイルからパスを読み込みます。

というファイルがありますパスの回復.txtこれには他のファイルへのパスが含まれています。 ~へパスの回復.txt: ./a2111oi/sky130_fd_sc_hd__a2111oi_0.txt ./a2111oi/sky130_fd_sc_hd__a2111oi_2.txt ./a2111oi/sky130_fd_sc_hd__a2111oi_4.txt 各パスは、以下のように同じ構造を持つ別のテキストファイルを指します。 HEAD INFO BEGIN sky130_fd_sc_hd__a2111oi_0 ... ... END BEGIN END INFO END HEAD path_resume.txtから各.txtファイルを読み取ろうとし、それらの間のすべての行をコピーします。スタートそして終わりが始まる出力.txtという別のファイルに増分保存します。 BEGIN sky130_fd_sc_hd__a2111oi_0 ... ... END BEGIN BEGIN sky130_fd_sc_hd__a2111oi_2 ... ... END BEGIN BEGIN sky130_fd_sc_hd__a2111oi_4 ... ... END BEGIN 私が実行したとき: awk '{while((getline a < $0)> 0) print a}' path_resume.txt path_resume.txtのすべてのファイルを正しく読み取ることができますが、不要な行を削除することはできません。 私が実行したとき: awk '/BEGIN/{flag=1}/END BEGIN/{flag=0}flag' ./a2111oi/sky130_fd_sc_hd__a2111oi_0.txt >> output.txt 不要な行を削除できますが、ファイルパスを手動で渡す必要があります。私の目標を達成するためにこれら2つのコマンドをマージする方法がわかりません。助けてくれてありがとう。

Admin

条件付きgrepパイプラインの問題
bash
scripting
syntax

条件付きgrepパイプラインの問題

私はbashスクリプトを学ぶための初心者ですが、この問題を解決する方法がわかりません。 ./elleviemme.sh: line 7: unexpected token `|', conditional binary operator expected ./elleviemme.sh: line 7: syntax error near `|' ./elleviemme.sh: line 7: `if [[ lsblk | grep -qwEo ^$dvin ]]'

Admin

長いコマンド転送を知らせるには?
linux
bash
scripting
vpn
notify-send

長いコマンド転送を知らせるには?

私は通知を送信してn分ごとにVPNのステータスを知らせる通知を表示したいと思います。 このコマンドは私が設定したポップアップターミナルで実行され、whileループを停止するまでビューから隠されます。その後、ctrl-cを実行するだけです。 次のコマンドを作成しましたが、無限のエラーが発生します。Invalid number of options. while true do notify-send $(timeout 5s nordvpn status | rg -i "status|country|uptime" ; nordvpn settings | rg -i "kill switch|auto-connect")` done timeout 5s場合によっては、nordpnが応答しないため、5秒後にコマンドを終了する必要があるためです。 助けてくれてありがとう。

Admin

Fishリストから変数を抽出するには?
shell-script
scripting
fish

Fishリストから変数を抽出するには?

だから最近、Linuxに似たコマンドをたくさん共有するcliをダウンロードしました。 [ OK mega-login]: login& mega-logout: logout cli を使用する必要があるたびに、大容量ファイルの作成に疲れました。エイリアスを使うかと思いましたが、私が知らない別のコマンドを使って他のプログラムを台無しにしたくはありません。エイリアスを生成すると、loginシステムloginコマンドはどうなりますか?また、cliには多くのコマンドがあります。 それで、スクリプトを活用してみるかと思いましたが、どうすればいいのかわかりませんでした。これが私の考えです。したがってm login、orを使用すると、m logoutそのコマンドを検索するか、存在mega-loginするかを検索mega-logoutして呼び出します。 それで、フィッシュを学びながら数時間の間、さまざまな機能を考えてこう書いています。 function m --argument value $argv echo value $value $argv if type mega-$value set MEGA for temp in $argv set MEGA $MEGA$temp echo $MEGA end echo MEGA \n\n $MEGA eval $MEGA end end それでも動作しません。 PS:初めて文章を書こうとしましたが、わからないのであきらめました。 function m --argument value $argv echo value $value $argv if type mega-$value set MEGA mega-$argv echo $argv \n\n\n $MEGA eval $MEGA end end

Admin

汎用スクリプト言語としての Lua? 質問する
python
scripting
lua

汎用スクリプト言語としての Lua? 質問する

Lua を見ると、私が目にするのは「埋め込みに最適」、「高速」、「軽量」、そして何よりも「World of Warcraft」または略して「WoW」という表現だけです。 なぜ全体を別のアプリケーションに埋め込むことに限定されているのでしょうか? Python や Perl のように汎用スクリプトを記述しないのはなぜでしょうか? Lua は速度やメモリ使用量などの面で優れているようです (私が知る限り、最速のスクリプト言語)。では、タスクを自動化するための「デスクトップ スクリプト言語」として Lua が使用されているのを見たことがありません。たとえば、次のようになります。 多数のファイルの名前を変更する ウェブからファイルをダウンロードする ウェブスクレイピング 標準ライブラリが不足しているのでしょうか?

Admin