这是我的 Unix shell 脚本代码
#! /bin/ksh
echo "Enter your first number:"
read first
echo "Enter your second number:"
read second
echo "Enter your third number:"
read third
SUM=`expr $first + $second + $third`
AVERAGE="$SUM/3"
echo "The average is $AVERAGE"
Run Code Online (Sandbox Code Playgroud)
基本上我想找到用户输入的 3 个值的平均值。输入所有值后得到的结果,例如所有值加起来为 12;是“平均值是12/3”。
我有一个 sub sh 文件,它接收本地目录和远程目录路径的输入,并将文件发送到远程目录。如果远程路径无效,则无法检测到错误。
sh 脚本内的示例内容:
sftp XXX@XXX.XXX.XXX.XXX <<==
cd $remote_dir <= error happens here
lcd $local_dir <= error did not happen so the return code resets
put a.tar
bye
==
$result=$?
return $result
Run Code Online (Sandbox Code Playgroud)
有些应用程序调用sh文件,然后通过返回码来判断文件是否发送成功。如果是,则将本地文件备份到某处然后将其删除。
问题是即使文件发送失败,shell 也会返回零。文件确实没有被复制,您可以看到当您手动执行命令时会发生错误,屏幕上显示找不到路径,但是当作为作业运行时,它会失败。
我知道另一种方法是执行两个步骤,首先检查路径是否存在,然后输出到日志文件并检查日志是否包含错误,如果没有错误则可以调用 sftp put shell 脚本。我想知道是否还有其他方法。
我想做的是获取用户输入并搜索文件,如果匹配,它将显示结果,否则它将回显 $keyword not found。
但我的票据总是返回错误。
read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;
Run Code Online (Sandbox Code Playgroud) 我想编写一个shell脚本,首先打开sqlite,然后复制一个表,最后删除该表的内容。我怎样才能在 shell 脚本中做到这一点?
>>cd /var/www/dbs
>>sqlite3 ha.db
sqlite>>.timeout 2000
sqlite>>INSERT INTO table1 SELECT * FROM table2;
sqlite>>DELETE * FROM table2;
sqlite>>.quit
Run Code Online (Sandbox Code Playgroud)
这部分怎么写呢?
>>sqlite3 ha.db
sqlite>>INSERT INTO table1 ('a1','a2') VALUES ('1','2');
sqlite>>.quit
Run Code Online (Sandbox Code Playgroud) 我在 txt 文件中有 5000 个问题,如下所示:
<quiz>
<que>The question her</que>
<ca>text</ca>
<ia>text</ia>
<ia>text</ia>
<ia>text</ia>
</quiz>
Run Code Online (Sandbox Code Playgroud)
我想在 Ubuntu 中编写一个脚本来转换所有问题,如下所示:
{
"text":"The question her",
"answer1":"text",
"answer2":"text",
"answer3":"text",
"answer4":"text"
},
Run Code Online (Sandbox Code Playgroud) 我正在尝试读取一个 json 文件并使用其输出来jq构建另一个 json,结合上述 json 文件和其他值,并将其传递给CURL
我用以下方式读取文件policies=$( sed 's/\\//g' policies.json)
我使用以下jq命令构建新的 json
BODY=$( jq -n \
--arg cid "$chaincodeId" \
--arg cv "$chaincodeV" \
--arg ct "$chaincodeT" \
--arg ar "$chaincodeArgs" \
--arg pol "$policies" \
'{chaincode_id: $cid, chaincode_version: $cv, chaincode_type: $ct, endorsement_policy: $pol}' )
Run Code Online (Sandbox Code Playgroud)
它对于前 4 个属性效果很好。但是,endorsement_policy 属性包含反斜杠,因此服务器无法读取通过curl 发送的属性。
body 的输出如下
{ "chaincode_id": "IdentityManager", "chaincode_version": "testcc2", "chaincode_type": "node", "endorsement_policy": "{\n \"identities\": [\n {\n \"role\": {\n \"name\": \"member\",\n \"mspId\": \"org1\"\n }\n },\n {\n \"role\": …Run Code Online (Sandbox Code Playgroud) 我全力支持 shell 脚本的可移植性。
但我不确定我现在是否做得太过分了。
在此示例中,我们有一个名为 的函数confirmation,它接受第一个参数作为包含问题的字符串,并且所有其他参数都是可能的有效答案:
confirmation ()
{
question=$1; shift; correct_answers=$*
printf '%b' "$question\\nPlease answer [ $( printf '%s' "$correct_answers" | tr ' ' / ) ] to confirm (Not <Enter>): "; read -r user_answer
# this part iterates through the list of correct answers
# and compares each as the whole word (actually as the whole line) with the user answer
for single_correct_answer in $correct_answers; do
printf '%s' "$single_correct_answer" | grep -i -x "$user_answer" > /dev/null …Run Code Online (Sandbox Code Playgroud) 我确实需要练习我的脚本编写...到目前为止,我有下面的脚本,当将一个 .zip 和一个平面文件放入源位置时,该脚本可以工作。如果放入多个文件,它就无法正确执行,我希望看看是否有人可以提供更有效的方法帮助。只是为了澄清脚本需要扫描源文件夹中的任何 .zip 文件,将它们解压缩到同名的文件夹中减去 .zip,然后将新文件夹与文件夹中的任何其他平面文件或非 .zip 文件一起移动到目标位置。将源复制到单独的位置,并删除原始 .zip 文件。
#! /bin/bash
cd "/path/to/source/Location"
for file in $(ls *.zip); do unzip $file -d $(echo $file | cut -d . -f 1); done
mv $(echo $file | cut -d . -f 1) /Destination/for/Unzip
rm *.zip
mv LOCK* /Destination/For/Flat/Files
Run Code Online (Sandbox Code Playgroud) 我尝试通过脚本组织我的文件和文件夹。它使用我的文件的第一个字母创建文件夹并将其移动到那里。
第一次,脚本完成这项工作,但如果我创建新文件并再次执行脚本,它会在文件夹中创建一个新的子文件夹,依此类推。
例如,它创建T/T/toto.txt.
我的脚本:
for first in $(ls -1 | sed 's/^\(.\).*$/\1/' | tr '[a-z0-9]' '[A-Z0-9]' | uniq)
do
mkdir tmp
mv "$first"* tmp/
lower=$(echo $first | tr '[A-Z]' '[a-z]')
mv "$lower"* tmp/
mv tmp/ "$first";
done
Run Code Online (Sandbox Code Playgroud) 假设我有各种想要下载的图像,并且有可用的链接:
我不想一一输入
wget https://images.unsplash.com/photo-1548363585-5b1241ee3b85?ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80
wget https://images.unsplash.com/photo-1556648011-e01aca870a81?ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这个目标?我读到,将这些链接保存在 .txt 文件中并使用 for 循环不是正确的方法。