bash

Execute an array of string describing shell command

旧城冷巷雨未停 提交于 2021-02-18 11:04:00
问题 I'm struggling to execute a set of command lines stored as strings in an array. My code looks like this : arr=( "sudo apt-get update" "sudo apt-get install xxx" ) ... arr=( ${arr[@]} "ln -s /path1 /path2" ) etc... # Then I loop on the array of command for (( i = 0 ; i < ${#arr[@]} ; i++ )) do eval ${arr[$i]} done When it loops over the array, the array is larger than the number of command stored into it. As if the blank spaces in my strings split the array in more elements A typical ouput is

Get Subnet mask in Linux using bash

风格不统一 提交于 2021-02-18 09:28:09
问题 I am using bash to get the IP address of my machine with that script: _MyGW="$( ip route get 8.8.8.8 | awk 'N=3 {print $N}' )" And now I am trying to get the Subnet Mask in this type: 192.168.1.0/24 But I have no idea how can I do that. 回答1: there are couple of ways to achieve this: first: to print the mask in format 255.255.255.0, you can use this: /sbin/ifconfig wlan0 | awk '/Mask:/{ print $4;} ' second: we can use ip command to get the mask in format 192.168.1.1/24 ip -o -f inet addr show

.profile not working from terminal in mac

荒凉一梦 提交于 2021-02-18 08:54:18
问题 I had a .profile file that I was reading and using aliases from in my terminal, but at some point the aliases stopped working for no clear reason (other commands were still working). Thinking to make a quick fix, I deleted (rm) and recreated my .profile file in my user directory. There are no bash_profiles or others in existence that I can see, at leasts in that directory. The new .profile did not work. I tried restarting the terminal and the computer. Now, the commands that worked in the

Linux下针对服务器网卡流量和磁盘的监控脚本

送分小仙女□ 提交于 2021-02-18 08:52:20
1)实时监控网卡流量的通用脚本: [root@ceph-node1 ~]# cat /root/net_monit.sh #!/bin/bash PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH function traffic_monitor { OS_NAME=$(sed -n '1p' /etc/issue) eth=$1 if [ ! -d /sys/class/net/$eth ];then echo -e "Network-Interface Not Found" echo -e "You system have network-interface:\n`ls /sys/class/net`" exit 5 fi while [ "1" ] do STATUS="fine" RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}') TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}') sleep 1 RXnext=$(cat /proc/net/dev | grep $eth | tr : " " |

MacPorts and the bash PATH

泪湿孤枕 提交于 2021-02-18 08:35:43
问题 I am using 10.8.1 (Mountain Lion). After upgrading to Mountain Lion, some of my MacPorts stopped working. For an easier life, I simply cleared out /opt/local/ and reinstalled the latest version of MacPorts, followed by the ports themselves. This has had the side-effect that many of the ports I was using have gone back to their bundled OS X defaults. I opened up .profile to make sure that /opt/local/bin came first in the PATH, but that hasn't solved the problem. I suspect the output of port

executing a script which runs even if i log off

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-18 08:23:08
问题 So, I have a long running script (of order few days) say execute.sh which I am planning to execute on a server on which I have a user account... Now, I want to execute this script so that it runs forever even if I logoff or disconnect from the server?? How do i do that? THanks 回答1: You have a couple of choices. The most basic would be to use nohup : nohup ./execute.sh nohup executes the command as a child process and detaches from terminal and continues running if it receives SIGHUP . This

Difference in pipe and file redirection - BASH

独自空忆成欢 提交于 2021-02-18 08:10:29
问题 Redirection is used to redirect stdout/stdin/stderr! Ex: ls > log.txt. Pipes are used to give the output of a command as input to another command. Ex: ls | grep file.txt Why exactly are these two operators doing the same thing? Why not just write ls > grep to pass the output through, isn't this just a kind of redirection also? I realize Linux is "Do one thing and do it well", so there has to be more of a logical reason that I'm missing. 回答1: You do need a differentiating syntax feature - and

Put only the stdout of the last shell command in a Python variable [duplicate]

两盒软妹~` 提交于 2021-02-18 07:50:07
问题 This question already has answers here : How to get the last N lines of a subprocess' stderr stream output? (2 answers) Closed 16 days ago . prova.sh contains: #!/bin/bash echo "Output that I don't want." echo "Output that I don't want." echo "Output that I don't want." echo -e "Output that I want.\nI want this too.\ \nI want this too." #This is the last command of the bash script, which is what I'm looking for. This solution: import subprocess output = subprocess.check_output('./prova.sh',

bash - pipe creates a subshell

三世轮回 提交于 2021-02-18 06:44:07
问题 So this read is executed after the pipeline, which means that the output of the echo gets read into str - but because it is after a pipe, the contents of str are now in a subshell that cannot be read by the parent shell. My questions is - what happens in to the contents of str ? Does the pipe create a subshell, and then once the content are read into str , does the parent process kill the child process and str is erased - or does the contents of str live on somewhere outside the shell. Like

bash: -exec in find command and &

。_饼干妹妹 提交于 2021-02-18 05:32:11
问题 I want to run: ./my_script.py a_file & ... on all files in the current folder that end with .my_format , so I do: find . -type f -name "*.my_format" -exec ./my_script {} & \; but it doesn't work. How should I include & in the -exec parameter? 回答1: Try this: $ find . -type f -name "*.my_format" -exec sh -c './my_script {} &' \; The mostly likely reason your attempt didn't work is because find executes the command using one of the exec(3) family of standard c library calls which don't