Bash is the Bourne Again SHell, the successor to the classic Unix sh (shell).

learn more… | top users | synonyms (1)

6
votes
4answers
100 views

Bash one-liner to atomically create a file if it doesn't exist

I want a Bash one-liner that atomically creates a file if it doesn't exist. That means essentially "if file doesn't exist, create it" but ensuring that no one else manages to create the file in the ...
32
votes
4answers
26k views

Check if array is empty in Bash

I have an array which gets filled with different error messages as my script runs. I need a way to check if it is empty of not at the end of the script and take a specific action if it is. I have ...
5
votes
1answer
4k views

How to change default shell in FreeBSD?

The default shell in FreeBSD is sh and I really can't stand it: autocomplete is very limited, and rehash commands kill me. I feel like a one armed and one legged man... How can I change the shell ...
2
votes
1answer
1k views

JBoss EAP 6.2 on RHEL 6: ./bin/init.d/jboss-as-standalone.sh hangs while calling via SSH

I'm using jboss-as-standalone.sh to manage JBoss EAP standalone as a service. I can start/stop the service with "service jboss-as-standalone.sh start/stop" while I'm on a terminal. But I would like ...
1
vote
3answers
22 views

Calculating the percentage of the total available memory on linux as an integer in bash

Here is a bash script that is calculating 80% of the total RAM available as an integer on a Linux box: server_ram_mb=$(awk '/MemTotal/ {printf( "%.2f\n", $2 / 1024)}' /proc/meminfo) echo ...
1
vote
1answer
33 views

ZFS Send Script works when run manually but not in Cron (Already tried common solutions)

Script on sending end is as follows: #!/bin/sh #Variables bigdata=vid-zfs-live/bigdata today=`gdate +%m-%d-%y` yesterday=`gdate -d'yesterday' +%m-%d-%y` #Actions zfs snapshot -r ...
5
votes
4answers
8k views

Kill all processes that are running for more than 5 minutes by a given user in linux bash script

I need the command to kill processes that have been running since at least 5 minutes for a given process. I will have to run that command every five minutes or so. Thanks a million ! (system is ...
0
votes
1answer
30 views

Log all bash actions with multiple scripts

Using the command below, I can log all actions that happened inside that script. bash -x script.sh Sadly, the script calls another script, which does not have the -x setting active then, i.e. the ...
-1
votes
2answers
27 views

Script to delete older files and copy what is left

We have a server that runs an application that is used internally. The application has a utility that creates its own backups every 24 hours. Backups are stored in: ...
2
votes
2answers
1k views

Shell script for docker ps -a | grep to find number of certain containers running

I want to write a script that is executed by my development build server that will remove any 'similar' docker containers before building and running a new container. Below is pseudo code for the ...
-2
votes
0answers
24 views

how to set date from the main centos server to all remote server using bash scripts

h! Actually i want to set date from the main centos server to all remote servers simultaneously and no any date on remote server might be different with the help of bash script. In short getting ...
2
votes
1answer
46 views

How can I set the root password in a docker container from a script?

I have a script that runs on the host and creates/starts/stops a docker container. I'd like the script to change the password of the root user within the container. Since the container is an ssh ...
0
votes
0answers
4 views

How to prevent malicious injection in argument to php “exec” (webhook -> bash script) [migrated]

I've written a webhook to integrate into chat (slack / mattermost api). The simplest way to get it up and running was a quick php script, which looks like this: <?php $token = $_POST["token"]; ...
-2
votes
1answer
36 views

SAVE IP ROUTE RULES AFTER REBOOT

I have two internet connections from two different ISPs and I need to balance & failover the traffic originating to and from my network between the two ISPs. My settings: FIRST ISP (eth0): ...
1
vote
2answers
28 views

How to run ./configure from a different folder?

I'm writing an ansible playbook which is supposed to compile from source some tool and install it. While checking my playbook, I've tried running the relevant commands manually to check their output. ...
2
votes
1answer
34 views

unix utility to limit the number of bytes placed into a bash variable

Is there a unix/linux utility, perhaps a shell builtin or an external command, that will limit the number of bytes placed into a bash variable? For example, I want to make sure STRING gets at most ...
0
votes
2answers
287 views

Rewrite sh script for freebsd

I have an sh script: #!/bin/bash fullpath="$1" filename="${fullpath##*/}" dir="${fullpath:0:${#fullpath} - ${#filename}}" base="${filename%.[^.]*}" ext="${filename:${#base} + 1}" if [ -f ...
2
votes
6answers
8k views

Don't need the whole line, just the match from regular expression

I simply need to get the match from a regular expression: $ cat myfile.txt | SOMETHING_HERE "/(\w).+/" The output has to be only what was matched, inside the parenthesis. Don't think I can use ...
23
votes
4answers
13k views

How can I check in bash if a shell is running in interactive mode?

How can I tell (in ~/.bashrc) if I'm running in interactive mode, or, say, executing a command over ssh. I want to avoid printing of ANSI escape sequences in .bashrc if it's the latter.
3
votes
2answers
70 views

Allowing PHP to run specific bash script with root permissions

I have a php script calling a bash script like this: <?php $result = exec('sudo /bin/bash /var/www/my_bash_script.sh /var/www/vhosts/testsite/htdocs/'); var_dump($result); ?> This is ...
35
votes
7answers
27k views

Log all commands run by admins on production servers

It is company policy for admins to login to the servers via a personal username, and then run sudo -i to become root. Upon running sudo -i, sudo will create an environmental variable called ...
1
vote
0answers
14 views

Why my profile looks for /usr/local/etc/profile.global (NIS)

My Ubuntu ID is manged by NIS/yellowpage. For some reason, when I log in, I'm getting this message. cat@local:~$ ssh catserver cat@catserver's password: Welcome to Ubuntu 15.10 (GNU/Linux ...
1
vote
2answers
47 views

pacemaker corosynce lsb resource script( Sybase Database ASE Server and Backup Server)

I'm trying to create a lsb resource with pcs on rhel7.2 The "sybase" script is about Sybase Database ASE Server and Backup Server Startup & Stop & Restart $ status script Before ...
18
votes
2answers
2k views

Will Ansible prevent the execution of 'rm -rf /' in a shell script

This is based upon this hoax question here. The problem described is having a bash script which contains something to the effect of: rm -rf {pattern1}/{pattern2} ...which if both patterns include ...
8
votes
4answers
6k views

Possible to disable “You have new mail in /var/mail/root”?

I would like to disable the mailcheck in FreeBSD 9.1. My login shell is BASH, so I have tried to add unset MAILCHECK in /root/.bashrc and /root/.profile, but I still get those messages. Question ...
2
votes
2answers
23 views

how to match a whitespace in a bash variable

According to the man page, the string to the right of =~ is treated as an extended regular expression. I've tried the following things to match an "ok" surrounded by whitespace on either side, but ...
17
votes
5answers
14k views

Newline-separated xargs

Is it possible to make xargs use only newline as separator? (in bash on Linux and OS X if that matters) I know -0 can be used, but it's PITA as not every command supports NUL-delimited output.
1
vote
1answer
2k views

Nginx how to run shell script on page load via lua module os execute and then serve directory listing?

So I have a bash script that I want to execute/run on each "page load" of url so that it will modify (creating symlinks) to a /public/html/ folder that I want the server to then serve. The problem ...
69
votes
6answers
68k views

How to run a command multiple times?

Is there a way to run a command (e.g. ps aux|grep someprocess) for n times? Something like: run -n 10 'ps aux|grep someprocess' I want to use it interactively, please do not post scripts. ...
59
votes
4answers
15k views

Is there a difference between how two ampersands and a semi-colon operate in bash?

If I wanted to run two separate commands on one line, I could do this: cd /home; ls -al or this: cd /home && ls -al And I get the same results. However, what is going on in the ...
5
votes
2answers
120 views

Cat, Grep, Redirect Output… Blank File?

I just ran cat /opt/webapplications/Word/readme.log | grep -v 'Apple' and I got the output on the cli that I was expecting, which was all the lines in readme.log that did not contain 'Apple'... ...
1
vote
2answers
1k views

Inserting multiple lines into a sqlite column with bash script

I am trying to insert a value into my sqlite DB that consists of multiple lines and special characters. For example: { "foo"="$BAR" } I have been using the sqlite3 command but I dont know how to ...
0
votes
1answer
1k views

Monit failed to start shell script

So I am having a problem with monit starting a process this is my code: check process program_1 matching "program_1" start program = "/home/user1/files/start.sh" stop program = ...
1
vote
1answer
274 views

lftp mirror leaves empty folders on remote

My script is set up with this for lftp lftp -p 2218 -u $login,$pass sftp://$host << EOF set mirror:use-pget-n 5 mirror -c -P5 --depth-first --Remove-source-files ...
0
votes
1answer
1k views

OpenVPN run script after tunnel is set up

I'm having a hard time figuring out exactly how can I update my A records on my cloudflare account. I have already set up a script that checks for IP changes, and pushes the changes through the ...
2
votes
2answers
181 views

Allowing httpd to run a bash script in /usr/bin/

Migrating a system from CentOS6 to RHEL7 with SELinux running Enforced. A php script makes a call to /usr/bin/processdata.sh to generate some data behind the scenes. This worked fine with the old ...
3
votes
4answers
1k views

Extract repository name from GitHub url in bash

Given ANY GitHub repository url string like: git://github.com/some-user/my-repo.git or git@github.com:some-user/my-repo.git or https://github.com/some-user/my-repo.git What is the best way in ...
0
votes
0answers
4 views

How can a command in a pipe read from a keyboard? [migrated]

I randomly tried to execute the following command in bash1 cat /some/file | ssh user@machine 'cat >/destination/file There are no keys installed on the remote machine, so the only way to ...
6
votes
4answers
14k views

How do I rename multiple files by removing characters in bash?

I have to rename multiple files in directory by removing first 5 characters for each filename. How can I do this i bash/shell? I'm using Ubuntu 11.10. Thanks.
0
votes
6answers
194 views

Sed regexp /d deleting whole file

Given a typical known_hosts file: [123.45.67.89]:22 ssh-rsa bbuivvhbkbjdnflksndkfnksnfk... [98.76.54.32]:14444 ssh-rsa hohdibsodbfoasbfbsabdfbsd... [77.34.122.33]:32209 ssh-rsa ...
23
votes
11answers
11k views

Linux: How to use a file as input and output at the same time?

I've just run the following in bash: uniq .bash_history > .bash_history and my history file ended up completely empty. I guess I need a way to read the whole file before writing to it. How is ...
15
votes
5answers
50k views

Determine if filesystem or partition is mounted RO or RW via Bash Script?

Is there an easy way to determine if a mounted filesystem is mounted as Read-Only or Read-Write? I was thinking just to pipe mount but I thought there might be an easier way.
7
votes
5answers
4k views

Send message to IRC channel from bash

I have installed ircd-hybrid on my Ubuntu Server (192.168.1.2, example.com). We use #teamchannel to communicate inside the team. The question is: how can I send some short message from example.com to ...
5
votes
3answers
4k views

Linux file copy with ETA?

I'm copying a large amount of files between disks. There's approximately 16 GB of data. I'd like to see progress information, and even an estimated time of completion from the command line. Any ...
1
vote
1answer
34 views

Ansible & rbash

I want to manage a bunch of devices running /bin/rbash as the default shell with Ansible. The problem is that Ansible always try to execute a list of commands that rbash doesn't like at all: umask ...
0
votes
0answers
23 views

Cacti poller runs but not full script

I'm using Cacti and the poller calls a bash script to get some data. At the end of this script I have a logical test and then an email is sent if true. The issue I have is when the poller is running ...
0
votes
0answers
18 views

“-bash: fork: retry: …” on SSH connection

When I'm connecting via ssh to my useraccount of my server, I'm getting the following error: -bash: fork: retry: No child processes -bash: fork: Resource temporarily unavailable Does anyone ...
0
votes
3answers
45 views

Bash if condition to check if a ubuntu package needs an upgrade?

I need a bash line to check if a ubuntu package needs an upgrade or not. For example, I want to check if the package 'firefox' needs an upgrade using dpkg or apt-get commands. Hypthetical Example: ...
8
votes
5answers
9k views

chsh: PAM authentication failed

i want to change default shell env and run chsh -s /bin/zsh as root user, but a error message appeared: chsh: PAM authentication failed. i find this thread ...
0
votes
1answer
1k views

Using curl with cookies

I hope someone can help me out on this one. I have been trying this for a while now, with all sorts of variations and cannot seem to get this to work. I am trying to script a way to log in to ...