1. How to create a command to input h1 and print out hello world ?

vim ~/.bashrc
function h1(){
echo hello world
}
Source ~/.bashrc (. ~/.bashrc)

Then type h1 in the terminal.

1.1 How to remove h1 command added by source

unset h1

2. How to write a script to add the h1 command to output hello word?

open a file called h2 and edit it.

Vim ./h2

Write a script as follows.

#!/bin/bash
Echo hello world
echo $PATH
pwd
export Path=”$PWD:$PATH”
chmod +x h2

(or ./h2)

3. How to locate the path of a file?

whereis h2 or just type which h2

4. How to check the type of a command?

type h2

5. How to check how to use a command?

Man echo
help echo
tldr echo

6. What type could a bash command be?

  • brought by shell buildin/shell itself

  • Offered by system

  • customized function

  • written executable scripts

7. How to deal with input with space in a script?

name=$* or name=$@

8. What are the differences of $* and ¥@ ?

then $@ will pass your parameters as separate parameters, whereas $* will just pass all params as a single parameter.

9. What does $0 represent?

$0 Expands to the name of the shell or shell script.

10. How to out put varaibles?

image.png

  • variables

    image.png

  • function or the variables of a script

    image.png

  • env variables

    image.png

11. What does $# mean?

$# refer to The value of the total number of command line arguments passed.

12. How to output a time with format?

date "+%Y-%m-%d %H:%M:%S"

13. How to echo the result of other command?

echo $( date "+%Y-%m-%d %H:%M:%S")

image.png

14. How to search several characters in a file

grep judi name.list

15. How to get value of first and second column of the file?

cut -d' ' -f1 name.list
cut -d' ' -f2 name.list

-d : delimiter

-f: field

15.1 How to search some words and output the second column?

grep jdu name.list | cut -d' '  -f2
grep jdu name.list | cut -d' '  -f2-3

A pipeline is a sequence of one or more commands separated by one of the control operators | or |&.

16. How to write a script to search a name by id and hide the error?

TS=$(date "+%Y-%m-%d %H:%M:%S")
ID=$1
name=$(grep $ID name.list 2>/dev/null | cut -d' ' -f2)
if [[ "" == "$name" ]]
then
   name="unkown"
fi
echo "[$TS] hello $name"

FYI:[[ or ]] has space before and after


> file redirects stdout to file

1> file redirects stdout to file

2> file redirects stderr to file (Standard error)

&> file redirects stdout and stderr to file

> file 2>&1 redirects stdout and stderr to file

/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.

Note that > file 2>&1 is an older syntax which still works, &> file is neater, but would not have worked on older systems.

18. What is piping?(|)

Piping: a mechanism for sending data from one program to another

19. How to put current date into a new log file?

date >data.log

19.1 How to amend a date to a log file?

date >>data.log

19.2 How to collect standard error into a log file

date 2>data.log

19.3 How to collect standard and normal info into a log file?

Abc > data.log 2>&1

19.4 How to store standard and normal info separately?

Abc >data.log 2>erroe.log

20. What difference of cut -d' ' -f2 name.list and cut -d' ' -f2 <name.list?

The former reads data from a file, the latter from standard input provided by the shell

21. How to view all processes running under root?

Ps aux | grep ^root |less -N
Ps aux | grep ^root |cat -n
Ps aux | grep ^root |wc -l
Less -N (Add ids to each line)

23. How to output only the first 10 rows or the last 10 rows of history?

History | head 
History | tail
History | head -3
History | tail -3

24. How to count the history commands?

history | sed -E 's/^ +//' | cut -d' ' -f3 | sort | uniq -c | sort -n

25. What is the difference between exit0 and exit1 in Shell?

It means that the result of running the program is different.

exit (0): Run the program normally and exit the program;

exit (1): abnormal operation leads to exit the program;

26. How to save and exit after finishing a file in Vim?

Esc->:wq

Last updated: 2024-03-14 12:03:48Bash
Author:Chaolocation:https://www.baidu.com/article/14
Comments
Submit
Be the first one to write a comment ~