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

learn more… | top users | synonyms (2)

4
votes
1answer
640 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
5answers
1k views

Execute string result from shell script as a set of shell commands

I create a string result from my shell script and i want to execute it as a shell command. For example file_a is: user1@gmail.com user2@gmail.com script: awk '{print "mail -s \"welcome\"", $1}' ...
0
votes
1answer
412 views

Shell script to check server state

I am writing a script that would ssh into the VMWare server and check if the VMs are up or not. There's some error in the server=awk "/$i/{print $2}" /home/dharmit/scripts/vmids line. I am getting ...
1
vote
1answer
233 views

Background script run at login interrupted when I run tmux in terminal

In ~/.profile I run a script in the background (redshift for changing the screen temperature at night time). After logging in, I can open a terminal, and run tmux. However, at this point, redshift ...
1
vote
2answers
906 views

The receiving of returns by Nagios from script through NRPE

I am using nagios3 and want to collect some information from nrpe_server. Next script is at nrpe_server: #!/bin/sh #set -x # script for checking disk usage on ZFS # requires min zpool version 13 or ...
3
votes
2answers
3k views

Making an ext3 filesystem image without mkfs.ext3

On ubuntu I'm using the following to create a ext3 filesystem image system.img. dd if=/dev/zero of=./system.img bs=1000000 count=200 mkfs.ext3 ./system.img I'm attempting to do the same on android ...
4
votes
3answers
4k views

How to get subshell's PID in Korn Shell (equivalent of $BASHPID)

In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below: #!/usr/bin/ksh93 echo ...
3
votes
5answers
607 views

changing pattern of a text file

A text file has contents something like chair table pen desk Now i want it to be changed and stored in a variable say var as below ('chair','chair'),('table','table'),('pen','pen'),('desk','desk') ...
3
votes
2answers
909 views

zsh: excluding files from a pattern

Say I have the following files: |-- bar `-- foo |-- type_A_1 |-- type_A_2 |-- type_B_1 |-- type_B_2 |-- type_B_xx |-- type_B_xx `-- something_else I thought the ...
1
vote
1answer
351 views

Can I get the backup option of cp and mv to work with directories?

--backup[=CONTROL] make a backup of each existing destination file The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this ...
3
votes
2answers
929 views

Keep global variables values piping through functions

I wrote a little bash script using sed on some html pages to extract some urls. To avoid each time grabbing sed results in a variable then read it again I simply made 3 functions and piped together. ...
3
votes
2answers
2k views

Creating a variable with sed in ksh

I'm using Ksh88 on a Solaris 10 machine. If I do the following: foo="one two three" for i in $foo; do echo $i; done The script executes as expected: $ ./script.ksh one two three However if ...
0
votes
1answer
380 views

Shell script executing in the terminal but not from shell script file [duplicate]

Possible Duplicate: How can I make variables “exported” in a bash script stick around? I have a problem with executing script from file. When I type in command line ...
13
votes
3answers
16k views

Testing if a variable is empty in a shell script

I have seen the following technique used many times on many different shells, to test if a variable is empty: if [ "x$1" = "x" ]; then # Variable is empty fi Are there any advantages on using ...
4
votes
2answers
996 views

How to output file & ignoring lines that start with “?”?

I do svn status --show-updates and then I want to either Q1: ignore (not to display) lines that start with ? Q2: display only lines that start with * Note that there are few spaces before * occurs. ...
0
votes
2answers
228 views

Autentification on SSH connection in OneLine [duplicate]

Possible Duplicate: Shell Script for logging into a ssh server I need to connect via SSH to a remote server, however I must do this with out interactive mode and with out use Public keys ...
0
votes
1answer
646 views

Calling a script in ~/.profile stops my logging in

I placed a call to a bash script in my ~/.profile. The script executes a CLI program to dim my screen at login. However, Gnome doesn't continue to log me in to my standard desktop after running the ...
1
vote
3answers
502 views

Problem with bash script executed from website

I have just begun writing scripts in bash and I am putting some of them on my web server (whihc is located on my virtual Debian 6.0.4 machine). I have installed lighttpd on this machine.and when I ...
1
vote
1answer
77 views

Can someone clarify: 'case “:$PATH:” in' in script?

I am not an expert at Unix scripting. An answer to another question suggests using the following piece of code in a script: case ":$PATH:" in *:$HOME/mydir:*) echo it is in the path;; *) echo not ...
1
vote
2answers
587 views

Checking if PATH contains $HOME/mydir and adding it if not (all in a script) [duplicate]

Possible Duplicate: keep duplicates out of $PATH on source I am not an expert with Unix scripting. Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from ...
5
votes
1answer
2k 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: ...
3
votes
1answer
641 views

merging files and getting column values based on id field

bash-3.2$ cat sample.log sample.log.1 sample.log.2 ID COL1 COL2 COL4 1 col1 col2 col4 2 c1 c2 c4 3 co1 co2 co4 ID COL3 COL1 1 col3 col1 2 c3 c1 3 co3 co1 ID COL1 COL2 COL3 1 col1 ...
1
vote
3answers
2k views

Script to show number of files (by file type) in a directory?

I want to show this kind of statistics about a particular directory: $ myscript.sh .c 34265 files .h 17866 files .cbl 1804 files .pco 389 files [...] Is there a tool that can do this? Here is my ...
5
votes
2answers
4k views

Set Variable Environment Variables in bash (or other)

I want my script to read a file containing key/value pairs of environment variables to set, and then to set them. So far, I have this: #!/bin/bash cat $1 | while read kv do key=${kv%=*} ...
2
votes
1answer
142 views

Files with no .sh extension not considered for autocompletion in zsh

I have noticed that unless I add an .sh extension to my shell scripts, Zsh does not suggest them for autocompletion for execution despite having them in my PATH. I have the same problem if I cd into ...
9
votes
3answers
15k views

File extensions for unix shell scripts

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...
19
votes
7answers
2k views

What purpose does the colon builtin serve?

I've hacked on a lot of shell scripts, and sometimes the simplest things baffle me. Today I ran across a script that made extensive use of the : (colon) bash builtin. The documenation seems simple ...
1
vote
2answers
465 views

Proper way to use shebang for bash

What is the recommended options in bash's shebang? #!/bin/bash -e I currently have it as above. This sources $HOME/.bashrc. I don't want to source user's .bashrc/.profile as it might interfere ...
2
votes
1answer
586 views

What's wrong with this csh file?

I don't have any experience writing .sh files. I want to run the pvm2raw utility of this app from the Volume Library. I get the following error when running build.sh in Ubuntu 10.10. build.sh is ...
4
votes
4answers
2k views

How can I pass a command line argument into a shell script?

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or ...
0
votes
3answers
8k views

How do I redirect command output to a file?

I have a command that I run in a folder that outputs MD5 hashes and filenames on the terminal: ls |sort -nr | xargs md5sum I need this output in a text file that I can download and compare to ...
3
votes
2answers
370 views

need shell script to grock a csv

Need some pointing in right direction on script to fetch and regex or sed. Site24x7 provides a URL with a CSV list of their source IP's used for monitoring. (they also provide other formats, CSV ...
3
votes
4answers
693 views

command for counting number of active cron jobs in crontabs

Problem statement I have 5 solaris boxes, some have Solaris 10 and some have Solaris 9. All of them have many cronjobs in their crontabs. I would like to know number of active cronjobs available, so ...
10
votes
2answers
208 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 ...
5
votes
3answers
898 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
1answer
2k views

“echo 1 > /sys/block/sdX/device/delete” on all disks except predetermined list

I have a list of scsi disks that I need to remove. The list is considered random at best and changes from time to time. I want to remove everything except a predefined list that I have created. Let's ...
2
votes
2answers
986 views

Invert results for “for in * do”-loops [duplicate]

Possible Duplicate: How do I reverse a for loop? Is there an easy way to invert a for in loop? For a script I want to process files in their reverse order. So instead of: for file in ...
146
votes
10answers
15k views

In Bash, when to alias, when to script, and when to write a function?

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just ...
0
votes
1answer
511 views

How do you wrap executable commands so that they work in an alias or function?

I'm still getting around the quirks of shell scripting, but I feel like I'm getting a little more comfortable with it. I've been trying to create an alias to generate a tar file that I have to create ...
3
votes
0answers
477 views

How to improve this backup script? [closed]

I wrote this backup script. I run rsync, tar and a dump of mysql. I'd like to add some features like email alert or some kind of statistic. Here is the script: #!/bin/sh DATE=`date +%D-%T` ...
8
votes
3answers
6k 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. ...
0
votes
3answers
205 views

help with script; if status is this do that if not do something else

I need to write a script that checks on the status of something. If the status "Copying" then exit (or retry in 60 minutes if we want to get fancy.) If all there is nothing returned (based on my ...
2
votes
2answers
295 views

Manipulating and saving multiple files

I have ~2k files that I want to extract 12 of 26 columns and save the extracted information into a new file. I have been doing this for individual files using cat "oldfile.csv" | grep -v "=" | cut ...
2
votes
2answers
1k views

How can I retain the console input in mplayer when reading from stdin?

I'm playing around with the command line interface of mplayer. I'd like to script it in the following way find /some/path/ -type f | grep -vif blacklist | mplayer -shuffle -playlist - where ...
10
votes
3answers
2k views

Fix or alternative for mktemp in OS X

I'm looking at a bash script someone else wrote that uses mktemp: TEMP=`mktemp --directory` However, this line does not work on my machine (OS X 10.6). How would I fix this line so that it is ...
4
votes
3answers
4k views

Get lines with maximum values in the column using awk, uniq and sort

I have a file with next format 2011-12-01 user1 access1 2011-12-01 user1 access2 2011-12-01 user2 access2 2011-12-01 user4 access2 2011-12-02 user1 access1 2012-01-01 user3 access1 2012-01-01 user4 ...
15
votes
3answers
3k views

How to properly collect an array of lines in zsh

I thought the following would group the output of my_command in an array of lines: IFS='\n' array_of_lines=$(my_command); so that $array_of_lines[1] would refer to the first line in the output of ...
68
votes
5answers
12k views

Why is it better to use “#!/usr/bin/env NAME” instead of “#!/path/to/NAME” as my shebang?

I notice that some scripts which I have acquired from others have the shebang #!/path/to/NAME while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME. Both seem to work ...
3
votes
3answers
397 views

How to make svn look for if there is an update once a minute and if there is call some script?

I am developing a server app. I have a remote machine with Debian that I use for testing. Currently I update the server manually when each new svn version comes out. It is just a testing server and ...
5
votes
3answers
1k views

Keep running a script via ssh

ssh can use to run remote commands. ssh me@server.com 'long-script.sh' I run a long script that will take a lot of time, but I want to close my computer and keep running the script in the remote ...