unix

How to replace every second occurence of a word in a text file

邮差的信 提交于 2020-04-15 20:56:26
问题 In a file called sample.txt , I have the following text: Once there is a tortoise and a rabbit. The rabbit was fast, tortoise was slow. Rabbit used to mock the tortoise. Once the rabbit challenged the tortoise for a race. Tortoise accepted rabbit’s request. Rabbit was overconfident. Rabbit thought to win the race. Rabbit ran fast. Then rabbit got tired. Rabbit wanted to take rest. So rabbit slept under the tree. Tortoise kept going and won the race. How to replace every second occurrence of

sudo: java: command not found after exiting from root user

六月ゝ 毕业季﹏ 提交于 2020-04-14 07:50:50
问题 I have installed jdk but when I tried to use the java command it said "command not found". So I then set the environment variable for my account in .bashrc and it works okay. export JAVA_HOME=/usr/java/jdk1.7.0_03 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/l\ ib/tools.jar export PATH=$PATH:$JAVA_HOME/bin However when I try to use sudo to execute java it says that again. So I used sudo -s to switch to root, put those into /etc/profile, and executed source

Unix / Linux 线程的实质

痴心易碎 提交于 2020-04-12 09:12:45
线 程 线程与进程的比较 概述 : 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动 , 进程是系统进行资源分配和调度的一个独立单位 . 线程是进程的一个实体 , 是 CPU 调度和分派的基本单位 , 它是比进程更小的能独立运行的基本单位 . 线程自己基本上不拥有系统资源 , 只拥有一点在运行中必不可少的资源 ( 如程序计数器 , 一组寄存器和栈 ), 但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源 . 一个线程可以创建和撤销另一个线程 ; 同一个进程中的多个线程之间可以并发执行 . 相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列。 在串行程序基础上引入线程和进程是为了提高程序的并发度,从而提高程序运行效率和响应时间。 区别 : 进 程和线程的主要差别在于它们是不同的操作系统资源管理方式。进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个 进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序 健壮,但在进程切换时,耗费资源较大,效率要差一些。 但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。 1) 简而言之 ,

Implementing a full Python Unix-style daemon process

走远了吗. 提交于 2020-04-10 04:53:09
问题 I'm currently trying to manually create a simple daemon process, I don't want to use the existing externals libraries to avoid overhead. I'm currently checking when my process runs that it doesn't have a PID file already created (meaning it's running), like described in this post. I also have a daemonizing module to detach the PID from current process and redirect stdout and stderr (so my daemon will keep running even if I end my session): import os import sys def daemonize(stdin="/dev/null",

about fork and execve system call

∥☆過路亽.° 提交于 2020-04-07 16:12:29
问题 It is said that fork system call creates a clone of the calling process, and then (usually) the child process issues execve system call to change its image and running a new process. Why this two-step? BTW, what does execve stand for? 回答1: Each step is relatively simple. In Unix, your process has two parts -- a read-only memory area with the application code ("text") and the read-write memory area ("data"). A fork clones the read-write area, leaving the text page alone. You now have two

about fork and execve system call

泪湿孤枕 提交于 2020-04-07 16:11:08
问题 It is said that fork system call creates a clone of the calling process, and then (usually) the child process issues execve system call to change its image and running a new process. Why this two-step? BTW, what does execve stand for? 回答1: Each step is relatively simple. In Unix, your process has two parts -- a read-only memory area with the application code ("text") and the read-write memory area ("data"). A fork clones the read-write area, leaving the text page alone. You now have two

File permission meanings

孤人 提交于 2020-04-07 10:44:32
问题 What do these file permissions mean? I am unable to understand them i tried looking at the 0-7 meanings but im unsure when they are together. -r-------x ----rw---- -rwx--x--x 回答1: Permissions as numbers are 3 octal numbers. 555, for example, when converted to 3 binary numbers is 101 101 101 which would correspond to r-x r-x r-x. The first set is owner, second set is group, third set is everyone else. r = read w = write x = execute If any of those are missing (-), then that set does not have

Remove a specific line from a file WITHOUT using sed or awk

孤街浪徒 提交于 2020-04-06 04:10:10
问题 I need to remove a specific line number from a file using a bash script. I get the line number from the grep command with the -n option. I cannot use sed for a variety of reasons, least of which is that it is not installed on all the systems this script needs to run on and installing it is not an option. awk is out of the question because in testing, on different machines with different UNIX/Linux OS's (RHEL, SunOS, Solaris, Ubuntu, etc.), it gives (sometimes wildly) different results on each

Remove a specific line from a file WITHOUT using sed or awk

痴心易碎 提交于 2020-04-06 04:09:21
问题 I need to remove a specific line number from a file using a bash script. I get the line number from the grep command with the -n option. I cannot use sed for a variety of reasons, least of which is that it is not installed on all the systems this script needs to run on and installing it is not an option. awk is out of the question because in testing, on different machines with different UNIX/Linux OS's (RHEL, SunOS, Solaris, Ubuntu, etc.), it gives (sometimes wildly) different results on each

段错误调试神器 - Core Dump详解

喜欢而已 提交于 2020-04-04 00:57:37
一、前言 : 有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的. 但这不像编译错误一样会提示到文件某一行, 而是没有任何信息, 使得我们的调试变得困难起来. gdb: 有一种办法是, 我们用gdb的step, 一步一步寻找. 这放在短小的代码中是可行的, 但要让你step一个上万行的代码, 我想你会从此厌恶程序员这个名字, 而把他叫做调试员. 我们还有更好的办法, 这就是core file. ulimit: 如果想让系统在信号中断造成的错误时产生core文件, 我们需要在shell中按如下设置: #设置core大小为无限 ulimit -c unlimited #设置文件大小为无限 ulimit unlimited 这些需要有root权限, 在ubuntu下每次重新打开中断都需要重新输入上面的第一条命令, 来设置core大小为无限. 用gdb查看core文件: 下面我们可以在发生运行时信号引起的错误时发生core dump了. 发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行. gdb [exec file] [core file] 如: gdb ./test test.core 在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里,