CP1.Beginning bash

1.which factors drove the evolution of the Unix shell?

user convenience and programming

2. Can't I write a program to do this for me ?

A shell is also a programming language that's specially designed to work with your computer system's commands.

3. The bash Shell

bash is a shell: a command interpreter .The main purpose of bash (or of any shell) is to allow you to interact with the computer's operating system so that you can accomplish whatever you need to do.

$ in the prompt generally mean you are logged in as a regular user

# mean you are root, the root account is the adminstrator of the system. root is all-powerful and can do anything on a typical Unix or Linux system.

~ means you are in your home directory.

4. Show Where You Are

solution: pwd

[root@DBAMAXWELL learning_bash_shell]# pwd
/root/learning_bash_shell
[root@DBAMAXWELL learning_bash_shell]#

5. Finding and Running Commands

Try the type, which,apropos ,locate,slocate,find, and ls commands.

[root@DBAMAXWELL learning_bash_shell]# type which
which is a function
which () 

    ( alias;
    eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
}
[root@DBAMAXWELL learning_bash_shell]# type ls
ls is aliased to `ls --color=auto'
[root@DBAMAXWELL learning_bash_shell]# type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
[root@DBAMAXWELL learning_bash_shell]# which which
which ()

    ( alias;
    eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
}
[root@DBAMAXWELL learning_bash_shell]# 

apropos music

man -k music

locate apropos

help type

man which

man apropos

man locate

man slocate

man find

man ls

6. Getting Information About Files.

Solution : ls , stat, file, or find commands.

[root@DBAMAXWELL learning_bash_shell]# touch /tmp/sample_file
[root@DBAMAXWELL learning_bash_shell]# ls /tmp/sample_file   
/tmp/sample_file
[root@DBAMAXWELL learning_bash_shell]# ls -l /tmp/sample_file
-rw-r--r-- 1 root root 0 Mar  6 11:50 /tmp/sample_file
[root@DBAMAXWELL learning_bash_shell]# stat /tmp/sample_file
  File: /tmp/sample_file
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 8591        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-03-06 11:50:59.619014318 +0800
Modify: 2022-03-06 11:50:59.619014318 +0800
Change: 2022-03-06 11:50:59.619014318 +0800
 Birth: 2022-03-06 11:50:59.619014318 +0800
[root@DBAMAXWELL learning_bash_shell]# 

7.Showing All Hidden (dot) File in the Current Directory

Solution : 

[root@DBAMAXWELL learning_bash_shell]# ls -d .*
.  ..
[root@DBAMAXWELL learning_bash_shell]# ls -d .b*
ls: cannot access '.b*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# ls -d .[!.]*
ls: cannot access '.[!.]*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# grep -l 'PATH' ~/.[!.]*
/root/.bash_history
/root/.bash_profile
grep: /root/.cache: Is a directory
grep: /root/.cassandra: Is a directory
grep: /root/.pip: Is a directory
grep: /root/.ssh: Is a directory
[root@DBAMAXWELL learning_bash_shell]# 

[root@DBAMAXWELL learning_bash_shell]# ls -a
.  ..
[root@DBAMAXWELL learning_bash_shell]# ls -d .[!.]*
ls: cannot access '.[!.]*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# ls -d .[!.]*
ls: cannot access '.[!.]*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# ls -d .??*
ls: cannot access '.??*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# ls -d .[!.]* .??* | sort -u
ls: cannot access '.[!.]*': No such file or directory
ls: cannot access '.??*': No such file or directory
[root@DBAMAXWELL learning_bash_shell]# man ls

8.Using Shell Quoting

[maxwell@DBAMAXWELL ~]$ echo "$USER won't pay $5 for coffee."
maxwell won't pay  for coffee.
[maxwell@DBAMAXWELL ~]$ echo "$USER won't pay \$5 for coffee."
maxwell won't pay $5 for coffee.
[maxwell@DBAMAXWELL ~]$ echo 'I won'\''t pay $5 for coffee.'
I won't pay $5 for coffee.
[maxwell@DBAMAXWELL ~]$ 

9. Using or Replacing Built-ins and External Commands

Solution: 

[maxwell@DBAMAXWELL ~]$ type cd
cd is a shell builtin
[maxwell@DBAMAXWELL ~]$ type awk
awk is /usr/bin/awk
[maxwell@DBAMAXWELL ~]$ which cd
/usr/bin/cd
[maxwell@DBAMAXWELL ~]$ which awk
/usr/bin/awk
[maxwell@DBAMAXWELL ~]$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.
    
    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.
    
    Options:
      -d        output short description for each topic
      -m        display usage in pseudo-manpage format
      -s        output only a short usage synopsis for each topic matching
                PATTERN
    
    Arguments:
      PATTERN   Pattern specifiying a help topic
    
    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.
[maxwell@DBAMAXWELL ~]$ man which
WHICH(1)                                       General Commands Manual                                       WHICH(1)

NAME
       which - shows the full path of (shell) commands.

SYNOPSIS
       which [options] [--] programname [...]

DESCRIPTION
       Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executa‐
       bles that would have been executed when this argument had been entered at the shell prompt. It does this by
       searching for an executable or script in the directories listed in the environment variable PATH using the
       same algorithm as bash(1).

       This man page is generated from the file which.texinfo.

OPTIONS
       --all, -a
           Print all matching executables in PATH, not just the first.

       --read-alias, -i
           Read aliases from stdin, reporting matching ones on stdout. This is useful in combination with using an
           alias for which itself. For example
           alias which=´alias | which -i´.

       --skip-alias
           Ignore option `--read-alias´, if any. This is useful to explicity search for normal binaries, while using
           the `--read-alias´ option in an alias or function for which.

       --read-functions
           Read shell function definitions from stdin, reporting matching ones on stdout. This is useful in combina‐
           tion with using a shell function for which itself.  For example:
[maxwell@DBAMAXWELL ~]$ 
[maxwell@DBAMAXWELL ~]$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.
    
    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.
    
    Options:
      -d        output short description for each topic
      -m        display usage in pseudo-manpage format
      -s        output only a short usage synopsis for each topic matching
                PATTERN
    
    Arguments:
      PATTERN   Pattern specifiying a help topic
    
    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.
[maxwell@DBAMAXWELL ~]$ help builtin
builtin: builtin [shell-builtin [arg ...]]
    Execute shell builtins.
    
    Execute SHELL-BUILTIN with arguments ARGs without performing command
    lookup.  This is useful when you wish to reimplement a shell builtin
    as a shell function, but need to execute the builtin within the function.
    
    Exit Status:
    Returns the exit status of SHELL-BUILTIN, or false if SHELL-BUILTIN is
    not a shell builtin..
[maxwell@DBAMAXWELL ~]$ help command
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.
    
    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.
    
    Options:
      -p    use a default value for PATH that is guaranteed to find all of
            the standard utilities
      -v    print a description of COMMAND similar to the `type' builtin
      -V    print a more verbose description of each COMMAND
    
    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.
[maxwell@DBAMAXWELL ~]$ help enable
enable: enable [-a] [-dnps] [-f filename] [name ...]
    Enable and disable shell builtins.
    
    Enables and disables builtin shell commands.  Disabling allows you to
    execute a disk command which has the same name as a shell builtin
    without using a full pathname.
    
    Options:
      -a        print a list of builtins showing whether or not each is enabled
      -n        disable each NAME or display a list of disabled builtins
      -p        print the list of builtins in a reusable format
      -s        print only the names of Posix `special' builtins
    
    Options controlling dynamic loading:
      -f        Load builtin NAME from shared object FILENAME
      -d        Remove a builtin loaded with -f
    
    Without options, each NAME is enabled.
    
    To use the `test' found in $PATH instead of the shell builtin
    version, type `enable -n test'.
    
    Exit Status:
    Returns success unless NAME is not a shell builtin or an error occurs.
[maxwell@DBAMAXWELL ~]$ help type
type: type [-afptP] name [name ...]
    Display information about command type.
    
    For each NAME, indicate how it would be interpreted if used as a
    command name.
    
    Options:
      -a        display all locations containing an executable named NAME;
                includes aliases, builtins, and functions, if and only if
                the `-p' option is not also used
      -f        suppress shell function lookup
      -P        force a PATH search for each NAME, even if it is an alias,
                builtin, or function, and returns the name of the disk file
                that would be executed
      -p        returns either the name of the disk file that would be executed,
                or nothing if `type -t NAME' would not return `file'
      -t        output a single word which is one of `alias', `keyword',
                `function', `builtin', `file' or `', if NAME is an alias,
                shell reserved word, shell function, shell builtin, disk file,
                or not found, respectively
    
    Arguments:
      NAME      Command name to be interpreted.
    
    Exit Status:
    Returns success if all of the NAMEs are found; fails if any are not found.
[maxwell@DBAMAXWELL ~]$ 

10.Determining If You Are Running Interactively

11. Setting bash As Your Default Shell

[maxwell@DBAMAXWELL learning_bash_shell]$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[maxwell@DBAMAXWELL learning_bash_shell]$ chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/ksh
/bin/rksh
/usr/bin/ksh
/usr/bin/rksh
[maxwell@DBAMAXWELL learning_bash_shell]$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/ksh
/bin/rksh
/usr/bin/ksh
/usr/bin/rksh
[maxwell@DBAMAXWELL learning_bash_shell]$ man chsh
CHSH(1)                                             User Commands                                             CHSH(1)

NAME
       chsh - change your login shell

SYNOPSIS
       chsh [-s shell] [-l] [-h] [-v] [username]

DESCRIPTION
       chsh is used to change your login shell.  If a shell is not given on the command line, chsh prompts for one.

       chsh  supports non-local entries (kerberos, LDAP, etc.) if linked with libuser, otherwise use ypchsh, lchsh or
       any other implementation for non-local entries.

OPTIONS
       -s, --shell shell
              Specify your login shell.

       -l, --list-shells
              Print the list of shells listed in /etc/shells and exit.

       -h, --help
              Display help text and exit.

       -v, --version
              Display version information and exit.

VALID SHELLS
       chsh will accept the full pathname of any executable file on the system.  However, it will issue a warning  if
       the  shell  is  not listed in the /etc/shells file.  On the other hand, it can also be configured such that it
       will only accept shells listed in this file, unless you are root.

[maxwell@DBAMAXWELL learning_bash_shell]$ man passwd
PASSWD(1)                                           User utilities                                          PASSWD(1)

NAME
       passwd - update user's authentication tokens

SYNOPSIS
       passwd  [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin]
       [-?] [--usage] [username]

DESCRIPTION
       The passwd utility is used to update user's authentication token(s).

       This task is achieved through calls to the Linux-PAM and Libuser API.  Essentially, it initializes itself as a
       "passwd"  service  with  Linux-PAM  and utilizes configured password modules to authenticate and then update a
       user's password.

       A simple entry in the global Linux-PAM configuration file for this service would be:

        #
        # passwd service entry that does strength checking of
        # a proposed password before updating it.
        #
        passwd password requisite pam_cracklib.so retry=3
        passwd password required pam_unix.so use_authtok
        #

       Note, other module types are not required for this application to function correctly.

OPTIONS
       -k, --keep-tokens
              The option -k is used to indicate that the update should only  be  for  expired  authentication  tokens
              (passwords); the user wishes to keep their non-expired tokens as before.
[maxwell@DBAMAXWELL learning_bash_shell]$ man chpass
No manual entry for chpass
[maxwell@DBAMAXWELL learning_bash_shell]$

 12. Getting bash for Linux

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值