Linux进程,shell scripts相关培训练习

Managing Running Processes

  • Run a vi session with nice value 10
# vim test&
[1] 14771
# renice +10 14771
14771 (进程 ID) 旧优先级为 0,新优先级为 10
  • Open another session and list all the process given by your user by top command and sort them by nice value, find your vi process and process ID

Open a new session first

#top -o NI
14771 root      30  10  149176   4700   2472 T  0.0  0.2   0:00.03 vim
  • List all the processes with ps command, display your desired output fields and sort them by nice value, find your vi process and process ID
#ps aux --sort -ni
#ps aux --sort -ni|grep vim

Writing Simple Shell Scripts

  • Write a script to print out all arguments given to your script, one per line, in the format like below. The number of parameters given to the script may be any number.
#vim sh03.sh

#!bin/bash

read -p "How many number would you like to input?:" num

for((a=1; a<=$num; a++)); do
    read -p "please input nmuber$a:" num$a
done

for((b=1; b<=$num; b++)); do
    echo -e "\$$b=$[num$b]"
done

Let’s try

# bash sh03.sh
How many numbers would you like to input?:5
please input nmuber1:12
please input nmuber2:44
please input nmuber3:22
please input nmuber4:31
please input nmuber5:45
$1=12
$2=44
$3=22
$4=31
$5=45

PS.
When I use ./sh03.sh, it will display that

-bash: ./sh03.sh: bin/bash: 坏的解释器: 没有那个文件或目录

but it works when I use bash sh03.sh I don’t know the reason.


尝试了培训过程中的一点疑问

*Flow control in shell - conditional execution

想尝试以下命令

[ test] && commands1||commands2 

于是写了如下脚本

# vim sh04.sh
#!bin/bash

read -p "input a num:" num
read -p "input anthor num:" num2

[ "$num" -gt "$num2" ]&&echo -e "left"||echo -e "right"

尝试了一下脚本,

#bash sh04.sh
input a num:4
input anthor num:5
right

#bash sh04.sh
input a num:5
input anthor num:4
left

想知道如果把&&,||交换,会怎么样,所以把脚本改了一下

# vim sh05.sh
#!bin/bash

read -p "input a num:" num
read -p "input anthor num:" num2

[ "$num" -gt "$num2" ]||echo -e "left"&&echo -e "right"

尝试一下,

#bash sh05.sh
input a num:4
input anthor num:5
left
right

#bash sh05.sh
input a num:5
input anthor num:4
left  

**right

看来是条件不成立就执行两个commands,成立就执行左边的。
还是没有弄明白这里的逻辑;不成立的明白了,当条件不成立的时候,||代表或,&&是和,所以一起执行后面两个命令;但是按照我的理解,如果条件成立,应该像

[ test ] && commands 

一样,执行&&后的命令,但是事实上执行的是&&之前的。
执行的是之后的,我之前看错了


以上就是我的作业和一些想法。

猜你喜欢

转载自blog.csdn.net/LuYiming_Ben/article/details/81569898