bash

10月12日任务

为君一笑 提交于 2021-02-16 19:53:25
8.6 管道符和作业控制 ctrl + z 可以将一个正在前台执行的命令放到后台,并且暂停, fg 将后台中的命令调至前台继续运行 jobs 查看当前有多少在后台运行的命令 bg 将一个在后台暂停的命令,变成继续执行 [root@aming-01 ~]# vim 1.txt [1]+ 已停止 vim 1.txt [root@aming-01 ~]# ls 111.txt Desktop Public anaconda-ks.cfg Documents Templates apr-util-1.6.1.tar.bz2 Downloads Videos apr-util-1.6.1.tar.bz2.gz Music 跟阿铭学Linux第三版.pdf a.txt Pictures [root@aming-01 ~]# fg [root@aming-01 ~]# jobs [1]+ 已停止 vim 1.txt Linux sleep命令可以用来将目前动作延迟一段时间。 8.7/8.8 shell变量 [root@aming-01 ~]# myname='Aming Li' [root@aming-01 ~]# echo $myname Aming Li 双引号""内的特殊字符如$可以保持原有的属性 [root@aming-01 ~]# myname="Aming's" [root

Linux实现自动登录

£可爱£侵袭症+ 提交于 2021-02-16 17:54:07
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄、收藏。可是为什么要这么写却不知其然。本文用一个最短的例子说明脚本的原理。   脚本代码如下: #!/usr/bin/ expect set timeout 30 spawn ssh -l username 192.168 . 1.1 expect " password: " send " ispass\r " interact   1. [#!/usr/bin/expect]   这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。   注意:这一行需要在脚本的第一行。   2. [set timeout 30]   基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒   3. [spawn ssh -l username 192.168.1.1]   spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir

changing the output filenames in [split] [closed]

纵饮孤独 提交于 2021-02-16 16:40:09
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . Improve this question I would like to split a huge text file into separate text files. I use: split -l 1000 file.txt split_file and I will get split_fileaa , split_filebb , etc. How can I change the extension to be split_file0.txt, split_file1.txt, etc ? 回答1: --additional-suffix is

How to execute the same loop for 1 hour in bash/ linux scripting?

◇◆丶佛笑我妖孽 提交于 2021-02-16 16:22:08
问题 I want to open and close some web pages with random intervals for an hour. So far i have written the innards FILE=webpages TIMES=0 while test $TIMES -lt 10 do #Picks random line(Website) from FILE TEMPWEB=`shuf -n 1 $FILE` echo "Opening" $TEMPWEB #Opens TEMPWEB xdg-open $TEMPWEB sleep 10 #Kills firefox ps -e | grep firefox | sed 's/\(.[0-9]*\).*/\1/' | xargs kill TIMES=$(($TIMES+1)) done So I'm am missing the while condition. I want something like: TIMER = 0h 0m 0s while( TIMER < 1 h) ....

Execute a passed alias inside a function?

扶醉桌前 提交于 2021-02-16 15:49:52
问题 Background: I'm trying make a function that runs commands on a set interval because I don't have access to a "watch" program. Simplified to it's most basic from, the function I'm trying to write is runit() { $1; } . What works: This works fine and dandy when I pass it things that aren't aliases. For example, runit "ls -l" works fine. I get the full output from the ls -l command. What doesn't work: The problem starts when I pass it an alias. For example, setting alias ll="ls -l" then calling

Bash Loop Through Variable With Rows and Columns

痞子三分冷 提交于 2021-02-16 15:30:36
问题 After searching around for many hours testing different solutions, I've yet to find a working solution. Bash and Shell Scripting is not my strongest area. I have a variable which has rows (new lines) and columns (tab separated). What I want to do is loop through the rows and get 'Column X' then put that item into a variable so I can do something with that Cell. i.e. echo out for example. There are an unknown number of rows, but known number of columns, so that it is possible to always get

bash return doesn't work as expect in echo “aa” | while read ln

雨燕双飞 提交于 2021-02-16 14:35:12
问题 #!/bin/bash function doSomething() { callee echo $? echo "It should go to here!" } function callee() { cat line.txt |while read ln do echo $ln if [ 1 ] ;then { echo "This is callee" && return 2; } fi done echo "It should not go to here!" } doSomething Below is the result aa This is callee It should not go to here! 0 It should go to here! Why "return" works like "break" ? I want it exit the function! not only break the loop ... 回答1: This is because you are using a pipe into a while loop, which

How do I split a text file into an array by blank lines?

眉间皱痕 提交于 2021-02-16 14:19:28
问题 I have a bash command that outputs text in the following format: Header 1 - Point 1 - Point 2 Header 2 - Point 1 - Point 2 Header 3 -Point 1 - Point 2 ... I want to parse this text into an array, separating on the empty line so that array[0] for example contains: Header 1 - Point 1 - Point 2 And then I want to edit some of the data in the array if it satisfies certain conditions. I was looking at something like this Separate by blank lines in bash but I'm completely new to bash so I don't

Bash semicolon being equal to newline is not exactly true?

强颜欢笑 提交于 2021-02-16 14:12:25
问题 I've read in multiple articles that semicolon( ; ) in UNIX-like shells is equal to a new line. However, the following is confusing me and I'm having no luck googling it either. I'm guessing it's an issue with do in shell, but "bash semicolon do" is not exactly the most google-friendly search term combination. Below is a simple for statement. for i in {1..10} do echo "hi" echo "bye" done As many Stack Overflow gurus have posted, every newline can be substituted with semicolons. So.. we have

sed remove all capital letters

混江龙づ霸主 提交于 2021-02-16 13:54:12
问题 I am trying to delete all occurences of Capital Letters only in the following string with the sed command below but it is only outputting the sting that I enter - how do I put the substitution in correctly ? echo "Dog boy Did Good" | sed 's/\([A-Z]\+\)/\1/g' 回答1: echo "Dog boy Did Good" | sed 's/[A-Z]//g' 回答2: echo "Dog boy Did Good" | sed 's/[A-Z]//g' og boy id ood You substitute something (UPPERCASE) with nothing, and you don't need to group it, because you don't use it later, and you don't