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

learn more… | top users | synonyms (2)

1
vote
0answers
28 views

Selecting the columns from source file based on control file [duplicate]

I have requirement where I need to construct a file based on control file. The first row of my source file contains column names. The control file contains the columns which I need to output into a ...
2
votes
6answers
339 views

Extracting IP address from a text and store it in a variable

I have a text file named abd shown below. 48878 128.206.6.136 34782 128.206.6.137 12817 23.234.22.106 I want to extract only IP address from the text and store it in a variable and use for other ...
2
votes
1answer
55 views

How to enter a sum derived from awk in the middle of text output [closed]

I am trying to print a sum in the middle of a line. My script prompts for a user name and then pulls a total number of processes and a total of time for that user from a file. I want to print a ...
3
votes
2answers
78 views

Shebang line for “run with $SHELL”

I'm looking for what to put on my_zsh_script.sh's "shebang line" that would have the same effect, portably, as $SHELL my_zsh_script.sh IOW, I looking for the valid equivalent of #!$SHELL or ...
1
vote
1answer
65 views

Substituting default value ('0') for null parameter

I have a query that returns some numbers from the db and I'm trying to put that into a file to send out. However, some of the values come back as null and I'm trying to substitute '0' in for some ...
2
votes
1answer
89 views

How to delete core.* files while the program is running?

I launch my program from .sh file (for simplicity let's say it's content is just ./myprogram). For some unknown reason the program generates large core.* files. What should I add in my .sh file so ...
2
votes
3answers
137 views

Source only part of a script from another script?

This would probably never be the BEST approach to something, but I'm wondering if it's even possible. Something like: awk '/function_i_want_to_call/,/^$/{print}' script_containing_function | xargs ...
1
vote
1answer
75 views

Linux shell script to reboot a few linux machines

I am task with something I have never done before. I am sure is easy for some of you :) I need to execute a linux shell script from our network that will connect ssh to this devices and reboot them ...
2
votes
1answer
67 views

Datalogger with PI

I am building a datalogger with raspberry pi. I have used a Shell script in rc.local, I am capturing the Bytes using tcpdump. The Problem am facing is that the log file is getting replaced each and ...
2
votes
2answers
176 views

grep -E-f to capture matching ip-addresses in two files, and , print or echo result of each iteration, xx.xx.xx.xx found or xx.xx.xx.xx not found

Having one short list of few hundred ip addresses in file1, and want to identify if any of these ip addresses are missing from the master list, file2. Looking for an output > redirect to a file that ...
3
votes
2answers
90 views

Bash Scripting: How do i limit the amount of options in a select loop?

When you use the select loop to present the elements of an array as an option, it will display it all at one time. Is there a way to present the options to the user for like 25 options at a time?
2
votes
0answers
20 views

On using $hash_array[$(global_alias)]

Identical code works on one system, fails on another: % zsh --version zsh 5.0.7 (x86_64-pc-linux-gnu) % typeset -A frobozz % alias -g foo='echo xyz' % frobozz[$(foo)]=9 % echo ${(kv)frobozz} xyz 9 ...
4
votes
2answers
152 views

Test for existence of multiple files, given by pipe

I have a command that gives me a list of files, one on each line. Filenames are "normal" - no spaces, no need to escape parentheses etc. Now I want to pipe that command to something like test -f and ...
3
votes
4answers
155 views

Is it possible to run nohup and still see verbose output on screen

I have a fairly simple shell script patch file (in essence, some scripting plus rpms all in a single file). It works great but I encountered something I wasn't expecting: a user accidentally closed ...
1
vote
1answer
52 views

How to create shell script to handle commands like ls -l /somedir

My script so far can handle the ls command and returns the files in the directory. Now i am trying to make it handle commands like ls -l /tmp . Any help? My script so far: #!/bin/bash #set -x clear ...
2
votes
2answers
38 views

Forcing the expansion of a global alias in part of a word

I have a couple of scripts /tmp/foo/bar.sh and /tmp/foo/baz.sh, the look like this: # /tmp/foo/bar.sh alias -g __FILE__='${(%):-%x}' alias -g __DIR__='${${(%):-%x}%/*}' printf "sourcing %s\n" ...
3
votes
2answers
65 views

How to keep exactly one file in a directory?

I have recovered files and currently they have this structure: root/MD5_of_file1/file1 root/MD5_of_file2/file2 ... root/MD5_of_filen/filen Obviously, duplicates are now in the same folder. The ...
0
votes
3answers
57 views

Bash script to remove the oldest file from from a folder

I want to make a bash script to delete the older file form a folder. Every time when I run the script will be deleted only one file, the older one. Can you help me with this? Thanks
1
vote
1answer
51 views

How to add change directory and git pull to this crontab line?

I have this directory structure: /var/sync/ ├── sync_bi ├── sync_pfizer └── sync_sandbox I have a few files running by the following cronjob: 10 */12 * * * find /var/sync/ -name 'Replicator.php' ...
2
votes
0answers
45 views

How to avoid namespace collisions/pollution in a script meant to be sourced?

I want to implement a script foo.sh that culminates in the an execution of a command bar (with some arguments). The command bar modifies the current shell environment, which means that foo.sh must be ...
1
vote
4answers
62 views

How do I get to the top or bottom of a really long script quickly? [closed]

I'm writing an extremely long bash script and scrolling up and down throughout the entire thing is extremely tedious and a major hassle. I'm new to linux and any help would be appreciated.
3
votes
3answers
177 views

Find and replace with contents of a file

I have a file that begins with a few "include" statements, like this: include foo.xyz include bar.xyz do c do d Suppose the file foo.xyz contains do a and the file bar.xyz contains do b. How can I ...
1
vote
2answers
271 views

notepad++ adds \r to shell scripts [closed]

I noticed notepad++ adds characters when writing bash scripts. For example a simple wait script written in notepad++ it adds \r breaking the script. The standard windows notepad does not have this ...
-1
votes
1answer
38 views

extracting parts of a file to different file with sed

I am new at shell script. I want to write a script to extract parts of my data file and save it in different files. I wrote a script but it gives some error. The script is #!/bin/bash nn=0 mn=0 for ...
1
vote
1answer
89 views

set -A command not running in Linux

if [ $UNITS = day ]; then while ((OFFSET > 0)); do if (( OFFSET >= day )) ;then month=$((month - 1)) if (( month == 0 )) ;then year=$((year - 1)) ...
-1
votes
2answers
192 views

Nested while loop in bash [closed]

In a txt file, I have a list of sites that I'd like to visit. Each of them provides a list of pages on the particular site to be visited (when called without page parameter) via list.php. The problem ...
6
votes
2answers
859 views

Count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable)

Brief statement of the question: Is there built-in bash method to count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable), without resorting to ...
0
votes
1answer
186 views

How to pass output to the xargs command by using piping and update user's plan after running the finger command [closed]

I'm a new user on Ubuntu and working on a task, but I haven't found any way to resolve this problem. Here is the statement: finger takes the user account name as the argument. You can feed the ...
2
votes
1answer
30 views

Portable way to customize executable called by script

I have a script that I need to instrument. Suppose it calls a foo program. I can define foo function and export it to sub-shell, thus providing instrumented foo call. This works only if script ...
11
votes
6answers
1k views

Using “reserved” codes for exit status of shell scripts

I recently came across this list of Exit Codes With Special Meanings from the Advanced Bash-Scripting Guide. They refer to these codes as being reserved and recommend that: According to the above ...
3
votes
2answers
260 views

Run commands in parallel and wait for one group of commands to finish before starting the next

I have script including multiple commands. How can I group commands to run together ( I want to make several groups of commands. Within each group, the commands should run in parallel (at the same ...
2
votes
2answers
97 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
3
votes
3answers
395 views

How do I write a script to count the total number of files, and directories in my home directory then display it?

Question is in the title been trying to do this. I want to do it with a for loop. This is what I've come up with so far which does not work. for home in /home/ {.,/}*; do echo "$home"; done I would ...
0
votes
2answers
64 views

How to redirect the standard input of the `while` instruction on a file?

I'm learning about shell scripting and I have some difficulties understanding the development of the main command structures needed to write a script, especially with the while,do and done commands. ...
0
votes
1answer
59 views

how to save data in terminal as a spreadsheet file

I am a beginner to Linux. I am using this command bzgrep "1675391601497" ts3events.csv.bz2 | less which pulls up a bunch of data separated by commas. I would like to save them as a spreadsheet. what ...
0
votes
1answer
70 views

grep a variable within an if statement

Maybe I am approaching this the wrong way, so I am open for suggestions. I am trying to avoid adding the output of INSTALLED_LIST to a file. I wish to use it within a variable. Basically, I want to ...
0
votes
1answer
147 views

Init Scripts: Do I Need a PID File?

I am new to the the world of init scripts and still a BASH neophyte. I'm currently in a situation where I could use some guidance. We run an application that utilizes JBOSS and I need to be able to ...
1
vote
1answer
92 views

How to change password using script

#OUTFILE=out.xml prefix='EDSAD150' password="asdf1234" pass=$(perl -e 'print crypt($ARGV[0], "asdf1234")' $password) for i in {01..02};do usname=$prefix""$i yes $pass | yppasswd $usname ...
0
votes
2answers
53 views

Set yesterday Date in script special for Monday

I am writing a script a check whether exist a backup file every morning Monday to Friday. These backup file is saved in the end of everyday from Monday to Friday only named as 02_10_15 There is ...
3
votes
2answers
96 views

Compare files and rename if contents differ

I need to create a code that will: Check for two arguments at the start minus the script name itself (i.e dar /mnt/sdb1 /root/testdir/testarc with $1 = /mnt/sdb1 and $2 = /root/testdir/testarc) Copy ...
0
votes
4answers
55 views

how to use strings in a file as a variable

I have a conf file content like below. file.conf: key name value pswd Now I want write a script to have the two variable as above script.sh key="name" #key as variable value="pswd" ...
1
vote
4answers
86 views

I need to make a file undeleteable, but allow it to be modifiable and renameable

I have a script that checks for the presence of a file, and if it exists, read a list of email addresses from it to send notifications. When I don't need to be notified, for instance, when I'm at work ...
4
votes
4answers
520 views

can't figure out error in shell script unexpected operator

$ sh backup-to-s3.sh backup-to-s3.sh: 11: [: bkup_20151106_150532.zip: unexpected operator backup-to-s3.sh: 11: [: bkup_20151106_150532.zip: unexpected operator backup-to-s3.sh: 11: [: ...
0
votes
1answer
37 views

Replace a value in a random position with specific with restriction [duplicate]

I need to replace randomly 10 % of a numbers in a column with other specific one. e.g. randomly replace 10% of any number with "1" except in first position in file1.txt 1 1234567890 2 0987654321 ...
1
vote
1answer
146 views

want to su without password inside shell script

I have written a script which will run a script for a backup before: # cat backup-before-shutdown #Scritp to take backup before shutdown #Author Ashish Karpe echo "Taking backup to s3" su ubuntu ...
1
vote
0answers
28 views

Nested parameter expansion [duplicate]

I know how to use ${parameter#word} Remove matching prefix pattern and ${parameter%word} Remove matching suffix pattern. but is there a way to nest them in a single call, such as ...
4
votes
4answers
435 views

How can I get this script to error exit based on result of for loop?

I have a bash script which uses set -o errexit so that on error the entire script exits at the point of failure. The script runs a curl command which sometimes fails to retrieve the intended file - ...
2
votes
1answer
75 views

sed regex for capture group between delimeters

I have a two line file that I'm trying to get some info out of for a bash script using sed. # File Comment PrefixForInformation {information to be captured} I need to get the information between ...
0
votes
4answers
38 views

How do i write a script to run only if another script has run more recently?

I am attempting to write two scripts. But i run into problems when i try to prevent either one of them from running twice in a row. For example, i have script1 and script2. i want to be able to run ...
3
votes
3answers
54 views

query r.e running for loop scripts in parallel

I have the following in a shell script: for file in $local_dir/myfile.log.*; do file_name=$(basename $file); server_name=$(echo $file_name | cut -f 3 -d '.'); ...