gogoWebsite

Shell - Write a shortcut script to execute C/C++

Updated to 2 days ago

Shell - Write a shortcut script to execute C/C++

I have been learning shell for a week, and the development environment has changed from vscode to Xshell. The previously arrogant graphical interface has also become a small black window. Although I am a little disappointed, it is also an important exercise for me, allowing me to experience the early programmers, and it may be the general daily life of C++ development in the future.

I have many problems that I have to solve by myself. After learning shell scripts, my first idea is to write a script to simplify the process of compiling and running C, so as not to write a long list of gcc and g++ after writing it once.

Single file compiled version, support C/C++

#!/bin/bash
file=$1
# Remove the file extension, leaving only the file name
filename=$(echo "$1" | cut -f 1 -d '.')
if [ -z $1 ]
then
    echo "no file appointed!"
    exit 1
fi
echo compiling $file
echo output name: $filename
# 11.6.2 Both brackets can be used for advanced processing of strings
if [[ $file=="*.cpp" ]]
then
    g++ "$file" -o $filename
    ./$filename
elif [[ $file=="*.c" ]]
then
    gcc "$file" -o $filename
    ./$filename
else
    echo "invaild file!"
fi


test:

[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# sh 
no file appointed!

[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# sh   
compiling 
output name: test1
I'm test1 file

[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# sh  
compiling 
output name: test2
THIS IS TEST2 FILE

[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# sh   
compiling 
output name: testinp
input a number:50
number is :50[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]#

Upgrade to global command

vim openroot/.bashrcFolders

Then write it inalias cppr=sh /path/to/

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

export NVM_DIR="/www/server/nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

alias cppr='sh ~/myScripts/'

In the future, you can directly execute c/c++ files by cppr command, which is comfortable.

Test it out:

[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# cppr
no file appointed!
[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# cppr 
compiling 
output name: test1
I'm test1 file
[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# cppr 
compiling 
output name: test2
THIS IS TEST2 FILE
[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# cppr 
compiling 
output name: testinp
input a number:
46
number is : 
46[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# 

Problem record

Distinguish file types

According to the current knowledge you have learned, you can use itif [[ expr ]]The method is completed. According to "Linux Command Line and Shell Programming" 11.6.2, both brackets can be used for advanced processing of strings.

Exclude filename extensions

​ Scripts must not only be able to distinguish c and cpp files, but also remove the file name extension.*sThe previous search found a cut command:

# -d is used to set the splitter -f is used to specify the fields to select printing. Here, the two fields trun and exe are separated by the delimiter as the boundary.
[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# echo ""|cut -f2 -d'.' 
exe
[root@iZbp13zqzr3c74v3o1ry3mZ 13_userInput]# echo ""|cut -f1 -d'.'
trun