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

learn more… | top users | synonyms (2)

1
vote
2answers
2k views

sh + how to use array in sh script in order to print all values in array

I want to use arrays in my sh script. My target is to create an array with the values a b c and print all values in the array. I succeeded to print each array, but I failed to print all values in ...
1
vote
2answers
269 views

Problems with basename in a loop

I am new at shell script programming and I'm trying to execute a software that reads a text and perform it's POS tagging. It requires an input and an output, as can be seen in the execute example: $ ...
1
vote
2answers
396 views

nested case - why must “;;” be put after possible commands and not directly behind “esac”?

I would like an explanation about the termination ;; in a nested case statement. Is it documented somewhere ? Why doesn't it work like this: #!/bin/ksh ... esac ;; ...
1
vote
4answers
300 views

Add/delete workstation ip to /etc/sysconfig/iptables and then run service iptables reload

I need a script to add a users workstation ip to /etc/sysconfig/iptables and then run a service iptables reload. I need the same type of script for removing this rule from iptables. I found the ...
1
vote
1answer
289 views

can't get hello world shell script to run in FreeBSD

Server is running FreeBSD 9.2. Using vim, I wrote the following script called hello: #!/bin/sh echo "hello world" Then I set it as executable: >chmod 755 hello Then I tried to run it from ...
1
vote
3answers
9k views

How can I run a “grep | grep” command as a string in a bash function?

I'm trying to build a command that pipes the results of one grep command to another grep command in a bash function. Ultimately, I want the command executed to look like this: grep -I -r FooBar ...
1
vote
2answers
726 views

How to move or copy list of files referenced in a csv file to a new folder

I'd like to write a shell script (OSX) which copies files listed in a csv file to a particular directory. I need to strip quotes from each line in the csv file. My attempt for i in $(cat myfile.csv) ...
1
vote
2answers
71 views

Splitting file using prefix

I need to split a file into 5 parts but the tricky part to append the numeric number in the prefix of the filename and not at the end. split -n 5 -d -a 2 "$file" "$file" Name of the file:- file = ...
1
vote
2answers
5k views

copy files from one path to another path in linux

I am new to linux. I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern: file-1.txt file-2.pdf file-3.ppt .... I created ...
1
vote
2answers
100 views

What's the difference between print and head?

I know head gives you the beginning section of a file. But in this case, I'm trying to go through my source control and find every file that has a shebang line. And I'm failing to get results when I ...
1
vote
1answer
188 views

How can I use a loop inside a command?

I'm writing a shell script that contains a command that takes multiple directories as space-separated input arguments (like ls). I would like to fill in those arguments from a variable but don't know ...
1
vote
2answers
224 views

Creating a shell script that parses and redirects number into next command?

I am trying to create a script that kills an Xorg process. Here is an example of me running the commands in a terminal: First, I find the process id of the Xorg program (in this case, 2494): . ...
1
vote
1answer
393 views

removing data from txt file

I have a comma-delimited giant file that was exported from a database. The file has this format Date,n1,n2,n3,n4,n5,n6,color,type,reference 10-Jan-2000,344,13,21,2,55,3,"rose garden" ,3444,32 ...
1
vote
2answers
107 views

Mailing out a customised content based on specific search in a logfile

Using below test.sh, I am getting result in dump.txt as below: test.sh: #!/bin/bash #"Maxl-script" zone starts..... essmsh -l admin password -s localhost -i << EOF spool on to 'dump.txt'; ...
1
vote
3answers
185 views

apply specific command for each git repository

I want compress all the git repositories on my computer (say in ~). I.e., for each directory {}, that contains directory or file (in case of submodules) named .git, I want to execute git gc ...
1
vote
2answers
8k views

Concatenating string variable inside a for loop in the bash shell [closed]

#!/bin/bash names= find /home/devuser -name 'BI*' echo $names for name in {names[@]} do echo $name $var = $var$name done echo $var
1
vote
1answer
3k views

How to use command line arguments in a shell script

I have a shell script and when invoking it ./test it asks for an input.I 've seen a quicker way just by writing at once ./test myInput ,how is that achievable?
1
vote
3answers
205 views

Filtering a result from a command

I am having trouble writing a little program; when I run the following command from my program: gsettings get org.gnome.desktop.background picture-uri I get: ...
1
vote
3answers
198 views

Extracting mail addresses

I have a file that contains lines like this: user=<xxxxxxx@asd.com.tr>, user=<yyyyyyy@asd.com.tr>, user=<zzzzzzz@asd.com.tr>, user=<ccccccc@asd.com.tr>, ...
1
vote
1answer
164 views

Parsing the headers of sequence file

I have a multiple sequence file as >abc|d017961 sequence1...... >cdf|rhtdm9 sequence2...... >ijm|smthr12 sequence3...... >abc|d011wejr sequence4...... >stg|eethwe77 sequence5...... ...
1
vote
2answers
732 views

Improve sed command to replace first instance of character and all following characters?

Input: <e1 name="file1" id="id1" anotherId="id2"> Desired output: file1 I can get what I need with this: echo '<e1 name="file1" id="id1" anotherId="id2">' | sed ...
1
vote
2answers
583 views

Shell: Break both if and for loop at once

Shell: Break both if and for loop at once. My script: ip to name resolve code IP=192.168.27.191 hostNameChecker() { if [ `getent hosts $1 | wc -l` -ne 0 ];then HOST_NAME=`hostname` ...
1
vote
1answer
244 views

Is there a way to see which process invoked a shell script?

Is there a possibility so see from some unix log file what process have invoked a bash/ksh shell script or is this shell script invoked manually from command line? So is there some log file which ...
1
vote
3answers
1k views

Renaming or making filenames unique

I have written a plsql procedure using utl file to create files in the temp directory in Unix. I am sending the names of the files to a shell script to mail these files and delete after mailing from ...
1
vote
1answer
529 views

Why do I get different outputs when running my shellscript manually from when I run it with cron? [duplicate]

I have a fairly simple shell script (foo.sh) that looks like this: #!/bin/bash echo -n "$USER,$(date)," `binary -flags | sed -e '1,30d'`; exit 0; This script is supposed to prepare some output ...
1
vote
3answers
1k views

Setting file permissions in bash script

This script is not working the way I thought it would .I though it would find all the scripts that have every one rwx permissions changed to the permissions of xx5 #!/bin/bash # the / makes find ...
1
vote
2answers
119 views

Shell script execution

I have two shell script files with exactly the same permissions etc. I can run one of them by just giving its name in the command line but for the other one I should use sh or ./ to run it. What is ...
1
vote
1answer
360 views

What does mean ` AWK=@AWK@` in shell scripting

I found AWK=@AWK@ while reading sh script, it's something like AWK=$(which awk), but it's not working. So can anyone explain this ? Script : #!/bin/sh - # makelist.sh: Automatically generate header ...
1
vote
2answers
328 views

Merge chunks from multiple files

I need to merge chunks from a few but large files. Each line contains the file name and byte offsets. # file begin end foo/a 11970 12010 foo/a 22995 23035 foo/b 45090 45130 foo/b ...
1
vote
3answers
96 views

How can i check for existence of color in my linux box?

I want to do something like this in my linux box: if [[ colors exist ]];then echo "some text in colors" fi my doubt is : i am using sed to put colors in log file for some text but if the linux box ...
1
vote
1answer
169 views

Copy files excluding x,y,z causing error in shell script

I'm not well versed in shell scripting but I imagine this to be a painfully easy fix but its eluding me. I need to copy the contents of a directory excluding a number of files. I have a command that ...
1
vote
2answers
821 views

Ignore all matches to negated extglob pattern in recursive ls

I would like to recursively list all files in a directory which match a given input pattern using ls. (Oh, and this is in Bash). The only way I can think of to do this using ls is to shopt -s extglob ...
1
vote
2answers
2k views

How to remove element from the list?

I have written a command that output a list of hostnames, e.g. : clab148node11 clab148node12 clab148node15 clab148node16 Now, I would like to remove those nodes that do not respond to ping. For ...
1
vote
3answers
145 views

How to make a script for run apps for 1 second?

I have the following bash script for running some apps in a folder for 1 second each (for testing): #!/bin/bash for app in ./*.app do myrunner $app & sleep 1 killall -9 myrunner done ...
1
vote
2answers
956 views

bash scripting - Any function to escape character?

I am trying to automate some of my stuff and facing a issue, the problem boils down to escaping the value that is being passed as a argument to the script. myScript.sh #!/bin/bash loadPatch -name ...
1
vote
2answers
387 views

Easiest way to find/replace in a file using a list?

I have a file, foo.txt, and a regexp I want to find in that file. Each time I find the regexp, I want to take a line from another file, bar.txt, and substitute it in for the regexp match I found in ...
1
vote
2answers
764 views

Print/Tee to console without passing output to pipe

Is there a way to print or tee one thing to the console and still pass something else through to the next pipe? Something like: echo dog | printOrWhatnot "PUTTING MY THING DOWN" | sed 's/dog/cat/g' | ...
1
vote
1answer
689 views

How to create script that toggles one value in synclient?

I use synclient touchpadoff=1 to disable touchpad and synclient touchpadoff=0 to enable touchpad. $synclient | grep TouchpadOff TouchpadOff = 1 I'd like to create script that will ...
1
vote
3answers
3k views

How to rename files using sed?

This is an sed expression that was created by a contributor here. More specifically by @craig-sanders. $FileToMove is set to "2012-09-01 Home insurance quote_zz20.pdf" # use sed to extract folder ...
1
vote
2answers
3k views

How to create a md5-file for every folder in a drive recursivley?

I'm searching for a script which creates a md5 chechsum file for every single folder inside drive recursivley? I do have a copy of md5deep on my machine but I'm not that good in scripting bash.
1
vote
2answers
411 views

Remove oldest folders + files (50GB) from a folder when share/HD reaches 95% full capacity

I have a big share (~5TB) that is getting full. Now I want to make a script that deletes data from 2 specified folders. But this need to be the oldest files/folders and it need to stop when ~50GB has ...
1
vote
2answers
209 views

How do I create a bunch of files with different parameters in the body of each file?

So, say, I want to create a bunch of files named this way (where I have an original file "albedo10solar100" with text inside), where I vary albedo from 10 to 60 in steps of 10 and solar from 90 to 100 ...
1
vote
1answer
2k views

Trying to create a cron to Rsync then tar the folder

I'm stuck at the first hurdle! I want to run a couple of commands from a bash script. First something like this to rsync some directories: rsync -e ssh -az user-whatever@website.com:/home ...
1
vote
2answers
1k views

How to change user with authentication inside shell script

It is possible to change the user within a shell script by su - newuser; but what if the newuser needs authentication. How can I provide the password within the shell script? None of the users is ...
1
vote
1answer
285 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
3answers
454 views

Returning a portion of a line matching a pattern

I have a file (file_name) which contains exactly one occurrance of the string "Result: " at the start of a line. I want to print all the characters after the string "Result: " in that line until I ...
1
vote
3answers
822 views

To list out duplicate filenames in a listing?

bash-3.00$ cat f.txt -rw-r--r-- 1 mukesh other 102 Nov 5 18:32 f1.txt -rw-r--r-- 1 mukesh other 19 Nov 5 18:32 f2.txt -rw-r--r-- 1 mukesh other 204 Nov 5 18:32 ...
1
vote
3answers
479 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 ...
1
vote
1answer
10k 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): ...
1
vote
2answers
846 views

writing a command transcript to file

Here is a script from Mercurial issue 2743. Call it mq.sh. I'm trying to get it to do the following things, on Debian squeeze. Echo commands + standard output + standard error to a file. Prefix with ...