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

learn more… | top users | synonyms (1)

7
votes
9answers
327 views

How would I grab a text file off of a windows machine from a linux cli?

I am writing a bash script to analyze the mail logs on our network, which is mostly postfix boxes, but would like to include MS Exchange logs in the analysis if possible. Is there any way to copy a ...
7
votes
3answers
17k 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 ...
7
votes
6answers
8k views

crontab environment

I have written various scripts to launch Java server applications, which are typically run for 24 hours before being shut down (by invoking the same script with a different parameter). The script ...
7
votes
4answers
1k views

Bash: execute command on results of FIND

I am trying to resize all files that are found using a FIND command (they are all files within a directory and its subdirectories). I tried many options but stumble across different errors each time. ...
7
votes
4answers
3k 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 ...
7
votes
2answers
1k views

bash sub-string extraction for specified start and end character

For example I have a log file having this entry: [Wed Aug 08 11:39:41 2012] [error] [client 155.94.70.224] ModSecurity: [file ...
7
votes
6answers
24k views

How should someone create an encrypted password for /etc/shadow?

I am setting up a new account on a Linux box for Subversion repository access, and can send the password to the new user. However, I think there was a command line utility for this new user to encrypt ...
7
votes
3answers
1k 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 ...
7
votes
3answers
3k views

echo based on grep result

I need a one liner which displays 'yes' or 'no' whether grep finds any results. I have played with grep -c, but without success.
7
votes
2answers
2k views

Bash: Run an executable giving a good exit status

I want to run an executable in Linux, and regardless of the exit status that it returns, I want to return a good exit status. (i.e. no error.) (This is because I'm using sh -ex and I want the script ...
7
votes
3answers
343 views

How to execute a command for each file in a deep directory structure, recursively?

I have a deep directory structure, with a large number of files (about 1M). I'd like to execute a command against each. (In my case the files are pngs which I'd like to run optipng against.) I've ...
7
votes
3answers
4k 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 ...
7
votes
4answers
3k views

Execute program each time somebody logs in via SSH into a Linux box

I have a small SSH server, and I'd like to write a script to run each time somebody logs in via SSH. Now, how would I go about that? I'd like the script to run everytime ANYBODY logs in, and I need ...
7
votes
3answers
7k 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 ...
7
votes
5answers
2k views

Pass a pipe to a command that expects a filename

Say I have a command foo which takes a filename argument: foo myfile.txt. Annoyingly, foo doesn't read from standard input. Instead of an actual file, I'd like to pass it the result of another command ...
7
votes
5answers
6k 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 ...
7
votes
1answer
1k views

Verify disown command

I issued the ^z; bg; disown sequence in order to allow me to close an ssh session in which I am running a super-important long-running process. This process writes status output to stderr, and it has ...
7
votes
1answer
3k views

How can I get remaining args after pulling out parsed items using getopts?

I want to parse some arguments to a bash script using getopts but want to be able to access the remaining args that are not included in the option list. So for example, if I have a call: % script -a ...
7
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 ...
7
votes
4answers
6k 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 ...
7
votes
4answers
10k views

What's a proper way of checking if a PID is running?

I have a .pid file, and I need to check if the process is running. So far I found two options kill -0 `cat something.pid` which prints out an error if the pid isn't running. I know this can be ...
7
votes
1answer
1k views

Can i call pushd/popd and prevent it printing the stack?

After calling pushd/popd in bash, it will print off the current directory stack. Is there any way to prevent this behaviour, so that it will act 'quitely'? This sort of noise in a command is uncommon ...
7
votes
2answers
5k views

linux: kill all my processes but not this terminal

I want an easy way in linux to kill all my current processes. The problem with kill -9 -1 is that it also kills the current terminal. Is there an easy way to kill everything except the current ...
7
votes
3answers
9k views

Stdout/stderr redirects in start-stop-daemon

I'm trying to write init config that will redirect output of my daemon to two files (for stdout and stderr). The problem is, it's not working. I'm reading this right now. So, I've done this shell ...
7
votes
3answers
226 views

How to safeguard a shell script against running out of control?

I've recently had an experience of writing a shell script which crashed a server (and damaged a partition) by consuming all resources. It was hooked up to a cron job, and it seems it took longer to ...
7
votes
3answers
1k views

Bash: how to know if the last command's output ends with a newline or not?

Most of the time the output of a command ends with the newline character. But sometimes it does not, so the next shell prompt is printed in the same line together with the output. Example: ...
6
votes
4answers
826 views

Script to automatically install packages

I want to write a bash or perl script to install a number of packages on my debian based machine. I want it to be something like : aptitude install package1 aptitude install package2 But, I do ...
6
votes
4answers
6k views

Linux Bash Syntax: Meaning of &&, \, and -

What do the '&&', '\' and '-' mean at the end of bash commands? In particular, I came across the following combination of lines that are supposed to add public keys to Ubuntu's aptitude ...
6
votes
6answers
12k 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 ...
6
votes
4answers
279 views

How to determine how many shells deep I might be?

I'm wondering whether I've called the shell recursively, is there an easy way to find out? Is any solution specific to the shell? I'm using bash.
6
votes
5answers
6k 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 ...
6
votes
3answers
1k views

How to recover from a deleted /bin/bash?

On my debian maschine I deleted /bin/bash by accident. Is there a way to get it back without reinstalltin the machine? If it helps. I'm still logged in. Guess once I'm out I cannot log in since it's ...
6
votes
2answers
1k views

Who can delete a file?

I created a file, did chmod 000 to it and then changed the owner and group to root:root. I was still able to delete the file as the original creator of it (not root). Who has permissions to delete a ...
6
votes
3answers
3k views

Non-recursive find on linux

I am trying to find all directories that are directly inside the current directory and older than a specific age. The trick is, I only want to consider the age of the immediate descendants of the ...
6
votes
2answers
6k views

cat * | grep something… what file is the result in?

If one was to run the following command cat * | grep DATABASE the shell would spit out all the lines in * files that contained the word DATABASE in them. Is there any way to also spit out what file ...
6
votes
2answers
9k 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
3k views

SSH from A through B to C, using private key on B

I'm looking for a simple way to SSH from my local machine, A, through a proxy, B, to a destination host, C. The private key that goes with the public key on C is on B, and I can't put that key on my ...
6
votes
5answers
3k 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 ...
6
votes
3answers
11k views

Linux gzip multiple subdirectories into separate archives?

How would I be able to compress subdirectories into separate archives? Example: directory subdir1 subdir2 Should create subdir1(.tar).gz and subdir2(.tar).gz
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
2answers
832 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
4answers
12k views

BASH Scripting, su to www-data for single command

I am working on automating the creation of subversion repositories and associated websites as described in this blog post I wrote. I am running into issues right around the part where I su to the ...
6
votes
2answers
3k 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
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 ...
6
votes
2answers
4k 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
6answers
3k views

Prune duplicate entries from PATH variable

I modify my .bashrc frequently and then source it. However, when I have things like export PATH="~/bin:~/perl5/bin:$PATH" in my file, then the PATH environment variable grows every time I source the ...
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
4answers
2k views

Bash: Put a job in the background and then tail the log

How do you make the escaping work so that the & is actually running the first command in the background? # foo param1 param2 >> run.out &; tail -f run.out
6
votes
5answers
704 views

Hide the output of a shell command only on success?

Hiding the output of a shell command usually involves redirecting stderr and stdout. Is there any builtin facility or command which by default hides the output but on error dumps all the accumulated ...
6
votes
2answers
8k views

“Cannot set terminal process group” during su to another user as login shell

Note: Please read the updated information starting with "EDIT" near the halfway point of this post - the environment and background of this problem has changed I've got a bog standard Debian 6.0 ...