lunix shell 基礎常用整理

來源:互聯網
上載者:User

標籤:style   color   使用   io   ar   for   檔案   資料   art   

 1   ps  -ef    顯示正在啟動並執行進程,pid 等資訊
 UID PID PPID C STIME TTY TIME CMDroot 1 0 0 03:45 ? 00:00:02 init [5]root 2 1 0 03:45 ? 00:00:00 [migration/0]root 3 1 0 03:45 ? 00:00:00 [ksoftirqd/0]root 4 1 0 03:45 ? 00:00:00 [events/0]root 5 1 0 03:45 ? 00:00:00 [khelper]root 6 1 0 03:45 ? 00:00:00 [kthread]root 9 6 0 03:45 ? 00:00:00 [kblockd/0]root 10 6 0 03:45 ? 00:00:00 [kacpid]

2   df    -h    可讀顯示磁碟空間   M是兆Filesystem Size Used Avail Use% Mounted on/dev/sda2 18G 5.0G 12G 31% //dev/sda1 289M 16M 258M 6% /boottmpfs 506M 0 506M 0% /dev/shm
3  藉助df命令,可以方便地瞭解磁碟是否已空間不足,系統管理員需要瞭解空間不足時該怎麼辦。 我們可以使用du命令 ,du命令顯示特定目錄的磁碟使用方式,這是判斷系統是否存在磁碟佔用大戶的快捷方法。du    ,   du  -h(顯示M,G單位)  ,du  -c (匯總),du  -s(匯總所有,只顯示一個)du [ -a | -s ] [ -k ] [ -m ] [ -g ][ -l ] [ -r ] [ -x ] [ -H | -L ][ File ... ] 
8.0K ./test_flume/.flumespool24K ./test_flume8.0K ./mnt58M ./awk28K ./shell18M ./spark76M .
4   sort  (排序)  sort  file.txt
預設按字元排序數字排序   sort -n  file.txtsort -n file2023567917
sort  -M  file3.txt  (按照月份排序)
sort -t ‘:‘ -k 3 -n /etc/passwd  按指定分隔字元‘:" 第三個欄位進行排序  按數字排序的
du -sh * | sort -nr  按空間佔用從大到小 排序  包括檔案夾和檔案    r是指降序排列
5   gzip :用於壓縮檔
gzip  2.shgzip  file*gzip -r  test1  test2  壓縮2個檔案

tar  -cvf  my.tar  my.sh    tar壓縮tar -xvf  my。tar      tar解壓
tar  -zxvf filename.tgz
7 列印環境變數  printenv 
bashtest=testingecho $test
7  finger  llisc
/usr/sbin/groupadd shared
/usr/sbin/usermod -G shared test
檔案許可權表    755 



chmod o+r  newfile 將條目讀取添加到任何人
chmod u-x newfile 將條目山下湖使用者擁有的執行許可權chmod u+x newfile 
chown  修改使用者檔案擁有者 和所在組

9 shell  
rpm  -qa | sort  | more 
echo $?




if   運算式  返回0  執行成功thenfi

if   test [ condition ]

for  var  in Li72 Alibaba taobao Newbatilrdo  echo the state $testdone

使用轉移字元反斜線符合來轉移單引號使用雙引號來定義單引號的值

#!/bin/bashfor file in /home/bigdata/test/*  /home/li75/do  if [ -d "$file" ]then  echo "$file is a directory "elif [ -f "$file" ]then  echo "$file is a file "fidone



#!/bin/bashfor(( i=1; i<= 10; i++))do echo " The next number is $i"done

#!/bin/bash var1=10while [ $var1 -gt 0 ]do  echo $var1  var1=$[ $var1 - 1 ]done

#!/bin/bashvar1=100until [ $var1 -eq 0 ]do echo $var1  var1 =$[ $var1 -25 ]done

#!/bin/bashfor((a =1; a<=3; a++))do  echo "starting loop $a:"for((b=1;b<=3;b++))  do  echo " inside loop: $a*$b "donedone

breakcontince

#!/bin/bashfor(( a=1; a<4 ;a++))do  echo "Outer loop :$a" for((b=1;b<100;b++))do if [ $b -gt 4 ]then  break 2fiecho "Inner loop :$b"donedone > test.txt   重新導向到一個檔案        |  sort   排序

參數 位置參數$0 為為程式名稱 $1 為第一個參數$2 為第二個參數$3 為第三個參數

#!/bin/bashfactorial=1for((number =1;number<= $1;number++))do  factorial=$[ $factorial * $number ]doneecho  The factorial of $1 is  $factorial


name =` basename $0`擷取運行shell 指令碼名稱


if   [ -n "$1" ]判斷有沒有傳入參數
if  [ $# <5 ]判斷傳入參數個數
 擷取最後一個參數的值 params =$#
echo  parm : $params或者The  last   paramer ${!#}   

  $* 把所有的參數當作一個參數  [email protected] 把所有參數當作字串  分開處理

#!/bin/bashwhile [ -n "$1" ] do    case  "$1" in-a) echo "Found the -a  option" ;;-b) echo "Found the -b option" ;;-c) echo "Found the -c option";;*) echo "$1 is not an option";;  esac shiftdone




#!/bin/bashecho -n ‘Enter your name :"read nameecho "Hello $name,  welcome to my prorram ."

#!/bin/bashread -p "Please enter your age :" agedays =$[ $age * 365 ]echo " That makes you over $days days old !"
ls -al test test22 test3 badtest &>test7
#!/bin/bashecho "This is an error " >&2echo "This is normal output "
#!/bin/bashexec 1>testoutecho "This is a test of redirting all output"echo "without having to redirect every line "
#!/bin/bashexec 2>testerror
echo "This is the start of the script"echo "now reidirecting all output to another "exec 1>testout2
echo "This putput should go to the "echo "but shis should go to testerror file >&2


#!/bin/bashexec 0<testfile2count=1while read linedo   echo "Line #$count :$line"  count=$[ $count +1 ]done
lsof -a -p $$ -d 0,1,2
重新導向到空檔案  ls -al >/dev/null
cat /dev/null > testfile 
同時記錄檔案和列印到螢幕
date | tee testfile
追加資料
date | tee -a testfile
kill -9   進程id


#!/bin/bashtrap "echo HaHa " SIGINT SIGTEERMecho "THis is a test program "count=1while [ $count -le 10 ]do  echo "Loop #$count"sleep 10count=$[ $count +1  ]done echo "This is the end of test program "

把命令在後台運行用  &
sh 37.sh &
ps  au
有時需要從終端啟動會話,然後讓指令碼在結束之前以後台模式運行,即使退出終端會話有時如此可以用nohup
jobsjobs -rjobs -sjobs -l
nice  設定優先權
nice -n 10 sh 38.sh >test4out &


at  命令batch  命令cron  表格

#!/bin/bashtime=`date +%T`echo "This script ran at $time "echo "This is the end of the script">&2

corn   表格使用特殊格式制定作業的時間。
15 10 * * * command
查看登陸使用者配置的定時任務
crontab -l 
編輯任務corntab -e 

#!/bin/bashfunction func1 {  echo ‘This is an example of a function"  }count=1while [ $count -le 5 ]do func1 count=$[ $count +1 ]done echo "This is the end of the loop"func1 echo "Now  this is the end of the script "
函數的返回值$?
#!/bin/bashfunc1(){echo "trying to display a non"ls -ls 41.sh}echo "testing the function:"func1echo "The exit status is :$?"
#!/bin/bashfunction db1 {read -p "Enter a value :" value echo "doubling the value " return $[ $value * 2 ]}db1 echo "The new value is $?"

函數返回值
#!/bin/bashfunction db1{read -p "Enter a value :" valueecho $ [ $value *2 ]}
result=‘db1‘echo ‘The new value is $result"

提供函數檔案   可以用source  命令或稱(點操作符)將現有的庫函數引進.bashrc中
source   /home/li72/libraries/myfuncs.    /home/li72/libraries/myfuncs

function addem   {echo $[ $1 +$2 ]}
function multem {  echo $[ $1 * $2 ]}
function divem {  if [ $2 -ne 0 ]  then    echo $[ $1 /$2 ]else    echo -1fi}

#!/bin/bash. ./myfuncs
result=`addem 10 16`echo "The result is $result"
.bashrc  每次啟動shell 都會檢查該檔案  ,在已有檔案的末尾添加自訂函數
.  /home/bigdata/test/shell/myfuncs
文本處理sed    luinx 編輯替換
gawk

echo "This is a test " | sed ‘s/test/big test/‘
cat>dog.txtThe  quick brown fox jumps over the lazy dog .The  quick brown fox jumps over the lazy dog .The  quick brown fox jumps over the lazy dog .The  quick brown fox jumps over the lazy dog .The  quick brown fox jumps over the lazy dog .
sed ‘s/dog/cat/‘ dog.txt    The quick brown fox jumps over the lazy cat .The quick brown fox jumps over the lazy cat .The quick brown fox jumps over the lazy cat .The quick brown fox jumps over the lazy cat .The quick brown fox jumps over the lazy cat .

sed -e ‘> s/brown/green/> s/fox/elephant/> s/dog/cat/‘ dog.txt
The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .
從檔案讀取編輯器命令 cat>scripts/brown/green/s/fox/elephant/s/dog/cat/

sed -f script dog.txt
The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .The quick green elephant jumps over the lazy cat .

cat>script2{print $5 "‘s userid is " $1 }
添加使用者
useradd xuhui修改密碼usermod  -p xuhui xuhui
查看密碼檔案tail -l /etc/shadow

cd -  返回上一個訪問的目錄
尋找檔案命令
find  [路徑]  [選項]  [操作]



find . -name ‘test*‘    尋找 檔案名稱為 test開頭的

find . -mtime -90 -print  尋找更改時間在90天內的檔案
grep  [選項]  [模式]  [檔案]


在wodl.txt  找World單詞grep -w  ‘World‘ wodl.txt


















lunix shell 基礎常用整理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.