我是靠谱客的博主 传统柚子,最近开发中收集的这篇文章主要介绍Linux-Linux Shell程序设计,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Linux Shell程序设计

(1)查看/etc/.profile文件:相当于DOS下autoexe.bat

root@ubuntu:/etc# cat profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    PS1='u@h:w$ '
    if [ -f /etc/bash.bashrc ]; then
	. /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

umask 022

(2)通配符“*”、“?”、“[]”的使用

root@ubuntu:/# ls test*
test  testchmod  testchown  testgzip.gz  testln  testtar  testunzip  testvi

testdu:
1  2  3  aaa  bbb  ccc

testmount:

root@ubuntu:/# ls te?t
test

root@ubuntu:/# ls [t]est*
test  testchmod  testchown  testgzip.gz  testln  testtar  testunzip  testvi

testdu:
1  2  3  aaa  bbb  ccc

testmount:

$ls [a-c]* 和 $ls [a,m,t]*命令

root@ubuntu:/# ls [a-c]*
bin:
bash                  dumpkeys    mountpoint        sh.distrib
bunzip2               echo        mt                sleep
busybox               ed          mt-gnu            static-sh
bzcat                 egrep       mv                stty
bzcmp                 false       nano              su
bzdiff                fgconsole   nc                sync
bzegrep               fgrep       nc.openbsd        tailf
bzexe                 fuser       netcat            tar
bzfgrep               fusermount  netstat           tempfile
bzgrep                grep        nisdomainname     touch
bzip2                 gunzip      ntfs-3g           true
bzip2recover          gzexe       ntfs-3g.probe     ulockmgr_server
bzless                gzip        ntfs-3g.secaudit  umount
bzmore                hostname    ntfs-3g.usermap   uname
cat                   ip          open              uncompress
chgrp                 kbd_mode    openvt            unicode_start
chmod                 kill        pidof             vdir
chown                 less        ping              which
chvt                  lessecho    ping6             ypdomainname
cp                    lessfile    plymouth          zcat
cpio                  lesskey     ps                zcmp
dash                  lesspipe    pwd               zdiff
date                  ln          rbash             zegrep
dbus-cleanup-sockets  loadkeys    readlink          zfgrep
dbus-daemon           login       rm                zforce
dbus-uuidgen          ls          rmdir             zgrep
dd                    lsmod       rnano             zless
df                    mkdir       run-parts         zmore
dir                   mknod       sed               znew
dmesg                 mktemp      setfont
dnsdomainname         more        setupcon
domainname            mount       sh

boot:
abi-2.6.32-21-generic         memtest86+.bin
config-2.6.32-21-generic      System.map-2.6.32-21-generic
grub                          vmcoreinfo-2.6.32-21-generic
initrd.img-2.6.32-21-generic  vmlinuz-2.6.32-21-generic

cdrom:

root@ubuntu:/# ls [a,m,t]*
mytesttar.tgz    test       testchown    testln   testunzip
mytestunzip.zip  testchmod  testgzip.gz  testtar  testvi

media:
floppy  floppy0

mnt:
hgfs

testdu:
1  2  3  aaa  bbb  ccc

testmount:

tmp:
keyring-ar8Oug      pulse-PKdhtXMmr18n         vmware-root
orbit-gdm           ssh-xSFSsS1780             vmware-xushicheng
orbit-xushicheng    virtual-xushicheng.Z0J8GO
pulse-d1Pj7lDqeQHy  VMwareDnD

(3)重定向和管道的使用
ls | more

root@ubuntu:/# ls | more
bin
boot
cdrom
dev
etc
home
initrd.img
lib
link2_testln
link_testln
lost+found
media
mnt
mytesttar.tgz
mytestunzip.zip
opt
proc
root
sample.txt
sbin
--More--

        cat > test.txt
root@ubuntu:/# cat > test.txt
aaa
bbb
ccc
q
exit
quit
^C
root@ubuntu:/# cat test.txt
aaa
bbb
ccc
q
exit
quit

(4)变量
$lookup=/usr/mydir
$echo $lookup
$export lookup:让进程使用

root@ubuntu:/# lookup="Hello World"
root@ubuntu:/# echo $lookup
Hello World

(5)编辑并运行以下程序(shell程序控制结构)
1 fortest

        #!/bin/bash
        for a in x y z
		do
        echo now a=$a
		done

root@ubuntu:/# vi fortest.sh
root@ubuntu:/# . fortest.sh
now a=x
now a=y
now a=z

2 fortest1

        #!/bin/bash
        for a
		do
        echo now a=$a
		done
root@ubuntu:/# vi fortest1.sh
root@ubuntu:/# cat fortest1.sh
#!/bin/bash

for a

do

echo now a=$a

done
root@ubuntu:/# . fortest1.sh
root@ubuntu:/#

3 functest

        #!/bin/bash
		setup()
		{
			echo setup…
		}
		do_date()
		{
			date
		}
		chgdir()
		{
			cd $1
		}
		do_date
		setup
		chgdir
root@ubuntu:/# vi functest.sh
root@ubuntu:/# . functest.sh
Thu May 30 20:34:21 CST 2019
setup…
	   4 paramtest
		#!/bin/bash
		echo filename:$0
		echo arguments:$*
		echo number arg:$#
		echo arg2:$2
		shift
		echo number arg:$#
		echo arg2:$2
set hello,everone
echo args:$*
echo arg2:$2

root@ubuntu:~# vi paramtest
root@ubuntu:~# . paramtest
filename:bash
arguments:
number arg:0
arg2:
number arg:0
arg2:
args:hello,everone
arg2:

5 untiltest

		#!/bin/bash
number=0
until ( test $number -gt 5 )  
do
	    echo "$number"
	    number=`expr $number + 1 `
done
root@ubuntu:/# . untiltest
0
1
2
3
4
5

最后

以上就是传统柚子为你收集整理的Linux-Linux Shell程序设计的全部内容,希望文章能够帮你解决Linux-Linux Shell程序设计所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部