A shell script is a script written for the shell, or command line interpreter, of an operating system.
4
votes
4answers
382 views
Is there any book , Tutorial on very very advanced shell scripting
I am looking for some tutorial where a simple script is written using very advanced methods so that i can learn more from
4
votes
1answer
744 views
Bash script IDE
Is there a bash/ksh/any shell script IDE.
Don't you get annoyed when you forget the space inside if or I don't know, some minor syntax mistakes you do from time to time, but takes you a long time to ...
4
votes
2answers
56 views
Specifying a generic interpreter for a program like expect?
I am writing expect script which can be used on mulple operating systems but the problem is
i can't use #!/usr/bin/expect evreywhere so instead i tried to do
#!`which expect`
at the top but it ...
4
votes
2answers
95 views
How to define a Bash function that can be used by different scripts
I have defined a bash function in my ~/.bashrc file. This allows me to use it in shell terminals. However, it does not seem to exist when I call it from within a script.
How can I define a bash ...
4
votes
1answer
224 views
Automate workflow in shell
Coming from GUI interfaces I've come to finally understand the power of having everything piped in text, but I don't really know how to use all that power yet.
I realized the workflow for a ...
4
votes
1answer
4k views
automate gnuplot plotting with bash
I have 6 files which need to be plotted as line graphs with error margins and output them to different png files. The file format is as follows.
seconds mean-average min max
How would I go about ...
4
votes
4answers
1k views
How to perform a sed in-place substitution that only creates backups of files that were changed?
I ran the following to replace a term used in all files in the current working directory:
$ find . -type f -print0 | xargs -0 sed -i'.bup' -e's/Ms. Johnson/Mrs. Melbin/g'
This performed the word ...
4
votes
3answers
2k views
Print shell arguments in reverse order
I am a bit stuck. My task is to print the arguments to my script in reverse order except the third and fourth.
What I have is this code:
#!/bin/bash
i=$#
for arg in "$@"
do
case $i
in
...
4
votes
1answer
365 views
Shell script printing the most visited directories
Is there a way to write a bash script with the following functionalities?
Be launched when I press some key or key combination. (not so important requirement)
Identify the 7 most visited directories ...
4
votes
3answers
2k views
Why is my variable being localized in one 'while read' loop, but not in another seemingly similar loop
Why do I get different values for $x from the snippets below?
#!/bin/bash
x=1
echo fred>junk ; while read var ; do x=55 ; done <junk
echo x=$x
# x=55 .. I'd expect this result
x=1
cat ...
4
votes
3answers
196 views
Best way to work through / display a tree of images sorted by size
I've got a deep directory tree containing .PNG files. I'd like to find all the .PNG files in the directory, sort them in order of size from smallest to largest, and then display every 50th image.
...
4
votes
2answers
112 views
Is it possible to store ssh connection in bash script instead of $(ssh user@ip command) every time?
I think that the code that I already have is not very efficient as it has to connect each time to the same machine and execute a command. Code:
tmp=$(ssh -nq $USER@$IP "$COMMAND" 2>> $LOG)
...
4
votes
2answers
190 views
Shell script to backup directories
I'm running eclipse on windows and would like to automatically backup my src direcories for all my projects. I installed cygwin and tried to make a simple backup script but I am having trouble ...
4
votes
1answer
450 views
How to create a confirmation question in Linux?
I have a certain command (git push server-name) that has major consequences. How to require confirmation for this command only? It should ignore white space.
The confirmation could be Enter 'yes i am ...
4
votes
1answer
204 views
Writing command synopsis in standard man format
I am writing a script and in the usage() function I want to specify the usage.
My script uses either option1 or option2 or both. One of them is mandatory.
Is there a standard way to write it up?
4
votes
2answers
258 views
Bash- detect init system
This may have more to do with detecting operating systems, but I specifically need the init system currently in use on the system.
Fedora 15 now uses systemd, Ubuntu uses Upstart, while others use ...
4
votes
1answer
781 views
awk: forcing a return status?
This is a followup to my earlier question.
I am validating the number of fields in /etc/passwd using this handy snippit. In the following example, the users 'fieldcount1' and 'fieldcount2' have the ...
4
votes
1answer
2k views
Recursively compare directory contents by name, ignoring file extensions
I have a directory containing about 7,000 music files. I used lame to recursively re-encode all files in it to a separate directory, outputting all files with the same relative path and file name. ...
4
votes
1answer
31 views
What do use the -t “tag” option for in logger?
When using the "logger" command to write messages to the system logs from scripts, I use the -t "tag" to store the scripts name along side the message it's written to the log.
I do this to make ...
4
votes
2answers
82 views
Extracting queries from log file that do not include banned lines
I have a log file that looks something like the following:
query1 startQuery
query1 do something
query1 do something else
query2 startQuery
query1 do something banned
query2 do something
query3 ...
4
votes
2answers
131 views
Dealing with script interruption
I need to iterate over between 120k and 500k files. find handles this well.
find $PWD -type f -path "fragments/*.pdbqt"
For some reason I want to list the same set of files again, in the same ...
4
votes
1answer
73 views
Transform directory structure
I have the following folder structure
root folder
|-al2
|- GER.zip
|- ENG.zip
|-ww
|- GER.zip
|- ENG.zip
and so on. The folder names are random strings.
I want to sort the files in ...
4
votes
1answer
218 views
In a script, check if standard input comes from a file or pipe
I know this has been poorly covered previously, but those answers either lack explanation or don't apply.
Basically at some point my scripts needs check if a file is specified, if it was it will be ...
4
votes
4answers
424 views
script for logging all the stats for a particular process
I want to monitor CPU usage, disk read/write usage for a particular process, say ./myprocess.
To monitor CPU top command seems to be a nice option and for read and write iotop seems to be a handy ...
4
votes
1answer
221 views
Passing clicked links in rxvt to a script
I start my browser vimprobable in tabbed like so:
"`tabbed -d > /tmp/tabbed.xid`; vimprobable2 -e `cat /tmp/tabbed.xid`" [1]
I am then able to send all new instances of vimprobable to tabbed (and ...
4
votes
1answer
76 views
How can I synchronize processes started in different TMUX panes?
Consider this:
tmux split-window -d program1
program2 # this program depends on some side effects produced by program1
In this case, program2 will start before program1 is ready. program1 will send ...
4
votes
2answers
329 views
Get line number in a Bourne shell script
I'd like to be able to print the current line number in a shell script. I know about the $LINENO variable in Bash shells, but it doesn't seem to exist in Bourne shells. Is there any other variable ...
4
votes
4answers
1k views
Pgrep returns mutiple PIDs
My script accepts a process name as an input and kills it, I tried using pgrep but its returning two PIDs , one for the process which is running and one for the script which accepts the process name ...
4
votes
1answer
135 views
Discard stdout of a command for t seconds
I am working on some batch scripts involving the following:
Run some non-terminating sub-processes (asynchronously)
Wait for t seconds
Perform other task X for some time
Terminate subprocesses
...
4
votes
1answer
293 views
Best way to swap filenames
I need to swap filenames of two files (file and file_1). I'm using the following code for it.
mv file .phfile
mv file_1 file
mv .phfile file
This works but is very buggy, It sometimes even results ...
4
votes
3answers
4k views
How to set global environment variables at boot through a script, and have them available for an application that runs before login?
I have a service that runs at boot, and in that service it calls a bash script in the background that exports some environment variables. The problem I'm having is that those environment variables ...
4
votes
1answer
145 views
what is the zsh equivalent of bash's export -f
So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, ...
4
votes
0answers
101 views
How to code shell script with command line arguments like -la [duplicate]
Possible Duplicate:
How do I handle switches in a shell script?
I know how to deal with simple arguments $1-$9 but I don't know how to code it with such as -la, for example ls -la. Could ...
3
votes
2answers
446 views
How does a script/program executes in unix?
I have a doubt about the execution pattern of a unix script.
For example a script containing lines:
command B
function C()
{
Myscript // Myscript is a script
command D
}
command E
and ...
3
votes
3answers
475 views
In Unix speak what is the difference between a shell script and an executable?
I have seen this question on this site and this prompted me to ask this question . I want to know in Unix speak what is the difference between an executable and a shell script ?
3
votes
2answers
371 views
bash myscript.sh runs in bash, but the first line is #!/usr/bin/sh
I have a script similar following one:
#!/usr/bin/sh
var="ABC"
if [ $var == "ABC" ]
then
echo True
else
echo False
fi
Above code does not work in Solaris Sparc and Solaris X64. It is showing ...
3
votes
2answers
876 views
How to write a script to execute files in multiple directories
How do I write a script to execute the files in multiple directories?
The problem is this: I have many directories, and each has a data file to be read and analyzed
by a python script (say, a.py). I ...
3
votes
3answers
147 views
Zsh function with su and echo
I'm trying to add a function to my .zshrc that makes adding new USE flags to my /etc/portage/package.use file easier. Normally, I'd have to do
su -c 'echo "net-misc/aria2 bash-completion bittorrent" ...
3
votes
2answers
723 views
Passing a bash command-line argument containing a dot
How can a command-line argument containing a dot (.) be passed? Are there any escape sequences for capturing characters like dot?
The following invocation of a bash-script from the shell does not ...
3
votes
4answers
103 views
Replacing pattern after nth match is found on each line?
I have a file containing lines:
india;austria;japan;chile
china;US;nigeria;mexico;russia
I want to replace all the occurences of semicolon on each line with e.g. ;NEW;, but starting from the 2nd ...
3
votes
2answers
1k views
Executing a shell script from remote server on local machine
Imagine a shell script on the remote server as
#!/bin/bash
rm /test.x
How can I (if possible) to execute this script from my local machine to delete /test.x file on my local machine. Obviously, the ...
3
votes
5answers
256 views
Remove lines based on pattern by keeping first n lines
I need to remove lines from a text file based on pattern but I need to keep the first n lines of that pattern.
Input
% 1
% 2
% 3
% 4
% 5
text1
text2
text3
output
%1
%2
text1
text2
text3
I used ...
3
votes
3answers
1k views
Error handling in shell script
I wrote a shell script run_script.sh, which includes a step which creates an empty file run_script.lck. Everytime the shell script was called by the cronjob, it will check for the existence of ...
3
votes
5answers
2k views
Script to list only files of type ASCII text in the current directory?
How to write a shell script which searches the current UNIX directory and returns the names of all files of type ASCII text?
3
votes
2answers
3k views
How to get elapsed time from two “text based” dates
I would like to know if there is a way to calculate the difference between two times in bash. I have two fields that are extracted from a log file:
Start time: Feb 12 10:02:10
End time: Feb 12 ...
3
votes
2answers
866 views
automatically load shell scripts from /usr/bin
Is there a way to have bash automatically load shell scripts I create and place into /usr/bin?
Or is there a different place I should put custom shell scripts so that they are automatically loaded?
...
3
votes
2answers
87 views
Clarification regarding behavior of shell script along with pipe
I need help understanding the behaviour of the following shell script.
#!/bin/bash
echo "First more"
more $1
echo "First echo"
echo $1
echo "Second more"
more $1
When I run the following command:
...
3
votes
2answers
305 views
exec < filename - what does this do?
I have found the following code snippet (sh):
TESTFILE=$TEST_PATH/test.out
if [ -f $TESTFILE ]
then
exec < $TESTFILE
else
echo "$TEST_PATH: no test.out file"
fi
...
3
votes
2answers
4k views
How do I delete the first n lines of an ascii file using shell commands?
I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so ...
3
votes
4answers
216 views
Run a given command on every subpath of a long path
Say I have a long path such as:
/a/b/c/d/e/f
I would like to run a command only on each of the subpaths of the path:
e.g. If my command is cmd, I am looking for a one-liner that can do (perhaps ...