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

learn more… | top users | synonyms (2)

1
vote
1answer
52 views

insert the content between the matching pattern in shell script

I want to insert the content between the matching pattern in shell For example : file called input.txt : var a = [ ] file called output.txt : 1152 1185 1185 I want to insert the values ...
3
votes
2answers
165 views

what is $$ in bash? [duplicate]

there is a script I evolved with it, it has line of command like below : mytemp=`echo ${sourcedir}|awk -F/ '{printf "/%s/tmp",$2}'`/`basename $0`-$1.$$ at the last of the command we see $$ that ...
0
votes
1answer
44 views

when i used sed -n ,why this err about sed -n ' $j,4800p' [duplicate]

LINEnum=$(grep -nr "#32" IM_DlCtrlRef.txt | cut -d : -f 1) for j in $LINEnum do echo $j sed -n '$j,4800p' IM_DlCtrlRef.txt >> IM_DlCtrlRef_bak for insertl in {1..4} do ...
1
vote
2answers
44 views

Can I use a shebang to have a file source itself into current bash environment?

I have a growing collection of scripts which should be sourced, not run. At the moment they have the shebang #! /bin/cat but I would prefer the have them be sourced into bash when run, in the same ...
-6
votes
0answers
33 views

Write a Shell Script to generate a report named “Report_DDMMYYYY_HHMMSS.log” [closed]

Write a Shell Script to generate a report named "DD_MM_YYYY_HH_MM_SS.log"which performs the following operations: 1) ** Display the number of files present in the given directory (Directory name ...
0
votes
1answer
22 views

joint two conditions in bash script

Note: Update Jun 28 I have an iptables script .sh (bin/bash) with the rules: function run_ips(){ for ip in $(sed '/#.*/d' ips.txt); do if echo $ip | grep -q "-" >/dev/null; then iptables -A ...
0
votes
2answers
43 views

Skip catching errors in a shell script

I have a script which compile a program. This program first compiles the source code using configure && make commands, then runs some tests using make test. This script also uses set -e to ...
-1
votes
1answer
51 views

Syntax error at line 18 : `<<' is not matched

Here is my script #!/bin/bash stty intr ^C erase ^H kill ^U p_id=`ps -ef|grep iucmh|grep iuc|grep MSC015|grep START|awk -F" " '{print $2}'` vLogin=`echo "iucmh/iucmh123@4s_MH"` if [ -z "$p_id" ]; ...
2
votes
2answers
44 views

Command behavior is different when executed by an other user (via su -c)

I want to kill every processes who match a specific chain. Here is my script, which works pretty well: echo `ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print $2 }'` | xargs kill ...
2
votes
1answer
32 views

shell script echo text name in for looping multiple texts

I am trying to do this text1="word1 word2 word3" text2="word4 word5" text1="word6 word7 word8" for var in $text1 $text2 $text3 do echo $var" in "(__?__) done expected output word1 in text1 ...
0
votes
1answer
54 views

Using multiple sed commands for text manipulation

I have a bash file, in which I would like to make several changes. sed -i 's/ / /g' testfile sed -i -e 's/#[a-z]$//' -e '/^$/d' testfile sed -i 's/ *$//' testfile sed '/##=/ r header' ...
0
votes
2answers
47 views

How to add a column of numbers to a array data file

I have a data file with two columns of numbers: input file 4.182 4.1843 4.184 4.2648 4.2281 4.0819 4.2204 4.1676 4.0482 4.1683 4.0156 4.2895 4.4504 5.2369 4.3776 4.4979 4.3797 4.1372 4....
0
votes
1answer
26 views

How to multiply two columns in a file by a constant number

I have a file like this. input data 4.2394 4.4569 4.2427 4.1011 4.2879 4.1237 4.2106 4.4844 4.2373 4.1071 4.1322 4.0502 4.3103 4.4255 4.4342 4.5262 I need to multiply each element by a constant ...
0
votes
1answer
34 views

Using http remote MD5 hash

I would like to use the md5sums function of pkgbuild without keeping track of new hashsum at each update, and just change the pkgver variable to build a new version of my package. md5 are available ...
0
votes
3answers
77 views

How to do a loop to execute many files

I have a script which function like this for one file. ./script 0001g.log > output for two or more files, like this ./script 0001g.log 0002g.log 0003g.log > output The script take one ...
0
votes
1answer
26 views

Run script simulatneously with loading desktop

I am running Kali 2, and have a script that I want to run on startup. Right now, I have the script in my profile.d directory, but it takes too long to execute. The effect is that after I login, the ...
0
votes
1answer
61 views

cat specific line range from /etc/hosts to dialog box

I need help with dialog box in bash. I like to list, for example all Ethernet switches from /etc/hosts and show them inside dialog box. After that I would like to connect selected switch from the ...
3
votes
2answers
60 views

Can you tell if a file has been renamed?

Many tools out there for syncing files aren't quite right for syncing you're home directory. Because of this, and to learn Ruby, I've been working on my own sync tool. An issue I've encountered, ...
1
vote
2answers
42 views

Defer variable expansion to subshell

My service uses environment variables extensively for configuration (mostly because it's really easy to inject those into a Docker container). I also have some maintenance scripts that I can run ...
2
votes
2answers
63 views

How to refer to multiple files from a directory to one command

I have a command that needs to take multiple input files, which are originally from one directory and have certain file name patterns. For example: In directory /home/mydir/ I have files: A.dat B....
1
vote
0answers
24 views

Avoid starvation using flock with 2 process with write and read lock

I have two scripts let's call them script1 and script2. The script1 is scheduled every 2 minutes and it takes 4 minutes to execute the script2 is scheduled every 30 minutes and it takes 4 minutes to ...
2
votes
1answer
44 views

One of several file-append redirections of “python --version” is repeatably 'overlooked' by bash, but the others aren't [closed]

Why is bash consistently passing one, specific redirected output from python --version to the console rather than appending to file, while every other output is redirected properly? I am developing a ...
1
vote
0answers
26 views

Bash check if firefox use internet

I want to check how much internet use Firefox with a shell script. Here I found good examples to see performances. #!/bin/sh free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*...
0
votes
0answers
25 views

Shell syntax var=${var:-$var2/file} [duplicate]

I came across the following syntax while working with some shell scripts. I am unable to understand what it does and I am not sure what should I search to get right documentation for this var=${var:-$...
24
votes
1answer
2k views

If “bash <file>” works, why is “source <file>” throwing an error?

I have the following script: #!/bin/bash set -x if :; then echo a fi If I run bash /tmp/file, a is echoed, but if I run source /tmp/file, I get: bash: /tmp/test: line 6: syntax error: ...
1
vote
3answers
71 views

Extract filename from another filename

I receive notification files for different files. I want to extract the filename from these notification files which I would use in another process. The first 4 strings of the notification file will ...
0
votes
0answers
48 views

A script to remove and put menu on Xfce

I encountered a bug in Xfce that if I move an program icon on panel, the applications menu disappears. I found the solution that if I remove the menu and then put it back, the menu won't disappear ...
0
votes
1answer
74 views

Simple if command not working in csh script

I have made a basic shell script using IF command as a purpose to help myself as I am a beginner. Problem is that when I execute this script using source overspeed (after giving myself execute ...
1
vote
2answers
134 views

Remove leading whitespace before possible shebang

I have a large collection of non-binary files in one place. Some of them have shebangs and of those some have (for some inexplicably reasons) whitespaces in front of the shebangs. This includes empty ...
0
votes
1answer
184 views

Check if date is n years old

I have a variable $date with format yyyy-mm-dd and I would like to check if it is exactly 1 year, 2 years (and so on) old. That's what I tried so far: dates=$(echo $date | cut -c6-10) todays=$(date +...
0
votes
0answers
33 views

How to transfer a big file simultaneously to several machines remotely? [duplicate]

I have launched 10 instances on a cloud service, and these are the steps that I want to do in parallel (simultaneously): 1) Copy a big input text file to each machine 2) Run a java program called ...
2
votes
1answer
163 views

How do I get bash to give a relevant error for a file with windows line-endings?

Suppose I have the following shell script: #!/bin/bash \n echo "Hello World" \n Running it gives Hello world. Now suppose I have the following shell script #!/bin/bash \r\n echo "Hello world" \...
2
votes
1answer
202 views

Print recursively all directories and subdirectories

I am trying to print all directories and sub directories with a recursive function but I get only the first directory. Any help? counter(){ list=`ls $1` if [ -z "$(ls $1)" ] then ...
-4
votes
0answers
22 views

shell scrit for posting the json file to curl [duplicate]

i have multiple json files which need to be post to curl one after the other. so am using loop in shell scrip which is not working for me here. can some one help me the write code to do so ??? here ...
1
vote
1answer
49 views

Can I check if create_ap works?

I am currently using the script create_ap to emit a Wifi Access Point (WAP) using my Raspberry Pi, which is running Raspbian. How can I check if the AP is up and running using a bash script? For your ...
-1
votes
1answer
47 views

How can I pass the file names to xargs and truncate them? [closed]

I have to write a script which will ssh into servers and truncate log files My script is #!/bin/bash for server in $(cat c10_servers.txt) do ssh $server " ls /var/log/hello grep -i hello.log. |...
1
vote
4answers
75 views

script to delete files if there is no corresponding file with the same name but a different extension in another subdirectory

I have a series of preview images and RAW files in a directory structure (shown below), I quickly review the preview images, and delete those that I don't want. The RAW files are in a RAW ...
3
votes
1answer
34 views

Capture output (sdout/stderr) of SSH port forwarder

I would like to store the error below in a variable: ssh -M -S /invalid/path/forwarder.socket -fnNT -Llocalhost:6301:remote_server:22 proxy_server muxserver_listen bind(): No such file or directory ...
1
vote
1answer
52 views

Make an IF condition out of result we get from remote server

I need to check for files availability in remote server, and based on the files availability i need to make an if condition. I have tried the below commands in a shell script to list the files in the ...
3
votes
3answers
91 views

md5sum check (no file)

So I'm trying to check dozens of files using a shell script. The file check happens at different times. Is there a way to do this? md5sum -c 24f4ce42e0bc39ddf7b7e879a File.name or even better ...
4
votes
4answers
45 views

Generate line number / “index” within a range from a date seed value

I'm trying to create a bash script to display a word of the day (random for each day). I have a dictionary file that on each line has a word and its definition. I'd like to use date to get a unique ...
1
vote
2answers
26 views

How to use top in a shell script to track memory consumption of a process?

I was wondering if there was a way to use top or another program to track the memory consumption of a script I will be running. I was using runtime.totalMemory - runtime.freeMemory But that'...
1
vote
3answers
49 views

Multiple logical operators, ((A || B) && C), and “syntax error near unexpected token”

I'm working with Bash 3, and I'm trying to form a conditional. In C/C++, its dead simple: ((A || B) && C). In Bash, its turning out not so (I think the Git authors must have contributed this ...
3
votes
1answer
47 views

How can I Split a text file into new files

I have a text file in the following format: 001|fileA 002|fileA 003|fileA 004|fileB 005|fileB 006|fileC . . . xxx|fileX I need to split this file into the files corresponding to each "|". The names ...
0
votes
2answers
39 views

Execute command when user interaction is detected ( ex: moving mouse, pressing key )

I am trying to make a bash script to turn down the brightness to lowest when idle for a certain amount of time but reverts back to its previous brightness when it detects user interaction whether it ...
3
votes
3answers
41 views

Proper type casting in a shell script for use with while loop and modulus

I am trying to write a script to get a random, even hex number. I have found the the openssl command has a convenient option for creating random hex numbers. Unfortunately, I need it to be even and my ...
1
vote
2answers
46 views

sed: Grabbing and replacing all data between last backslash and a comma

I have data in a csv format that looks like this: F1309-042543,07/14/09 01:39:25,N/A,C:\windows\system32\netsh.exe,N/A,True F1309-042543,11/21/10 03:24:02,N/A,C:\Windows\System32\networkexplorer.dll,...
0
votes
0answers
21 views

Ensure that file socket is already opened

I'd like to open a simple file socket that can run in background like: nc -l -k -m 10 -U my_socket -c cat & so other processes can connect into it with nc -U my_socket -c read. After this ...
0
votes
0answers
23 views

I want to write a UNIX script which reads files in a directory and if they are more than 5 days old, log the names and delete the files [duplicate]

This is the code that I tried: LOG_DIR = /home/xyz/ LOG_FNM = Delete_Logs.log files = "$(find -mtime +5)" for file in "$files"; do - exec rm{} echo "$file deleted !" >> $LOG_DIR/$...
1
vote
2answers
57 views

Creating a text file from other text files

I have a text file like this word_1 root_of_word_A word_2 root_of_word_B word_3 root_of_word_C word_4 root_of_word_A And other document like this root_of_word_A root_of_word_1 root_of_word_B ...