ajfkz 2022-01-01 10:43 采纳率: 57.1%
浏览 14
已结题

shell脚本的一个简单程序问题

问题遇到的现象和发生背景

[centos@localhost bin]$ show123-2.sh 3
This program will print your choice

Your choice is
3
[centos@localhost bin]$

问题相关代码,请勿粘贴截图
function printit(){
        echo -e "Your choice is  "
}
echo -e "This program will print your choice\n"
case $1 in
        "1")
                printit;echo -e "$1"
                ;;
        "2")
                printit;echo -e "$1"
                ;;
        "3")
                printit;echo -e "$1"
                ;;
        *)
                echo -e "You just have three choices that is 1 2 3."
                exit 1
                ;;
esac
exit 0
运行结果及报错内容

为什么Your choice is 3,is 和3之间有个回车呢?

我的解答思路和尝试过的方法
我想要达到的结果

能否去掉这个回车?


```bash


```

  • 写回答

1条回答 默认 最新

  • IT民工金鱼哥 运维领域新星创作者 2022-01-01 11:00
    关注

    因为函数调用再加echo会换行的~~所以你的结果会换行


    能否去掉这个回车?可以,很简单~~加一个 -n 参数就可以,脚本如下

    #!/bin/bash
    function printit(){
            echo -e -n "Your choice is "
    }
    echo -e "This program will print your choice\n"
    case $1 in
            "1")
                    printit;echo -e "$1"
                    ;;
            "2")
                    printit;echo -e "$1"
                    ;;
            "3")
                    printit;echo -e "$1"
                    ;;
            *)
                    echo -e "You just have three choices that is 1 2 3."
                    exit 1
                    ;;
    esac
    exit 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月9日
  • 已采纳回答 1月1日
  • 创建了问题 1月1日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度