Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

4
votes
5answers
9k views

Match regex in ksh

I am looking to do something like this in KSH: if (( $var = (foo|bar)[0-9]*$ )); then print "variable matched regex" fi Is it possible at all? For the record I'm using Ksh Version M-11/16/88i ...
1
vote
2answers
162 views

How to log if an environment variable is unset? [closed]

How can I write a shell script that checks for an environment variable and writes to a log file if the variable is unset? Also, if the file name pointed by env variable doesn't exist, how to create ...
4
votes
3answers
889 views

What's the best way to determine the working directory for UNIX process using 'ps'?

I am running a Ruby command line script (rufus.sh)which ultimately calls Thread.new, which spawns a UNIX process as shown below. I run this script for more than 1 directory as the output of the ps ...
5
votes
2answers
1k views

How to list files and directories with directories first

I have two questions. First, which command lists files and directories, but lists directories first? Second question: I want to copy a list of files into a single directory, but make the target ...
2
votes
1answer
992 views

How synchronize two disks (or folders) using symbolic (or hard) links (like rsync)?

I'm searching a tool or a shell script or another program to synchronize two or more folders or disks. But it differs of rsync tool, because, I don't want to duplicate the missing file, but do a ...
8
votes
6answers
8k 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?
1
vote
2answers
1k views

regex find and replace 0x0D, 0x0A characters

I have a text file of a database dump with some line break characters (0x0A0x0D) in the middle of lines. I want to replace them with commas, but I can't do it simply, because those characters are the ...
4
votes
0answers
105 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
1answer
2k views

Split video file into pieces with ffmpeg

When you want to extract two minutes of a video file from minute 2 to minute 4, you will use command ffmpeg -i input.mpg -sameq -ss 00:02:00 -t 00:02:00 output.mpg I'd like to split the video into ...
2
votes
2answers
238 views

List files that occur lexically after/greater than a given prefix (or, run database upgrade scripts in a proper order)

I'm trying to create a shell script that runs through a bunch of SQL scripts. I've these files: $ ls upgrade/ 01-foo.sql 02-bar.sql 02-baz.sql 03-foo1.sql 04-buz.sql And I've got the version of ...
1
vote
3answers
422 views

Evalute passed in variable in bash

Is there a way to pass in a variable as an argument to a bash script and have it evaluated scoped by the bash script? Given: # cat /path/to/file/of/host/names bob tom joe etc... # dofor FILE=$1 ...
7
votes
2answers
1k 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.
29
votes
10answers
15k views

Correct locking in shell scripts?

Sometimes you have to make sure that only one instance of a shell script is running at the same time. For example a cron job which is executed via crond that does not provide locking on its own (e.g. ...
3
votes
2answers
2k views

Ignore escape characters when printing string in shell script

I just wrote a simple shell script to save a LaTeX expression as a PNG file. It works fine, except for the LaTeX-syntax for "next row", i.e. the double backslash \\. When for instance my input ...
9
votes
2answers
2k views

Piping for loop output prevents local variable modification

I'm trying to write a simple bash function that takes, as its arguments, a number of files and/or directories. It should: Fully qualify the filenames. Sort them. Remove duplicates. Print all that ...
3
votes
2answers
3k views

Using nohup at /etc/init.d/service

I have been trying to create a daemon using /etc/init.d/myservice and it will call a shell script that contains nohup within the script line. Will it affect it if I could do service stop later?
5
votes
4answers
5k views

Is there a way to sum up the size of files listed?

This is the command I am using to list some files: find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \; -rw-r--r-- 1 qa1wrk15 test 1265190 Sep 29 01:14 ...
9
votes
1answer
664 views

How do you omit “./” when running scripts on current directory

On some tutorials over the internet they can run a script on the current directory without doing: ./script How do you omit this? I am using CentOS 5.6 x64. I want to run it this way script
5
votes
3answers
4k 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 ...
20
votes
1answer
3k views

Execute bash scripts on entering a directory

What is the best way to execute a script when entering into a directory? When I move into a new directory I would like bash to execute the projectSettings.bash script much like RVM does.
11
votes
1answer
765 views

How to generate a lot of (blank) images files?

For a test, I need to quickly create 1700 jpg-images (even blank) in a defined size 1000x706. I wonder if we could do this with a shell script or a program (like ImageMagick). I wish they called ...
3
votes
3answers
3k 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 ...
1
vote
1answer
161 views

How does this bash function work?

I'm looking at this short example, and I don't see how this function works: function EXT_COLOR () { echo -ne "\[\033[38;5;$1m\]"; } AFAIK -ne stands for not equal. What are we comparing here? I ...
3
votes
2answers
102 views

Unatended script execution in ubuntu terminal

I have installed Ubuntu on a VMWare virtual machine. When I got to installing a VMWare tools, I have stumbled upon a simple question: If I execute a script (perl script in VMWare tools case), is ...
1
vote
1answer
1k views

Allow www-data to execute a a script

I am trying to execute a script that will read a file written by PHP. The problem is that the script opens and some other files (C-ISAM file based database) and i have no clue what permisions to give ...
9
votes
4answers
5k 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 ...
1
vote
2answers
3k views

Where should I put a script that needs to execute once when the user logs in graphically?

Using Gnome 3 on Fedora 15, where should I put a script that needs to execute once when the user logs in graphically?
11
votes
5answers
5k views

How can I use environment variables in my shebang?

I have a Python script that need to be run with a particular python installation. Is there a way to craft a shebang so that it runs with $FOO/bar/MyCustomPython?
2
votes
1answer
513 views

Default fedora 15 install, how do you get a network drive to mount after login?

I am running a default fedora 15 install. My goal is to have a couple of network drives get mounted right after the user logs in graphically (not via ssh). The script I want to execute is basically ...
3
votes
2answers
285 views

What is the bug with this script - using 'select' command to print menu in linux

OS - Linux 64-bit I am trying to implement a simple menu using select command. The script (for testing purposes) is as follows #!/bin/bash echo "*******************" PS3='Select an option and press ...
8
votes
4answers
2k 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?
11
votes
3answers
1k views

Are free shell accounts safe?

I am trying to identify how safe the free shell accounts provided by the likes of shellmix.com are. I have never used one myself but want to suggest it to someone interested in learning shell ...
3
votes
3answers
392 views

How to “rpm -i” a package only when an older version does not exist?

I wish to install some RPM packages from a shell script. I want to ensure that each package is indeed installed, but I do not want to update a package that would already have an older version ...
4
votes
2answers
95 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 ...
5
votes
1answer
138 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 ...
3
votes
3answers
343 views

Delete files in a directory which are also in another directory

I copied a directory using this: cp -r dir/ ../../ without thinking and realized that it copied the contents of dir instead of actually dir to the above directory. Now I have a mess of files I need ...
2
votes
1answer
591 views

How do I replace certain text with the respective line number using sed?

I have a file where certain (not all) lines look like so: Sequence: n I wish to replace n with the respective line number. How can I do it with sed?
4
votes
3answers
2k views

How to kill two or more processes with one kill command?

I am using the following command to kill one java process via shell script: pidof java|xargs kill -9 $1 What if the list of pidof output has more than one elements? How do I kill them both with ...
2
votes
3answers
489 views

variable concatenation not working?

I have a simple bash shell script which is driving me bonkers. All I want to do is run a command which returns a result which I will then use in another command. The result of the first command ...
2
votes
2answers
3k views

Is there any way I can obfuscate “.sh” shell script?

I'm running Ubuntu. I need to know any way to obfuscate .sh shell script file contents in order to make it very difficult to read. Any suggestion are welcome, including using online obfuscator.
1
vote
4answers
3k views

How to iterate over two sets of iterables in a shell script?

Usually when I write a shell script for a specific task, I just make a list of files like so: #/bin/sh read -d '' imagefiles <<EOF ./01/IMG0000.jpg ./01/IMG0001.jpg ./01/IMG0002.jpg ...
2
votes
2answers
10k views

Bash shell script to locate and remove substring within a filename

I am trying to write a bash shell script in Mac OS X 10.6 (which is called by an Automator Action) to rename TV show DVD rips that I have named badly over the years. I want to remove part of the text ...
5
votes
2answers
3k views

How to pipe md5 hash result in shell

I am looking for a simple way to pipe the result of md5sum into another command. Something like this: $echo -n 'test' | md5sum | ... My problem is that md5sum outputs not only the hash of the ...
6
votes
4answers
2k 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 ...
6
votes
5answers
2k views

How do I configure mutt to display the date header in my local time zone in the pager?

When I view a message in the pager mutt displays the time in the Date header in UTC rather than my local time zone. The index view displays the local time correctly. I found this old mailing list post ...
5
votes
2answers
2k views

Arg list too long error while using find

I have the find command as follows: find /jobs/logs/* -prune -name '*.log' -type f -mtime +1 | while read LOGFILE do echo $LOGFILE done This is failing due to the following ...
8
votes
2answers
727 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 ...
11
votes
9answers
1k views

Detect init system using the shell

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 ...
1
vote
1answer
8k views

Execute a script automatically through pbrun?

There is a Unix environment named sftwrk01, uatwrk01, intwrk01 and some more I need to check the logs in each environment everyday by login in as below (through pbsu which is a wrapper for pbrun): ...
3
votes
2answers
4k views

Replace all occurrences of a character in a variable using korn script

Part of the Korn script I am writing requires that I replace all occurrences of the ' character with two occurrences (''). I am trying to log some SQL that I am generating in this script to a column ...