windows系统.bat脚本文件,linux shell 之--bash的应用

一、批处理.bat

1.概念:
批处理文件,在DOS和Windows(任意)系统中,.bat文件是可执行文件,由一系列命令构成,其中可以包含对其他程序的调用。这个文件的每一行都是一条DOS命令(大部分时候就好像我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件。
2.举例
demo1:双击.bat 文件打开一个文件夹某处的视频

@echo on //注释:让cmd命令窗口保留
cd  "C:\Users\LIMINGYANG\Desktop\lesson\c++\ly"
start  C++11thread13.mp4

pause

demo2:双击.bat打开一个文件夹某处的exe

@echo on
cd  /d "E:\BaiduNetdisk"
start  /b baidunetdisk.exe

pause

打开exe文件需要加/b 和/d !!!

脚本文件的命令有很多,具体详见:https://www.cnblogs.com/xpwi/p/9626959.html

二、linux的shell举例

1.概念:一个命令解释器

Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。


2、制作mybash

2.1bash的命令分类
2.1.1 内置命令:通过调用接口来实现
2.2.2 普通命令:在bin中已经封装好的exe
比如:如下代码:

ubuntu@VM-4-8-ubuntu:~/mybash$ which vi
/usr/bin/vi

现在用的多的linux shell就是bash
我们模拟一个bash 试试:(几个shell命令)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
/*mybash的demo*/
char *get_cmd(char *buff, char *argv[])
{
    if (buff == NULL || argv == NULL)
    {
        return NULL;
    }
    int i = 0;
    char *s = strtok(buff, " ");
    while (s != NULL)
    {

        argv[i++] = s;
        s = strtok(NULL, " ");
    }
    return argv[0];
}
void run_cmd(char *path, char *argv[])
{
    if(path == NULL||argv == NULL){
        perror("PATH OR ARGV NULL");
        return ;
    }
    pid_t pid = fork();
    if (pid < 0)
        return;
    if (pid == 0)
    {
        int res = execvp(path, argv);
        printf("我执行完命令回来了");
        printf("%s",*argv);
        if(res == -1){
        perror("execvp fail!");
        }
        exit(0);
    }
    wait(NULL);
}
int main()
{
    while (1)
    {

        printf("Myubuntu@VM-4-8-ubuntu\n");
        char buff[128] = {0};
        fgets(buff, 128, stdin);
        // printf("%ld\n",strlen(buff));
        // printf("%ld\n",sizeof(buff));
        buff[strlen(buff) - 1] = 0;
        char *argv[10] = {0};
        char *cmd = get_cmd(buff, argv);
        if (cmd == NULL)
        {
            continue;
        }
        if (strcmp(cmd, "cd") == 0)
        {
            if(cmd == NULL||argv[1]==NULL){
                perror("cmd error");
                continue;
            }
            if(chdir(argv[1])==-1) chdir :改变工作文件夹
            {
                perror("cmd error");
                continue;
            }
            continue;
        }
        if (strcmp(cmd, "exit") == 0)
        {
            exit(0);
        }
        /*调动系统调用实现ls*/
        if(strcmp(cmd ,"ls")==0)
        {
            char path[256] = {0};
            /*The  getcwd()  function  copies  an  absolute  pathname  of the current working directory to the array,得到当前文件夹名字*/
       pointed to by buf, which is of length size.
            if(getcwd(path,256) == NULL){ 
                perror("get cwd error");
            }
            DIR * pdir = opendir(path);
            if(pdir == NULL){exit(1);}
            struct dirent *s = NULL;
            while((s=readdir(pdir))!= NULL){
                if(strncmp(s->d_name,".",1)==0){
                    continue;
                }
                printf("%s  ",s->d_name);
            }
            closedir(pdir);
        }
        run_cmd(cmd, argv);
        
    }
    return 0;
}

你可能感兴趣的:(linux,linux,bash,windows)