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

learn more… | top users | synonyms (1)

6
votes
2answers
7k views

bash script: repeat command if it returns an error

I would like to create a loop that repeats a ncftp transfer if it returns an error. I'm a little unsure how the exit code variable can be used in a loop. Would something like this work? until [$? ...
6
votes
4answers
2k views

With a username passed to a script, find the user's home directory

I am writing a script that gets called when a user logs in and check if a certain folder exists or is a broken symlink. (This is on a Mac OS X system, but the question is purely bash). It is not ...
6
votes
5answers
3k views

How to include all files from a directory in shell script (/etc/init.d/iptables in this case)

I have an /etc/init.d/iptables start|stop|restart script on different ubuntu servers (which is a normal shell script) For each new service I have to edit and insert a line to open a port. This leads ...
6
votes
3answers
866 views

Can rm command complete in a bash script before all files have been removed?

I wrote a simple bash script to backup certain files daily to a backup mount and keep the last 3 days of backups. It's obviously too simple, as I'm occasionally getting odd behaviour that could be ...
6
votes
2answers
8k views

Writing to stdin of background process

I'm on an Ubuntu 10.04 box, and started a server in the background (myserver &) over ssh. It's been running fine, but I need a way to get at the server's stdin, as the only way to control the ...
6
votes
2answers
2k views

My cron tasks report command not found [duplicate]

This is the contents of my crontab file: 0 0,6,12,18 * * * cd /var/www/app/current && backup perform --trigger db_backup --config_file config/backup.rb --data-path db --log-path log ...
6
votes
4answers
6k views

Cron: Only get errors in emails?

I finally set up a realistic backup schedule on my data through a shell script, which are handled by cron on tight intervals. Unfortunately, I keep getting empty emails each time the CRON has been ...
6
votes
2answers
8k views

How to repeatedly call rsync until files are sucessfully transferred

I'm trying to syncronize files from a remote server that is not reliable, meaning the connection tends to fail "randomly" with rsync: connection unexpectedly closed Rsync is called with ...
6
votes
2answers
686 views

How to find out if an OpenSSL certificate was created by a certain private key?

I have an OpenSSl certificate. I also need the private key. I was told it is located somewhere on the server, and true enough, I found multiple SSL key files. Before I try them all by brute force: Is ...
6
votes
3answers
1k views

User name before sudo

I got a script requiring sudo, but the script must set parameters according to the original user, such as: chown "${USER}:${USER}" dir If I set it under sudo, I just end up with chmod root:root, ...
6
votes
2answers
3k views

How can I retrieve the absolute filename in a shell script on Mac OS X?

I'd like to retrieve the absolute file name of the script file that's currently executed. Links should be resolved, too. On Linux, this seems to be done like this: $(readlink -mn "$0") but ...
6
votes
4answers
7k views

Is it possible to run a command on windows server from Linux?

I want to restore a sqlserver db backup on remote windows server from linux. I tired "net rpc" but there is nothing for execute a command or script on remote server.
6
votes
2answers
3k views

rsync: How to exclude Dotfiles only in topmost directory?

When performing backups under Bash with rsync, I'm trying to exclude all dotfiles and hidden directories in the top directory, but not those in otherwise targeted directories. For example: /copyme.c ...
6
votes
3answers
2k 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 ...
6
votes
2answers
2k views

Where do you share or find useful powershell, bash, batch, vbs, scripts?

I just wrote a quick little powershell script to make my life easier for a task that I was facing. I thought it would be nice to put it on the Internet, where someone else may also find it useful. The ...
6
votes
5answers
12k views

Check to see if a directory exists remotely (shell script)

Is there a way to see if a directory exists on a remote server? Perhaps there's a better way, but I'm writing an application deployment script, and I want to create a directory on a remote server if ...
6
votes
6answers
4k views

how to write a script that only acts on new log entries

I feel like this should be a simple thing but I am having a hard time figuring it out. I am trying to write a script that will monitor one of the apache log files and take some specific action. But ...
6
votes
2answers
410 views

Bash script parameters

I need to write a bash script, and would like it to parse unordered parameters of the format: scriptname --param1 <string> --param2 <string> --param3 <date> Is there a simple way ...
6
votes
1answer
3k views

Add server to known_hosts

I would like to add github.com to the known_hosts file from the command line as I am creating a puppet manifest to provision a remote server. I have tried: "ssh-keyscan -H github.com > ...
6
votes
3answers
5k views

Rsync seems incompatible with .bashrc (causes “is your shell clean?”)

It turns out rsync can't work with a remote server which has a .bashrc file? At local client i got when run rsync: protocol version mismatch -- is your shell clean? (see the rsync man page for an ...
6
votes
5answers
4k views

Different color prompts for different machines when using terminal/ssh?

I have 5 machines I constantly ssh into to do work. Its getting increasingly frustrating when I am issuing wrong commands on wrong boxes. Luckily I havent done anything bad yet. I wanted to know if ...
6
votes
3answers
5k views

Find out want partition a directory is located on in Linux?

Is there a way to find out what partition a directory in located in? I know I can use df to list partitions and mount points but I need to be able to find out what partition any directory is located ...
6
votes
3answers
11k views

kill a screen (but not all screens)

I have multiple screens running on an Ubuntu server that are initiated as: screen -dmS screen1 cmd screen -dmS screen2 cmd etc... And I need to kill one screen, but not all of them. What is the ...
6
votes
1answer
2k views

Bash custom prompt breaking wrapping of long command lines

I have a custom bash prompt that colours my username, hostname and current working directory. The colours display correctly, however when I type a command line that needs to wrap, the terminal does ...
6
votes
3answers
2k views

UTF-8 and !# shell scripts

Is there a way to configure bash on Linux (red hat and ubuntu) to allow shell scripts to be encoded in UTF-8? I can't find a simple way to change just one little thing and have the whole system just ...
6
votes
1answer
324 views

Providing normal users(non-root) with initialization and shutdown auto-run capabilities

I'm hosting an experimental/testing Linux box, running Debian Wheezy 7.4.0 distribution. Different users log into the machine over ssh to their accounts and are allowed to run the development tools ...
6
votes
6answers
13k views

(svn+ssh) getting bash to load my PATH over SSH

This problem comes up with me trying to make svnserve (Subversion server) available on a server through SSH. I compiled SVN and installed it in $HOME/bin. Local access to it (not through SSH) works ...
6
votes
3answers
5k views

How do I exclude a folder when performing file operations i.e. cp, mv, rm and chown etc. in Linux

How do you exclude a folder when performing file operations i.e. cp etc. I would currently use the wild card * to apply file operation to all, but I need to exclude one single folder. The command ...
6
votes
1answer
286 views

How to start netcat in listening mode from another host?

I have a script where I would like to start nc in listening mode on 10.10.10.11, but I would like to do it from 10.10.10.10, where I have this script ssh -n 10.10.10.11 nc -l 8023 | mbuffer -s 128k ...
6
votes
3answers
6k views

iftop - how to generate text file with its output?

iftop is great tool to view almost live bandwidth usage distinguished by source-ip source-port destination-ip destination port. I'm using it to see which client's ip is using most bandwidth. Now I ...
6
votes
1answer
5k views

Launching a PHP daemon from an LSB init script w/ start-stop-daemon

I'm writing an lsb init script (admittedly something I've never done from scratch) that launches a php script that daemonizes itself. The php script starts off like so: #!/usr/bin/env php <?php ...
6
votes
4answers
2k views

How can one set a terminal's title with the 'tput' command?

I discovered through this answer that I've been setting terminal colours in my prompt in a naive way for years. I've now modified my .bashrc to use 'tput' commands to colourise various elements of my ...
6
votes
7answers
11k views

How do I read multiple lines from STDIN into a variable?

I've been googling this question to no avail. I'm automating a build process here at work, and all I'm trying to do is get version numbers and a tiny description of the build which may be multi-line. ...
6
votes
1answer
1k views

Debian SSH - Resizing terminal does not register with bash

We recently reinstalled our server due to a disk failure, and now we're having an issue with resizing terminals. We installed Debian 6.0.6. Symptoms When you resize a terminal, no ncurses-based apps ...
6
votes
2answers
3k views

rotate nohup out file (nohup.log)

is there a 'native' way where i can rotate nohup out file ?
5
votes
5answers
18k views

Read the first line of a file using bash

Does anyone know of a simple one liner to read the first line of a file in bash?
5
votes
6answers
11k views

What is a recommended way to patch the Shellshock Bash bug on an unsupported Ubuntu server?

I maintain an out-of-support Ubuntu 12.10 (Quantal Quetzal) server (don't ask me why, please), and we need to patch the Shellshock Bash security bug. As upgrades are not available anymore, what ...
5
votes
3answers
1k views

Why does 'pkill /?' kill my SSH session?

After running the following commands (bash) as root via SSH: pkill --help pkill -h pkill /? The first two commands didn't provide me any information, that's why ran the third (kinda ...
5
votes
9answers
8k views

Bash snippet to move all files in a directory into that directory

I have a directory with lots of files and directories in it. In order to tidy it up, I'm going to put all those files into a directory, so I was going to do something like this: $ ls | wc -l 123 $ ...
5
votes
9answers
1k views

Best practices to prevent 'rm -rf /' in bash scripts

It's well known fact that extra space in env.variable can lead to deletion of / directory in bash script. #!/bin/bash ... rm -rf /$MYPATH if $MYPATH contains values like " dir" or "dir /" it will ...
5
votes
8answers
3k views

Bash can't start a programme that's there and has all the right permissions

This is a gentoo server. There's a programme prog that can't execute. (Yes the execute permission is set) About the file $ ls prog $ ./prog bash: ./prog: No such file or directory $ file prog prog: ...
5
votes
5answers
5k views

How do I split a string against a character in BASH?

I would like to be able to split an authorization code by the - character so that I can operate on each segment individually. I do not want to use external binaries (awk, grep) - this is to be as ...
5
votes
3answers
8k views

bash: variable loses value at end of while read loop

I have a problem in one of my shell scripts. Asked a few colleagues, but they all just shake their heads (after some scratching), so I've come here for an answer. According to my understanding the ...
5
votes
5answers
2k 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
2k views

Bash command for regular expression substitution

What bash command can be used for regex substitution on a pipe? cat foo.txt | someprogram
5
votes
3answers
371 views

Bash variable assignment using another variable as part

I have the following excerpt from a bash script used to backup a database #!/bin/bash DB=database_name DBUSER=username FILENAME=$DB_$(date +%s).sql I am trying to reuse the value of DB within the ...
5
votes
5answers
3k views

Bash snippet for killing a process until it's dead?

I'm trying to write a robust bash script, and in it I create a background process. At the end of the script, I want to kill it. I have it's PID. I was thinking of somthing like this while [[ ps ef ...
5
votes
6answers
2k views

how do I make solaris shell more familiar for a linux user? [closed]

I've been a linux user for years. Very used to the bash shell, used to linux shell key mappings that come with most mainstream distros. I'm also a happy vim user in linux & love my arrow keys. ...
5
votes
3answers
3k 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 ...
5
votes
1answer
538 views

how to pre-create directories on linux for file storage

looking for a way to pre-create directories on linux to be used to store a large number of files. We will be generating file ids using a GUID - I need to keep a copy of these files on a linux web ...