我有一个shell脚本,需要一个cal命令和一个文本文件[关闭]

mdfafbf1  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(119)

已关闭,此问题需要更focused。它目前不接受回答。
**想改善这个问题吗?**更新问题,使其只关注editing this post的一个问题。

4天前关闭。
Improve this question
我需要编写一个shell脚本,并满足以下要求:
1.询问用户的出生年份(例如2020年),使用此变量使用cal命令。
1.将输出转换为名为pixar.txt的文件。
1.对文件使用less命令向用户显示文件的内容。
1.当less命令退出时,然后在这个文件上使用nano将顶部的birthyear从您的出生年份更改为birthyear。
1.退出nano后,再次对文件使用更少,并将其显示给用户。
我已经在第1部分和第2部分卡住了大约一天,因为这是我第一次使用C或Linux。
到目前为止,我已经写了这段代码:

#include<stdio.h>                                                               
int main() {                                                                    
int year;                                                                       
                                                                                
printf("Please enter your year of birth");                                      
scanf("%d", &year);                                                             
}

但我不知道接下来该怎么办。cal函数不能从shell脚本中调用,当我编译它时,它说它不识别cal函数。

egdjgwm8

egdjgwm81#

C程序不是shell脚本。C是一种编译语言,与解释性语言(如shell脚本)相反。
下面是一个bash脚本,可以做你想要的:

#!/bin/bash

# Before running the program, check if cal is installed
if ! which cal && which nano && which less ; then
    echo "Required programs are not install." >&2
    echo "sudo apt-get install ncal nano less" >&2
    exit 1
fi

# Step 1: Ask the user for their year of birth
read -p "Please enter your year of birth (e.g., 2020): " birth_year

# Step 2: Use the cal command and save the output to calendar.txt
cal $birth_year > calendar.txt

# Step 3: Display the content of the file using the less command
less calendar.txt

# Step 4: Use nano to change the birth year in the file
nano calendar.txt

# Step 5: Display the updated file using the less command
less calendar.txt

保存上述代码在一个文件中,例如,software_script. sh,并通过运行以下命令使其可执行:

chmod +x calendar_script.sh

要运行脚本,只需执行它:

./calendar_script.sh

确保在运行脚本之前安装了nanocalless

相关问题