sh

How to get the process id of command executed in bash script?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:41:51
问题 I have a script i want to run 2 programs at the same time, One is a c program and the other is cpulimit, I want to start the C program in the background first with "&" and then get the PID of the C program and hand it to cpulimit which will also run in the background with "&". I tried doing this below and it just starts the first program and never starts cpulimit. Also i am running this as a startup script as root using systemd in arch linux. #!/bin/bash /myprogram & PID=$! cpulimit -z -p

Replacing bash arrays and C-style for loops in POSIX sh

99封情书 提交于 2019-12-10 12:37:10
问题 I want to convert following bash code in pure shell script (sh) language so that it should run by other script language mode i.e dash script. arguments=("$@") for (( i=0; i<$#; i++ )); do case "${arguments[$i]}" in -foo) let "i = i + 1" echo "${arguments[$i]}" ;; *) break esac done above code finely run in bash mode but through an error on dash mode. 2: ./orig.sh: Syntax error: "(" unexpected Now If I change line 2 to get rid the error as following line arguments="$@" But now I got another

tmux: how to open file under cursor

白昼怎懂夜的黑 提交于 2019-12-10 10:40:14
问题 i am a vim user and got used to the gf command, which opens the file under the cursor. Now i wanted to ask, if there is something like that for tmux. I can navigate through a tmux-pane and it happens often that there is a file-path under the cursor. Now i like to have the possibility to open that file under the cursor with vim. A: in the current window B: in another window which includes and opened vim Maybe there is a possibility to run a sh-script in that navigation-mode when invoking a

Why does popen() invoke a shell to execute a process?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 03:21:20
问题 I'm currently reading up on and experimenting with the different possibilities of running programs from within C code on Linux. My use cases cover all possible scenarios, from simply running and forgetting about a process, reading from or writing to the process, to reading from and writing to it. For the first three, popen() is very easy to use and works well. I understand that it uses some version of fork() and exec() internally, then invokes a shell to actually run the command. For the

How do I compare strings in Bourne Shell?

泄露秘密 提交于 2019-12-10 01:16:15
问题 I need to compare strings in shell: var1="mtu eth0" if [ "$var1" == "mtu *" ] then # do something fi But obviously the "*" doesn't work in Shell. Is there a way to do it? 回答1: bash Shortest fix: if [[ "$var1" = "mtu "* ]] Bash's [[ ]] doesn't get glob-expanded, unlike [ ] (which must, for historical reasons). bash --posix Oh, I posted too fast. Bourne shell, not Bash... if [ "${var1:0:4}" == "mtu " ] ${var1:0:4} means the first four characters of $var1 . /bin/sh Ah, sorry. Bash's POSIX

git - checkout single file under bare repository

旧城冷巷雨未停 提交于 2019-12-09 15:26:42
问题 On the server I have bare repository which is origin for development process and to simplify deployment to QA environment. So in post-receive it simply does GIT_WORK_TREE=/home/dev git checkout -f But as product gets more complicated there are some other things should be happening. So now it is handled by deploy.sh script which is also tracked by repository. So what I want to do is to be able instead of checking out whole repository is to checkout only deploy.sh and run it. I thought

How to exclude a list of full directory paths in find command on Solaris

情到浓时终转凉″ 提交于 2019-12-09 13:29:32
问题 I have a very specific need to find unowned files and directories in Solaris using a script, and need to be able to exclude full directory paths from the find because they contain potentially thousands of unowned files (and it's normal because they are files hosted on other servers). I don't even want find to search in those directories as it will hang the server (cpu spiking to 99% for a long time), therefore piping the find results in egrep to filter out those directories is not an option.

How to extract numbers from a string?

流过昼夜 提交于 2019-12-09 11:13:39
问题 I have string contains a path string="toto.titi.12.tata.2.abc.def" I want to extract only the numbers from this string. To extract the first number: tmp="${string#toto.titi.*.}" num1="${tmp%.tata*}" To extract the second number: tmp="${string#toto.titi.*.tata.*.}" num2="${tmp%.abc.def}" So to extract a parameter I have to do it in 2 steps. How to extract a number with one step? 回答1: You can use tr to delete all of the non-digit characters, like so: echo toto.titi.12.tata.2.abc.def | tr -d -c

How to redirect stderr to a file in a cron job

♀尐吖头ヾ 提交于 2019-12-09 08:49:02
问题 I've got a cron job that is set up like this in my crontab: */1 * * * * sudo /home/pi/coup/sensor.py >> /home/pi/sensorLog.txt It puts stdout into sensorLog.txt, and any stderr it generates gets put into an email. I want both stdout and stderr to go into sensorLog.txt, so I added 1>&2 to the crontab, which is supposed to make stderr go into the same place as stdout. It now looks like this: */1 * * * * sudo /home/pi/coup/sensor.py >> /home/pi/sensorLog.txt 1>&2 Now, both stdout and stderr both

How to define global shell functions in a Makefile?

∥☆過路亽.° 提交于 2019-12-09 08:02:23
问题 I want to define a shell function #!/bin/sh test () { do_some_complicated_tests $1 $2; if something; then build_thisway $1 $2; else build_otherway $1 $2; fi } in such a way that I can use it in every rule of my Makefile, such as: foo: bar test foo baz To be clear, I want the shell function to be part of the Makefile. What is the most elegant way to do this? Bonus points if you can do it without calling make recursively. Background: My actual problem is that make -n produces a very long and