小编tri*_*101的帖子

Bash 脚本 - 反转字符串

我正在尝试创建一个脚本,该脚本将获取一个单词(最少 3 个字符,最多 10 个字符)作为其输入,然后它将反转字符的位置。

问题是如何组合 if 语句的操作以使该脚本工作。

#!/bin/bash
clear
ok=3
echo *****REVERSE*****
while [ $ok = 3 ]
do
echo "Enter a character: " 
read id
   if [ ${#id} -eq 3 ]
   then 
   echo  $id | rev
   elif [ ${#id} -eq 4 ]
   then 
   echo  $id | rev
   elif [ ${#id} -eq 5 ]
   then
   echo  $id | rev
   else
   echo Minimum character is 3 and Maximum is 5!Logging out..
   ok=4
  fi
done
Run Code Online (Sandbox Code Playgroud)

bash shell-script

2
推荐指数
1
解决办法
2530
查看次数

需要一个可以在一行中生成包含所有所需信息的输出的命令/脚本

如何使用 bash 脚本/或命令在下面生成输出:

期望的输出:

contacts.USA | Name:John Due | Gender:Male | Age:21 | Address: Texas    
contacts.USA | Name:Ed Mundo | Gender:Male | Age:41 | Address: California    
contacts.BRAZIL | Name:Tom Paul | Gender:Male | Age:26 | Address: Sau Paulo
Run Code Online (Sandbox Code Playgroud)

示例输入:我有 100 个用于差异的联系人文件。国家。

联系人.美国

Name:John Due 
Gender:Male 
Age:21
Address: Texas

Name:Ed Mundo 
Gender:Male 
Age:41 
Address: California
Run Code Online (Sandbox Code Playgroud)

联系人.巴西

Name:Tom Paul
Gender:Male 
Age:26 
Address: Sau Paulo
Run Code Online (Sandbox Code Playgroud)

我在下面使用 unix cmd 但无法生成所需的输出。

grep -E 'Name|Gender|Age|Address' contacts.*
Run Code Online (Sandbox Code Playgroud)

此 cmd 的输出连续显示结果:

contacts.USA Name:John Due 
contacts.USA Gender:Male 
contacts.USA Age:21 …
Run Code Online (Sandbox Code Playgroud)

linux text-processing

2
推荐指数
1
解决办法
91
查看次数

将读取 5 个数字然后从高到低排序的脚本

我正在尝试创建一个脚本,该脚本将获取 5 个数字,然后将它们从大到小排序。到目前为止,这就是我所拥有的:

#!/bin/bash
clear

echo "********Sorting********"
echo "Enter first number:"
read n1
echo "Enter second number:"
read n2
echo "Enter third number:"
read n3
echo "Enter fourth number:"
read n4
echo "Enter fifth number:"
read n5  
Run Code Online (Sandbox Code Playgroud)

shell-script text-processing sort

1
推荐指数
1
解决办法
1746
查看次数

Bash 脚本 - 测试文件是否存在

如何创建一个脚本来检查提供的文件是否存在,如果不存在则创建错误消息。我有以下代码,但它不起作用:

#!/bin/bash
echo "enter file name:"
read source
file= $source
if [ -f "$file" ] && find "$file" | grep -q .
then 
    echo "the file exists."
else
    echo "the file does not exist."
fi
Run Code Online (Sandbox Code Playgroud)

bash

-2
推荐指数
1
解决办法
6565
查看次数

标签 统计

bash ×2

shell-script ×2

text-processing ×2

linux ×1

sort ×1