A shell script is a script written for the shell, or command line interpreter, of an operating system.
6
votes
1answer
3k views
File descriptors & shell scripting
I am having a very hard time understanding how does one use file descriptors in shell scripts.
I know the basics such as
exec 5 > /tmp/foo
So fd 5 is attached to foo for writing.
exec 6 < ...
6
votes
3answers
288 views
shell scripts are still working without #!(sha-bang line)
I am new to shell scripts and many books have written that use #!(sha-bang) line in starting of the script to invoke the interpreter .And this will invoke a new shell for script and do the ...
6
votes
2answers
775 views
XFCE or pure X11 commands, toggle compositing immediately without restarting X
How can i disable compositing via the command line? I need to disable temporarily for some games, like Nexuiz, for use in a wrapper script to toggle compositing status.
6
votes
1answer
342 views
Are dash, ash and sh script 100% compatible?
I wanted to know whether scripts written for dash, ash, and sh are 100% compatible?
Are there any added features to dash or ash, or syntax change?
From what I heard, ash is a direct descendant of ...
6
votes
4answers
2k views
Parallel execution of a program on multiple files
I have a small script that loops through all files of a folder and executes a (usually long lasting) command. Basically it's
for file in ./folder/*;
do
./bin/myProgram $file > ./done/$file
...
6
votes
4answers
732 views
Why do I need to hit enter to get my shell prompt after my init.d script completes?
I had to write my own CentOS init.d script for celery because it only ships with one for Debian. You can see the script I wrote when I answered my own stack overflow question 3989656.
But there's a ...
6
votes
3answers
305 views
getopt, getopts or manual parsing - what to use when I want to support both short and long options?
Currently I'm writing a Bash script which has the following requirements:
it should run on a wide variety of Unix/Linux platforms
it should support both short and (GNU) long options
I know that ...
6
votes
4answers
2k views
printing colored text using echo
I know that for printing a colored text using echo, for example red color, the code is: echo -e "\e[1;31m This is red text \e[0m"
and I know that in this example, 31 is code of red color and the ...
6
votes
3answers
2k views
find -exec in bash script with variable expansion
I'm trying to run a command similar to the one below in a bash script. It should search through all subfolders of $sourcedir and copy all files of a certain type to the root level of $targetdir.
...
6
votes
3answers
629 views
Script to toggle setxkbmap
I would like a bash script that toggles between:
setxkbmap se and setxkbmap us.
Then my intentions are to map that script to CtrlEsc through the Keyboard > custom shortcuts.
The point is that I ...
6
votes
1answer
218 views
redirect and log script output
I am trying to tidy up the following snippets, design goals are to log all output from a script, and should not be a wrapper. Less lines are better.
I don't care about user inputs (at this stage), ...
6
votes
1answer
110 views
Where can I find out how to write a shell script that is as portable as possible? [duplicate]
Possible Duplicate:
Resources for portable shell programming
I want to write a shell script that is intended for further distribution. So I'd like to write it in a way that is as portable ...
5
votes
3answers
3k views
Processing bash variable with sed
Been banging my head off a wall on this
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
I want to parse these into a variable called LAT and ...
5
votes
3answers
534 views
Is there a more elegant way to count words and assign that count to variables?
I have a script:
#!/bin/bash
/root/xiotech status > xiostatus.tmp
SyncCount=$(grep -c Sync xiostatus.tmp)
PauseCount=$(grep -c paused xiostatus.tmp)
CopyingCount=$(grep -c Copying xiostatus.tmp)
...
5
votes
3answers
2k views
Storing output of command in shell variable
I have an operation using cut that I would like to assign result to a variable
var4=ztemp.xml |cut -f1 -d '.'
I get the error:
ztemp.xml is not a command
The value of var4 never gets ...
5
votes
3answers
2k views
How to run a script from another path, and know the script's path?
I need to run a script that access a file in the same path it is located.
For example: I have the script in /home/me/folder/script.sh, and this script will access the file /home/me/folder/myfile. As ...
5
votes
4answers
572 views
How to create a testcolor.sh like the following screenshot?
I found this image on the internet, but don't know how was it made,
Can anyone provide a sample on that? I just would like to have a color table as well.
5
votes
3answers
6k views
How to write a bash script, that logs onto an other machine to do stuff?
Is it possible to write a bash script, that
would be started from machine A, logs in on a different machine B by ssh (both machines A and B would be Linux-Machines),
copys some files on to machine B
...
5
votes
3answers
250 views
How to generate new var names on the fly
I'm trying to generate dynamic var names in a shell script to process a set of files with distinct names in a loop as follows:
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
...
5
votes
1answer
121 views
How would one detect if external command exists in a script?
Is there a way to detect if an external command exists (i.e. wget, svn)?
More specifically today, i was trying to run one of my scripts i wrote and the person didn't have wget or svn installed.
The ...
5
votes
2answers
201 views
Find all files with a Python Shebang
I'm trying to run a PEP8 check against a large source tree. The tree is comprised of a mix of files in various languages. The idea is to check all the Python scripts without having to explicitly list ...
5
votes
2answers
173 views
what is the meaning of 1 at the end of awk script
I was reading this awk script
awk -F"=" '{OFS="=";gsub(",",";",$2)}1'
I want to know what is the function of 1 at the end of it
5
votes
2answers
291 views
Extracting a string, according to a pattern, in a bash script
In bash, suppose that I have a string strname:
strname="ph7go04325r"
I would like to extract the characters between the first "3" character and the last "r" character in strname, saving the result ...
5
votes
5answers
2k views
Compare files that are in directory 1 but not directory 2?
I'm having trouble with a bash script I want to make
I know ls will list files that are in a directory but I want it to list directories that are in directory1 but NOT in directory2, and then list ...
5
votes
2answers
99 views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
5
votes
1answer
197 views
In the usage string of my custom shell script, shall I also display --help and --version?
I'm currently developing a shell script, called up, which shows a usage string on the commandline when called with --help. The output looks like this:
$ up --help
usage: up [-n ...
5
votes
3answers
781 views
exit shell script from a subshell
Consider this snippet:
stop () {
echo "${1}" 1>&2
exit 1
}
func () {
if false; then
echo "foo"
else
stop "something went wrong"
fi
}
Normally when func ...
5
votes
4answers
3k views
How can I use bash's if test and find commands together?
I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command.
The log files are stored in this format:
/var/log/crashes/app-2012-08-28.log
...
5
votes
2answers
554 views
Splitting bash command line argument
Is this the best way to split up a colon separated bash command line argument?
#!/bin/bash
hostlist=`echo $1| awk '{split($0,Ip,":")} END{for (var in Ip) print Ip[var];}'`
for host in $hostlist
do
...
5
votes
2answers
433 views
How to get top 5 most ram intensive applications from Bash?
So I look at avaliable to me servers load
and see that some other user has created some really ram intensive app that kills my server hosting abileties. I wonder what is bash command to get top 5 ...
5
votes
1answer
4k views
How to check if there are no parameters provided to a command?
How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
5
votes
4answers
249 views
Tool to create text files from a template
I have to regularly create 100+ text files from templates.
I currently use an overly complicated shell script. I think there is a smarter way to handle this, but I don't know how.
I have a "database" ...
5
votes
5answers
752 views
Unix bash script check empty gz files
I'm trying to write a bash script that will go into a directory loop through the .gz files and delete them if they are empty (ie the uncompressed file contained within in empty.
I've got a couple of ...
5
votes
2answers
560 views
How to tweet using terminal?
I would like to tweet a message using terminal.
I tried something like:
curl -u 'TwitterUsername':'TwitterPassword' -d status=”Your Message Here” https://twitter.com/statuses/update.xml
but seems ...
5
votes
2answers
228 views
vim: create file with +x bit
Is there any way to set +x bit on script while creating?
For example I run:
vim -some_option_to_make_file_executable script.sh
and after saving I can run file without any additional movings.
ps. ...
5
votes
3answers
657 views
How can I profile a shell script?
I have several programs that i'm executing in a shell script:
./myprogram1
./myprogram2
...
I know that I can profile each individual program by editing the sourcecode, but I wanted to know if ...
5
votes
2answers
4k views
Can I make scripts use aliases instead of commands?
I have an alias for a command (I'm setting up a Python development environment)
alias python=~/virtualenv/bin/python
so that I can run ~/virtualenv/bin/python by just typing python. Now in my ...
5
votes
3answers
3k views
Find files which are created a certain time after or before a particular file was created
I need a shell script which finds files which are created 1 hour before or 1 hour after a particular file (test.txt) was created.
If I go with find -newer, that means I'd have to create a temporary ...
5
votes
3answers
667 views
killing processes automatically
I need to kill all processs in a certain shell excluding certain processes.
Like sh which is my shell. And the comand.
This is what currently in my shell right now.
rcihp146 :/home/msingh2> ps
...
5
votes
1answer
818 views
Reason for ksh obsoleting -eq
The latest version of ksh obsoletes using -eq within [[ ]] blocks, prefering (( )) instead. Why is this? I can't find any documentation on the advantages of (( )) over [[ ]] anywhere, and I find that ...
5
votes
2answers
86 views
[ vs [[ : which one to use in bash scripts? [duplicate]
The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible.
The relevant section states:
The command attempts to ...
5
votes
1answer
1k views
Executing a script in zsh - file permissions
I'm confused about execute file permissions not behaving as I expect. Probably because my expectations are wrong. Anyway:
I have a script file, for simplicity is just called s, located in ~/bin. For ...
5
votes
1answer
426 views
A couple arithmetic related commands, Dash, POSIX shell, sh, Increment
When pre/post incrementing a variable, in POSIX Shell, what is the difference between these two examples. They seem like they would have the same consequences, but would they be used differently? ...
5
votes
1answer
300 views
How to find out how many cores (including emulated via HT and such) are avaliable to user?
I wonder how to get how many cores (including emulated via HT and such) are avaliable to user via bash shell-script command?
5
votes
2answers
387 views
How enable safeguards for CPU temperature
My desktop has a nasty habit. When I have several high intensity applications running and my CPU is at maximum usage for a period of time, the core temperature rises and my computer auto-shuts off.
...
5
votes
1answer
877 views
Converting html table into ncurses-like ascii table?
I have some documentation in an html table. I want to convert the table to an ascii table, like you see when you get a mysql command line result back, like this:
+------------+------------+
| header ...
5
votes
3answers
810 views
GUI input for shell script
I have a shell script which installs a program in RedHat. I need to add a GUI for entering some parameters so that it looks like an installation wizard . I tried Xdialog, Zenity, and Yad, but they ...
5
votes
2answers
144 views
Is it safe to remove a script file from within that script?
Imagine I have a script foo. It should be run once when the user logs in and isn't needed after a successful run.
My question: Is it safe to remove the script file from within the script?
E.g.:
...
5
votes
1answer
900 views
Cannot open: Input/output error with tar command in a shell script
I am trying to create a tar ball in a shell script (I have enabled set -x), but I get an error:
+ cd /home5/mysite/public_html
+ TAR_DUMP=gypo_2012-02-18-03:51:15.tar.gz
+ echo 'Tar name: ...
5
votes
4answers
991 views
Allow users to run only specific binaries with root permissions/privileges
I would like to allow a specific user to be able to sudo /sbin/iptables only.
I have a bash script which configures iptables. The problem is that configuring /sbin/iptables as sudoable is not enough ...