iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >Linux命令行和shell脚本编程宝典 Richard Blum
  • 963
分享到

Linux命令行和shell脚本编程宝典 Richard Blum

命令行脚本宝典 2022-06-04 21:06:36 963人浏览 八月长安
摘要

第一个脚本文件 #!/bin/bashecho "This is my first bash code!"exit 0 重定向符号和数学计算 #!/bin/bashecho -n "The time and

第一个脚本文件


#!/bin/bash
echo "This is my first bash code!"
exit 0

重定向符号和数学计算

#!/bin/bash
echo -n "The time and date are: "
date
value1=100 #等号前后不允许出现空格
value2=$value1
echo -n "value1="
echo $value1
echo -n "value2="
echo $value2
ls -l | sort > out.txt #管道符号(|)和重定向输出符号>
ls -l >> out.txt #重定向追加输出符号>>
echo -n "wc<out.txt:"
wc < out.txt #重定向输入符号<
echo "sort<<EOF ... EOF"
sort << EOF #内置输入重定向<<
`date`
EOF
#数学计算
echo -n "expr进行计算:1+5="
expr 1+5
echo -n "使用方括号进行计算:1+5="
echo $[1+5]
echo "使用bc计算器进行浮点运算"
var1=100
var2=200
var3=`echo "scale=4;$var1/$var2" | bc`
echo "$var1 / $var2 = $var3"
var4=71
var5=`bc<<EOF
scale=4
a1=($var1*$var2)
b1=($var3*$var4)
a1+b1
EOF`
echo "var5=$var5"
exit 0

使用test命令

#!/bin/bash
#使用test命令
var1=10
var2=100
if [ $var1 -gt $var2 ]
then
echo "var1 grate var2"
else
echo "var2 grate var1"
fi
#只能比较整数
test_user=hanxi
if [ $USER = $test_user ]
then
echo "Welcome $test_user"
fi
str1=Hanxi
str2=hanxi
if [ $str1 > $str2 ]
then
echo "$str1 > $str2"
else
echo "$str1 < $str2"
fi
if [ -n $str1 ]
then
echo "The string '$str1' is not empty"
else
echo "the string '$str1' is empty"
fi
#检查文件目录
if [ -d $HOME ]
then
echo "your Home dir exists"
cd $HOME
ls -a
else
echo "there's a problem with your HOME dir"
fi
pwfile=/etc/shadow
if [ -f $pwfile ]
then
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry, I'm unable to reas the $pwfile file "
fi
else
echo "Sorry, the file $pwfile doesn't exist"
fi
if [[ $USER == h* ]]
then
echo "Hello $USER"
else
echo "Sorry, I don't know you"
fi

循环语句

#!/bin/bash
for file in /home/hanxi/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done
#文件数据的循环
IFSOLD=$IFS
IFS=$'n'
for entry in `cat /etc/passwd`
do
echo "Values in $entry -"
IFS=:
for value in $entry
do
echo " $value"
done
done | more
for file in /home/hanxi/*
do
if [ -d "$file" ]
then
echo "$file is directory"
elif
echo "$file is a file"
fi
done > output.txt

读取参数

#!/bin/bash
name=`basename $0`
echo the commane entered is : $name
c_args=$#
echo count args:$c_args
#取最后一个参数
echo the last parameter is ${!#}
echo all parameter: $*
echo all parameter: $@
count=1
for param in "$@"
do
echo "$@ parameter #$count = $param"
count=$[ $count + 1 ]
done
#getopts
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option";;
*) echo "Unknown option : $opt";;
esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
read -p "Please enter your age:" age
echo age:$age
if read -t 5 -p "Please enter your name: " name
then
echo "Hellp $name,welcome to my script"
else
echo
echo "sorry ,too slow!"
fi
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
echo " fine, continue on...";;
N | n) echo
echo OK,Good bye
exit;;
esac
echo "This is the end of the script"
read -s -p "Enter your passWord: " pass
echo
echo "Is your password really $pass?"
#读取文件
count=1
cat for.txt | while read line
do
echo "Line $count: $line"
count=$[ $count+1 ]
done
echo "Finished processing the file"

重定向文件描述符

#!/bin/bash
#永久重定向
exec 9>&2
exec 2>testerror
echo "this will in testerror">&2
exec 2<&9
exec 9<&0
exec 0<testin
count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done
exec 0<&9
#重定向文件描述符
exec 3>&1
exec 1>testout
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to nomarl"
exec 4<&0
exec 0<testin
count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done
exec 0<&4
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "continue...";
esac
#创建读写文件描述符
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#关闭文件描述符
exec 8>&-
#列出文件描述服
#`/usr/sbin/lsof -a -p $$`|more
#禁止命令输出
#2 > /dev/null
#创建本地临时文件
tempfile=`mktemp test.XXXXXX`
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#在/temp中创建临时文件
tmpfile=`mktemp -t tmp.XXXXXX`
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#创建临时文件夹
tmpdir=`mktemp -d dir.XXXXXX`
cd $tmpdir
tempfile1=`mktemp temp.XXXXXX`
ls -l
cd ..
#记录消息
a=`date | tee testfile;
cat testfile;
date | tee -a testfile;
cat testfile`

信号处理

#!/bin/bash
#信号处理
trap "echo 'get a sign'" SIGINT SIGTERM
trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 10
count=$[ $count+1 ]
done
echo "This is the end of the test program"
trap - EXIT#移除捕获
#后台牧师运行
#./test6.sh &
#不使用终端的情况下运行脚本
#nohup ./test6.sh &
#查看作业
#jobs
#重新启动作业
#bg 2(作业序号)//后台
#fg 2//前台
#优先级
#nice -n 10 ./test6.sh
#renice 10 -p 25904(进程号)
#预计时间运行at命令
#at -f test6.sh 20:00
#batch命令,系统平均负载低于0.8时运行,可以设定时间,比at命令更好
#corn表格可以设定循环运行,格式:
#min hour dayofmonth month dayofweek command
#每个月第一天运行:
#12 16 * * 1 command
#每个月最后一天运行:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command

函数的使用

#!/bin/bash
#函数
#使用返回值
function func1
{
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#传递参数
function func2
{
echo $[ $1+$2 ]
}
result=`func2 2 2`
echo "the new result is $result"
#局部变量, 递归
function func3
{
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1-1 ]
local result=`func3 $temp`
echo $[ $result*$1 ]
fi
}
read -p "Enter value:" value
result=`func3 $value`
echo "the factorial of $value is: $result"
#调用当前目录下到函数库
#. ./myfuncs

--结束END--

本文标题: Linux命令行和shell脚本编程宝典 Richard Blum

本文链接: https://www.lsjlt.com/news/18179.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Linux命令行和shell脚本编程的示例分析
    小编给大家分享一下Linux命令行和shell脚本编程的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!第一个脚本文件代码如下:#!/bin/bashech...
    99+
    2023-06-09
  • 如何用PHP执行shell 脚本和 shell命令
    执行 shell 脚本: 或者首先使用 ssh2_connect 函数连接到远程主机,然后使用 ssh2_auth_password 函数进行身份验证。接下来,使用 ssh2_exec 函数执行 shell 脚本,将其输出流作为结果返回。...
    99+
    2023-08-31
    php linux 开发语言 Powered by 金山文档
  • 怎么远程执行Linux脚本和命令
    这篇文章主要介绍“怎么远程执行Linux脚本和命令”,在日常操作中,相信很多人在怎么远程执行Linux脚本和命令问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么远程执行Linux脚本和命令”的疑惑有所帮助!...
    99+
    2023-06-03
  • 如何远程执行Linux脚本和命令
    小编给大家分享一下如何远程执行Linux脚本和命令,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!对于 paramiko 安装直接 pip 或者 PyCharm 这...
    99+
    2023-06-16
  • Linux 查询正在运行的shell脚本命令
    查看当前运行的所有进程。 ps -A 如果太多了找不到,看的眼花,可以加条件 grep是分组 查看正在运行的shell脚本的进程shell脚本就是 sh ps -ef |grep  sh 如图下面就是查询出来的所有sh脚本,看第...
    99+
    2023-09-03
    linux 运维 服务器
  • linux怎么编写和执行shell脚本
    编写和执行Shell脚本的步骤如下:1. 打开一个文本编辑器,比如使用vi或者nano。2. 在第一行添加shebang,指定要使用...
    99+
    2023-09-22
    linux shell
  • 有哪些ssh远程执行命令方法和Shell脚本
    本篇内容主要讲解“有哪些ssh远程执行命令方法和Shell脚本”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“有哪些ssh远程执行命令方法和Shell脚本”吧!ssh执行远程操作命令格式代码如下:...
    99+
    2023-06-09
  • 怎么在shell脚本中执行hive和sqoop命令
    这篇文章将为大家详细讲解有关怎么在shell脚本中执行hive和sqoop命令,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1、test.sh脚本内容如下:#!/bin/bash#CURR_...
    99+
    2023-06-09
  • 如何在shell中使用expect命令进行远程执行命令脚本
    如何在shell中使用expect命令进行远程执行命令脚本?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。expect是用来实现自动交互功能的工具之一,使用expect-send...
    99+
    2023-06-09
  • node.js在Linux下执行shell命令、.sh脚本的问题
    首先,引入子进程模块 var process = require('child_process'); 执行shell命令 调用该模块暴露出来的方法exec process.exec(...
    99+
    2024-04-02
  • Linux Shell脚本多命令执行逻辑的示例详解
    目录简介一、分号二、&&三、||案例剖析简介 Linux 中可以使用分号";“、双and号”&&“和双竖...
    99+
    2022-11-13
    Shell脚本多命令执行逻辑 Shell 多命令执行逻辑 Shell 多命令执行
  • linux中shell脚本编写和运行的示例分析
    这篇文章主要介绍了linux中shell脚本编写和运行的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。编写第一个shell脚本在gedit中编写.sh格式的文件,保存...
    99+
    2023-06-09
  • shell脚本编程在UNIX和Linux下有什么区别
    这篇文章主要介绍了shell脚本编程在UNIX和Linux下有什么区别,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。与其他 UNIX 操作系统和 Linux 一样,IBM A...
    99+
    2023-06-16
  • 如何解决node.js在Linux下执行shell命令、.sh脚本的问题
    小编给大家分享一下如何解决node.js在Linux下执行shell命令、.sh脚本的问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!首先,引入子进程模块var process = requ...
    99+
    2023-06-28
  • Linux 脚本编写基础知识以及在shell脚本中可以使用三类命令介绍
    本篇内容主要讲解“Linux 脚本编写基础知识以及在shell脚本中可以使用三类命令介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux 脚本编写基础知识以及在shell脚本中可以使用三...
    99+
    2023-06-09
  • shell脚本如何实现同时多台远程主机执行命令
    这篇文章主要介绍shell脚本如何实现同时多台远程主机执行命令,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!实现需求在对单台机器做操作时我们会用“ssh ip”的方式登录到机器上,可以写这样一个工具vssh ip1,...
    99+
    2023-06-09
  • linux中shell脚本实现root切换到普通用户执行脚本或命令的示例分析
    这篇文章将为大家详细讲解有关linux中shell脚本实现root切换到普通用户执行脚本或命令的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。需求:安装deb包,设置程序安装后启动,不需要root...
    99+
    2023-06-09
  • 【Linux】Linux环境变量的理解 --- 命令行参数、shell子进程、环境变量、本地变量…
    加油布鲁斯,你能行的! 文章目录 一、环境变量PATH中的系统默认搜索路径1.将程序安装到/usr/bin目录(不带./运行自己写的程序)2.将程序路径添加到PATH环境变量里面(不带./运行自己写的程序) 二、环境变量的深度...
    99+
    2023-08-25
    linux 服务器 运维
  • 如何在Unix系统中使用Shell命令来进行重定向和Go语言编程?
    在Unix系统中,Shell命令是一种非常强大的工具,它可以帮助我们完成许多任务。其中,重定向是一项非常有用的功能,它可以将命令的输出重定向到文件或其他设备中。此外,如果你想学习Go语言编程,那么在Unix系统中使用Shell命令可以帮助你...
    99+
    2023-06-24
    重定向 shell unix
  • Unix系统中的Go编程:如何使用Shell命令进行重定向和处理输入输出?
    Unix系统和Go编程语言在现代计算机应用中都有广泛的应用。在Unix系统中,Shell命令是非常重要的,它们可以用来控制进程、文件和输入输出等。在Go编程语言中,我们可以使用os包和os/exec包来处理Shell命令的输入输出和重定向...
    99+
    2023-06-24
    重定向 shell unix
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作