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

learn more… | top users | synonyms (2)

4
votes
1answer
56 views

How to remove a positional parameter from $@

Basically, I want to "pluck out" the first occurrence of -inf from the parameter list. (The remaining parameters will be passed along to a different command.) The script I have has the following ...
1
vote
1answer
47 views

Implement a shell in a bash script [closed]

I am trying to emulate a shell in a bash script, like this: cmd_shell() { while true; do read -e -p 'pass> ' command run_cmd $command done } It works ok, and the option ...
-1
votes
1answer
23 views

script for automount, using if statement. cannot got the result [duplicate]

This script should auto mount a file system on a Linux server. testcheck=`df -h | awk '{print $6}' | grep "/test"`; if [$tescheck -ne "/test"] then mount /test else echo "failed"; fi I'm ...
2
votes
0answers
22 views

How to properly use $_ inside a tcsh script sourced from another tcsh script?

I try to retrieve the result from $_ inside a tcsh script itself sourced by another tcsh script (I'll refer to them respectively as the inner and outer script). But $_ is not set with the value I ...
0
votes
0answers
15 views

Setting anacron on mac

I set cron to run script every day using env EDITOR=nano crontab -e where I wrote something like this @daily path/to/sript.script Now I want to use anacron to be sure that script is going to be run ...
0
votes
1answer
33 views

Simple if/else statement not working [duplicate]

I have been trying for a long time to get this basic bit of code to work in a shell script, but to no avail, the code doesn't work! read dec if [ dec="Y"]||[dec="Y"]||[dec="y" ]; then let repeat=1; ...
0
votes
0answers
10 views

Jinja to populate shell script

I have written a shell script that creates datadog dashboards (ref. datadog monitoring tool) for the a cluster. I want to call these scripts in salt-stack state and pillar files to make the whole ...
2
votes
3answers
82 views

Patch a shell script prior to running

I have a shell script (arch-chroot from the Arch Linux arch-install-scripts package) that handles the mounting and unmounting of devices (with error checking) prior to starting a chroot. It starts the ...
-1
votes
0answers
26 views

dump non formated output of commands to csv file

I have some commands with which i can get list of VMs running on hypervisors. I redirect that output to some file.txt and use while loop to read each line from file.txt and execute two more commands ...
2
votes
1answer
34 views

Display integral in scientific notation?

I have an integral value: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 2400000 I assign it to a shell variable: CPU_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) ...
1
vote
2answers
37 views

Recover from faking /proc/meminfo

So, without really thinking too much, I ran this script: #!/bin/bash SWAP="${1:-512}" NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0" sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo ...
0
votes
1answer
17 views

get command line agrument by variable in shell script

I want to get the arg of a command from a var that has the index in it of the arg that I want. Something like this # command in terminal, `foo -r -f value_wanted` index="3" var=$"$index" echo $var ...
-1
votes
1answer
36 views

Replace duplicate rows by one column [duplicate]

I have a file in the below format. [ I tried using awk and sed in all the possible ways which i am aware of but couldn't find a way to reformat the file in the format which i am looking for. I would ...
0
votes
3answers
82 views

Need some help on grep command on shell script

I have a log file and I am trying to write a shell script to find multiple strings in a file on multiple lines. For example if I see the phrases ...
3
votes
2answers
71 views

Why is #!/usr/bin/env bash not working on my system?

I ran into some issues when running some installation scripts where they complained of bad interpreter. So I made a trivial example but I can't figure out what the problem is, see below. ...
1
vote
3answers
31 views

Print array line by line in a here doc

I currently have the following here doc message=`cat <<-EOF Test data: Test data 1: "${Test_data_array[@]}" EOF` echo "$message" Is there a way to get the array to display multi-line per ...
0
votes
1answer
49 views

How can I add all my groups to a different user in bash?

I've got a user named foo. I'd like foo to have all the same group memberships that I do. I thought I was close, with this: sudo usermod -a -G $(id -Gn | tr " " ",") node4 The id piped to tr is ...
0
votes
0answers
31 views

Git “unknown revision” error when within bash script

I'm writing a shell script that needs to perform certain operations based on the last commit's message of my local branches with a name starting with "task". Here's what my script looks like ...
4
votes
4answers
262 views

files greater than a average size of directory?

how could I find all the files and directories with size larger than or equal to the average file size of the current directory?
3
votes
2answers
57 views

How does the -d option to bash read work?

I have a bash script where I'm trying to assign a heredoc string to a variable using read, and it only works if I use read with the -d '' option, i.e. read -d '' <variable> script block ...
1
vote
1answer
41 views

SIGINT handling differences between bash 3 and 4

I have a script which works great in bash 4.3, but gives me unexpected behavior with bash 3.2. Here's a simplified version: set -o errexit -o pipefail task() { local name=${1} local ...
0
votes
2answers
37 views

How to merge files with same names

I want to know how to merge files with same names For example I have following files in one folder: 1081713FHN-NA.one_.txt 1081713FHN-NA.second_.txt 1081713UEN-NA.one_.txt 1081713UEN-NA.second_.txt ...
1
vote
2answers
69 views

BASH how to save output of rm with timestamp?

I nearly have this working, This is a script that will run in CRON to keep the latest 10 backups, and remove anything else, I want to log this to keep track to make sure all is in good order, I have ...
0
votes
1answer
51 views

Symlink to file which does not exist yet

I'm currently trying to cleanup my home directory by moving files like .vimrc, .bash_profile, etc. to a directory .dotfiles in my home directory. The idea is to use symbolic links to these files ...
0
votes
0answers
74 views

How to redirect linux command outputs to specific row and column in csv?

I'm working on Unix Build post documentation process, I have a CSV file to be filled in and uploaded for every new build, to ensure the servers are build as per the original request. The CSV file ...
1
vote
1answer
19 views

writing output of xbacklight -get to a variable in a bash script

I have a little script that lets me set the display brightness #!/bin/sh VALUE=$(zenity --scale --text="Select Display brightness" --value=50 --title "Helligkeit anpassen"); xbacklight -set $VALUE; ...
0
votes
2answers
36 views

Input redirection using command and options

I am trying to write a script that takes in 2 parameters - program name and optional flags. I am trying to execute the program command on an input_file and store the output in the output_file. ...
0
votes
2answers
63 views

Grep / awk on multiple files to single output

I have several txt-files containing data, where i use grep to search for a current string of text, and use awk to filter out the variable i need. The string is repeated through the file, so i ...
3
votes
1answer
28 views

Head Script output explanation

So this shell script: #!/bin/bash head >/dev/null; head; almost always gives the same output when called with sequential numbers (e.g. seq 10000 | ./sscript) OUPUT: //blank line 1861 1862 ...
3
votes
2answers
39 views

How can I get rid of all script blocks in html files? [duplicate]

How can I remove all script blocks (including multi-line ones) from html files such as: <script type="text/javascript"> var googletag = googletag || {}; googletag.cmd = googletag.cmd || []; ...
2
votes
1answer
22 views

Repeat based off user input OS X

I am creating a script which asks a user to enter a message then how many times they would like to display that message. This is what I have so far. message.sh #!/bin/bash echo -en "Enter a ...
2
votes
3answers
63 views

log out via shell script

I am doing some operation via shell script. If that script is successful, continue else logout of the current user. I am unable to find a way to logout of current user. I tried to use exit but seems ...
3
votes
2answers
44 views

confirmation before sudo su

most of the person in my team have sudo access. But I still want them to confirm before they do sudo su. if user press Y, then allow him as root else return him to his own home directory. I tried the ...
2
votes
2answers
36 views

Find commands/function whose name matches a value

I am looking for some command e.g. match ls which should match commands like ls, alsa asls,.. and return them. I preferably want it to cover both all commands and defined functions. Is there a builtin ...
0
votes
0answers
51 views

Shell scripts for proof reading

I am using below mentioned script present at link to check my writing. But, while running the scripts some syntax errors are shown up. I am not able to correct the same. Haroons-MacBook:democheck ...
0
votes
1answer
27 views

How to stop an infinite loop function called with myFunc &

In my bash script, I have a function that does one certain task in an infinite loop. To allow for the next part of my code to run at the same time, I run it with myFunc &. How can I kill this ...
0
votes
1answer
19 views

Bash script hangs on specific input

I am trying to write a simple script which does something if the first parameter of the input is equal to the -r string. But whenever I try to run this file, the program hangs and I have to terminate ...
9
votes
9answers
305 views

How to get the last argument to a /bin/sh function

What's a better way to implement print_last_arg? #!/bin/sh print_last_arg () { eval "echo \${$#}" # this hurts } print_last_arg foo bar baz # baz (If this were, say, #!/usr/bin/zsh instead ...
1
vote
1answer
52 views

get first CLI argument after the options in shell scipt

I have a script I class like so gitploy up -t 2.0.0 test_repo. I pull out the "action" up right away, then I need to be able to get the test_repo before I process the options. I don't want to lose ...
1
vote
1answer
24 views

Breaking out of a loop, return to start of loop with correct value of variable

I am sending processes to a server, but I only want a certain number running at a time. Thus I loop which checks how many jobs are running and only submits new one if the current amount of jobs do not ...
0
votes
1answer
21 views

Trying to find the index of an entry in a list [duplicate]

I am trying to find the index of an list entry using this simple method: #!/bin/sh partList="500 1000 2000 4000" a=( $partList ) echo ${a["500"]} this returns Syntax error: "(" unexpected. And the ...
-1
votes
2answers
26 views

Script not running! Also how to attach timestamp in mail?

I have the following bash script... #!/bin/bash # Delete JUNK temp folder if [ -d JUNK/FOLDER/PATH ] then rm -rf JUNK/FOLDER/PATH echo "Cleansed JUNK directory successfully!" /usr/bin/mail -s ...
0
votes
1answer
41 views

set date time from user input in shell

I am creating script to check RTC working & for that first I am getting the date & time from user input in yyyy mm dd hh:mm:ss format. read -p "Enter the date:" val echo $val date -s "${val}" ...
1
vote
1answer
42 views

Why do I get the message set no match when I run this script?

#! /usr/bin/tcsh -f set ps_output = "`ps -u $user`" @ i = 2 echo "$ps_output" set ps_test while ( $i <= $#ps_output ) set line = ( $ps_output[$i] ) if ( $line[4] != "ps" && ...
0
votes
2answers
31 views

How to assign a multiple line string value to a variable with indentation in a shell script?

I want to assign the following multiple line string value to a variable in a shell script with the the exact indentations and lines. Usage: ServiceAccountName LogFile Where: ServiceAccountName ...
0
votes
1answer
31 views

What are conditions I can check for detecting error in downloading using bash?

I want to continuously download a link until it successfully downloads, #!/bin/bash while [ some_condition ]; do curl my_url # or vagrant add my_vagrant_url ...
0
votes
0answers
13 views

How to generate total size of files from each unique source in bash

I have a large number of directories which each contain files with the format unimportant_data_here_source_date.gz I want to calculate how much data came from each source. That means figuring out ...
1
vote
1answer
25 views

How to programatically send break signal (Magic SysRq) through serial terminal

I have following setup - I have 2 computers connected by serial connection (null modem) emulating vt220. I'd like to send break signal (Magic SysRq) from shell script - is it possible without writing ...
1
vote
0answers
34 views

Bold and Underline using echo with email

I am using a script that emails me a particular status report. The script sends me the contents of several files. I am using echo to provide a heading in the email that is used to differentiate ...
2
votes
1answer
71 views

How to get filenames when using find and sed

I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified. This is how I am finding and then using sed: find . -type f -a ...