A shell script is a script written for the shell, or command line interpreter, of an operating system.
9
votes
2answers
910 views
How can I test for POSIX compliance for shell scripts?
Probably the main thing that this website has taught me so far is the importance of writing portable shell scripts.
Considering that POSIX is the closest thing to a common standard between all ...
8
votes
3answers
4k views
Find the owner of a directory or file, but only return that and nothing else
I am looking for a command that will return the owner of a directory and only that--such as a regex parsing the ls -lat command or something similar? I want to use the result in another script.
8
votes
3answers
713 views
How to sort the string which combined with string + numeric using bash script?
This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected.
/home/files/profile1
/home/files/profile10
/home/files/profile11
...
8
votes
2answers
477 views
date - years prior to 1901 are treated as invalid
I have date (GNU coreutils) 7.1 installed on my system.
If I try to check dates prior to 14-Dec-1901, I get an "invalid date" error. For example,
$ date -d 1901-12-13
date: invalid date ...
8
votes
3answers
2k views
How can I receive top-like CPU statistics from the shell?
I'm trying to get an accurate read of my used CPU (in percent) from top. This is the command I'm running for testing:
top -n1 | awk '/Cpu\(s\):/ {print $2}'
This returns:
10.7%us,
Which is the ...
8
votes
2answers
235 views
Newlines in filenames
I understand and accept the premise that defensive1 shell scripting is both prudent and, in the longer term, more sustainable.
Many of the answers to text processing questions here follow this ...
8
votes
3answers
3k views
Bash script testing if a command has run correctly
I am working on a bash script that I would like to work for several types of VCS.
I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
8
votes
3answers
346 views
How can one run multiple programs in the background with single command?
How can one run multiple programs in the background with single command?
I have tried the commands below, but they do not work.
nohup ./script1.sh & && nohup ./script2.sh &
-bash: ...
8
votes
1answer
153 views
How to start tailing a file that has not been yet created
I use tail to monitor the progress of jobs that I know will write their progress to disk. Almost always, I know which file they will create before they start running (the jobs are dispatched by a ...
8
votes
6answers
4k views
Find biggest files or directories
Which command will print the sizes of all files and directories in the tmp directory (including hidden ones) and sort them by sizes from
largest to smallest in human readable format (e.g. 2 GB)?
The ...
8
votes
1answer
614 views
Suppress output unless non-zero exit code
What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking:
quiet_success()
{
file=/tmp/suppressed
if ! ( "$@" > "$file" ...
8
votes
0answers
190 views
how to get exit status from the command before the last [duplicate]
I have solaris machine ( solaris 10 )
please Look on the following simple commands (haconf -makerw | grep -iq "Cluster already writable")
# haconf -makerw
VCS WARNING V-16-1-10364 Cluster ...
7
votes
2answers
1k views
Bash: run command2 if command1 fails
I want to do something like this:
if cmd1 && cmd2
echo success
else
echo epic fail
fi
How should I do it?
7
votes
4answers
967 views
Executing user defined function in a find -exec call
I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1).
The following code doesn't yield any result:
function foo {
echo "Hello World"
}
find somedir -exec ...
7
votes
1answer
1k views
find n most frequent words in a file
I want to find, say, 10 most common word in a text file. Firstly, solution should be optimized for keystrokes (in other words - my time). Secondly, for the performance. Here is what I have so far to ...
7
votes
2answers
1k views
Parallelizing a for loop
I want to parallelize the for loops of the following code. How to do this?
#!/bin/bash
N=$1
n=$2
for (( i=1; i<=$N; i++ )); do
min=100000000000000 //set min to some garbage value
for (( ...
7
votes
2answers
332 views
Shell Script for going through a dir recursively and chmodding based on conditions of file type
Can anyone point me to either code or a tutorial for writing a shell script that can recursively go through an entire directory structure (starting at the current working directory, or given an ...
7
votes
4answers
5k views
How to introduce timeout for shell scripting?
I want to run a shell script that got a loop in it and it can go for ever which I do not want to happen. So I need to introduce a timeout for the whole script.
How can I introduce a timeout for the ...
7
votes
4answers
557 views
What is the easiest way to execute text from tail at the command line?
Sometimes I'm working on a new (ubuntu) box and I type git and am alerted:
The program 'git' is currently not installed. You can install it by typing:
apt-get install git-core
If that happens I ...
7
votes
1answer
308 views
ftp and shebang
Is there a way to shebang-ify ftp and write small ftp scripts?
For example:
#!/usr/bin/ftp
open 192.168.1.1
put *.gz
quit
Any thoughts?
7
votes
1answer
134 views
“w | tail” chops text at right margin (78 characters)
Summary: w | tail chops rows after 78th column.
When I run the "w" command, it works as expected. However, when I pipe the output to "tail", it chops the output to 78 columns wide (truncates anything ...
7
votes
3answers
3k views
Shell script fails: Syntax error: “(” unexpected
I've been working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but ...
7
votes
5answers
1k views
How to run part of a script with reduced privileges?
I have the following problem: On every machine running Postgresql there is a special user postgres. This user has administrative access to the database server.
Now I want to write a Bash script that ...
7
votes
3answers
228 views
Do you prefer bash scripts or aliases for shortcuts?
I have a ~/bin directory (which is on my PATH) where I store a lot of little 1 or 2 line scripts. Some of them just cd into a directory and run a command on a file, like vim or something. But I also ...
7
votes
4answers
6k views
Copy/rename multiple files using regular expression (shell script)
I have a collection of files matching a pattern such as 'assignment02.cc', 'assignment02.h', 'assignment02.txt', etc. I would like to copy/rename these files into 'assignment03.cc', 'assignment03.h', ...
7
votes
2answers
261 views
Strip // Comments From Files
What's the best way to strip all code comments in a given directory? I'd like to strip out all // ... EOL comments, and /* blah \*/ (or /** ... \*/) comments as well.
This is a PHP project, and I'd ...
6
votes
7answers
619 views
Slick one-liner to convert a list like “1: 2, 3, 4, 5” to “1.2, 1.3, 1.4, 1.5”
Let's say I have a file that looks something like this:
23: a, b, c, d
24: b, d, f
25: c, g
and I want to get output like this:
23.a
23.b
23.c
23.d
24.b
24.d
24.f
25.c
25.g
Of course it's not ...
6
votes
3answers
1k views
How to “send” variable to sub-shell?
I run the following script:
VAR="Test"
sh -c 'echo "Hello $VAR"'
But I get :
# ./test.sh
Hello
How can I "send" the variable VAR of my script to the shell created with sh -c?
6
votes
4answers
2k views
How do I handle switches in a shell script?
Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments ...
6
votes
7answers
659 views
Bash - Continuous String Manipulation
#!/bin/bash
FILE="$(basename "$1")"
FILE="${FILE/%.jpeg/.jpg}"
Is there anyway to glue these two lines together into a one-liner?
6
votes
5answers
678 views
Bash CD up until in certain folder
I do a lot of work in Magento, and so do a lot of the people I work with, and it gets annoying to have to type:
cd ../../../../../../
To only find you're still a few directories from your root ...
6
votes
4answers
7k views
How to delete a file on remote machine via SSH by using a Shell Script?
I am writing a Shell Script where I have to delete a file on a remote machine via a Shell Script.
Flow manually:
ssh username@domain.com
.. then at domain:
cd ./some/where
rm ...
6
votes
4answers
2k views
How can I detect if the shell is controlled from SSH?
I want to detect from a shell script (more specifically .zshrc) if it is controlled through SSH. I tried the HOST variable but it's always the name of the computer which is running the shell. Can I ...
6
votes
4answers
2k views
How to define and load your own shell function in zsh
I am having a hard time defining and running my own shell functions in zsh. I followed the instructions on the official documentation and tried with easy example first, but I failed to get it work.
...
6
votes
4answers
3k views
How to read from two input files using while loop
I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB.
FileA:
[jaypal:~/Temp] cat ...
6
votes
4answers
6k views
How to inject keystrokes via a shell script?
I'm trying to inject keystrokes into the input daemon so as to simulate typing from a Bash script. Is this possible, and if so, how can I make it happen?
6
votes
2answers
1k views
Why does `exec 2>&1` fail in this bourne shell script?
I am porting an old ksh script to Bourne shell. The old ksh script contains the following code:
#!/bin/sh
tmpLog=/var/tmp/logfile.$$
exec 1> $tmpLog
exec 2>&1
eval $*
another_command_1
...
6
votes
2answers
792 views
Can't understand this script. bash <<(curl -s http://xxx.com)
In a rails book, the author wrote the script to install ruby rvm.
bash <<(curl -s https://rvm.beginrescueend.com/install/rvm)
But I don't understand how it works. Could you explain this?
6
votes
4answers
969 views
How to prepend a license header recursively for all .h and .cpp files in a directory
I'm trying to add a license header to all header files and source files in a project directory using a for loop. This is not working, is there any other approach using sed?
6
votes
2answers
373 views
Measuring time within a script
This thread shows how to measure the time it takes to run a script. In my case, I am interested in measuring time between two points within a script. Here is an example of how I would like to use ...
6
votes
1answer
308 views
Bash script doesn't read input
I have a script that is suppose to run a command in the background, and it does that. The problem is that when the script comes upon a read command, it doesn't pause and accept input. Here it is:
...
6
votes
1answer
338 views
What is wrong with this shell script?
The service for a piece of software I just installed (from a provided .deb file) on a Ubuntu server won't start. When running the service I get the error:
/etc/init.d/matrixsa: 44: Syntax error: "}" ...
6
votes
6answers
4k views
Shell script for moving oldest files?
How do I write a script for moving just the 20 oldest files from one folder to another? Is there a way to grab the oldest files in a folder?
6
votes
5answers
563 views
Binary to hexadecimal and decimal in a shell script
I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
6
votes
3answers
7k views
What characters do I need to escape when using sed in a sh script?
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
6
votes
2answers
917 views
bash: Piping for loop output prevents local variable modification
First off, sorry for the title. I'm not sure of the correct terminology so if anyone can improve upon it that would be good.
I am trying to write a simple bash function that takes, as it's arguments, ...
6
votes
3answers
236 views
What's the difference between Ctrl-Z and kill -STOP?
When I run a command (make on a large project) from the shell, I can type Ctrl-Z to stop the process and return to the shell. Subsequently, I can run fg to continue the process.
I'm trying to write a ...
6
votes
2answers
4k views
Remove end of line characters from stdout? Multiple lines into a single line
I have a script which outputs the following text. This is the output from a Netopia 2210-02 ADSL2 modem.
ADSL Line State: Up
ADSL Startup Attempts: 1
ADSL Modulation: DMT
ADSL Data ...
6
votes
2answers
113 views
Decode Flags for Kill Command
I am new to Shell Scripting.
I saw this Command on a Book for Linux shell Scripting
$ kill -HUP 1234 >killout.txt 2>killerr.txt
I know what the command does, but i am not able to understand ...
6
votes
1answer
221 views
How to find out whether a shell script is written in bash or sh
How can I find out whether a script is written in bash or sh?
The first line of the script is not helpful here, since on Linux, bash scripts have this line:
#!bin/sh
Actually, there are many ...
