Skip to main content

Questions tagged [scripting]

A script is a sequence of commands or instructions that are executed by an interpreter program rather than compiled into a standalone executable program.

Filter by
Sorted by
Tagged with
1 vote
1 answer
2k views

Why is my udev rule not working?

I need to automatically run my script /var/www/html/configWWW when any USB is plugged in to my Raspberry. UDEV RULE - /etc/udev/rules.d/myRule.rules ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="...
-2 votes
1 answer
79 views

Linux Bash Script - Yes or No - read answer

little script #!/bin/sh cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 4 printf 'do you like this? (y/n)? ' read answer if [ "$answer" != "${answer#[Yy]}" ] ;then ...
0 votes
2 answers
78 views

How to write an alias or bash script that renames a single file using the same syntax as the `ren` command of Windows?

I am a long-term a Windows user, so I find the syntax of the ren command both simple and convenient: ren C:\pathTo\myFile\oldFileName.txt newFileName.txt I want to be able to use the same syntax on ...
2 votes
1 answer
3k views

Add color to specific lines of a bash script

I'm trying to make a "Cryptocurrency news" script using curl and so far it works, but not completely the way I intended. Here is the code for the main part of the script: curl -s https://www....
1 vote
2 answers
350 views

Change color of a certain lines from an output bash

I have this output from a drive diag command. Slot Number : 0 Drive's position : DiskGroup 0 Media Error : 0 Other Error : 0 Predictive Failure : 0 Raw Size ...
12 votes
5 answers
22k views

How do you colorize only some keywords for a bash script?

I am running some unit test code. The unit test code outputs regular text. There is a lot of the text so I want to highlight for the user important keywords. In this case the keywords are "PASS" and "...
1 vote
3 answers
36 views

Bash: Access variable with another variable [duplicate]

How can I access a variable with another variable, like this? varAble="Hello" varA="\n" varBable="World" varB="END" for part in A, B do echo var${part}ble ...
0 votes
0 answers
17 views

Need some help doing a file copying script [closed]

I am not a programmer, I can do basic scripting but this is very far from my skillset. I am setting up an HTPC settop box for a relative that has no internet at their home, no networking either. ...
0 votes
2 answers
21 views

How do I update the contents of a script including comments (django/settings.py) using echo, printf or grep

I would like to overwrite a Django/app/settings.py text file using the Linux terminal command printf. The settings.py starts with docstrings comments, i.e. """some text"""...
0 votes
0 answers
23 views

SLURM job script - why is the tmp local directory deleted before archiving can occur? How to prevent this?

I wrote a SLURM job script to run a computational chemistry calculation using the CREST program (part of the xtb software package). In the script, I create a temporary directory on the local storage ...
2 votes
2 answers
4k views

Shell script to check existence of multiple strings in a file

I have 3 strings A='apples' B='bananas' C='carrots' I want to see if all of these exist in the fruit.txt file. If I'm missing A then add A, B then add B, and so on. This is what I have now if grep -...
-4 votes
3 answers
73 views

show which of a list of filenames do not exist in a given list of directories

I have a long list of filenames like 20231121_150111.jpg... in a text file (one filename per line). And another list of directories. Those directories contain files. How can I check and show which ...
14 votes
8 answers
68k views

How to read certain lines after finding some text?

How can I read and print a certain number of lines after I find some text? E.g.: Read next 2 lines after finding "Unix" in: Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test ...
1 vote
0 answers
25 views

Running a program/script before a user logs in on RedHat Linux GUI

I am looking to run a script prior to a user being able to log in on the GUI. Its a basic script, do you accept Ts&Cs Y/N, enter user name, data is captured and stored. Then once complete takes ...
30 votes
6 answers
47k views

Answer yes in a bash script

I'm trying to do a git clone trough a bash script, but the first time that I run the script and the server is not known yet the script fails. I have something like this: yes | git clone git@github....
5 votes
5 answers
735 views

how to format a text file in bash with dots from right

I have a file on a Linux system with various numbers, all on a single line (numbers_in_one_line.txt): 1941102556 1750145810 2604905 7000793682 5160065824 3000350768 6449300295 3046118928 12693055728 ...
-3 votes
1 answer
86 views

How to split a number into unequal parts in Bash

Hypothetic example: There is a number that I will split into x parts (smaller numbers, in a range you see in the example). I need a way, ideally a one liner for the Linux Bash, to split a large number ...
1 vote
1 answer
2k views

Setting environment variable using shell script

I am using csh scripts for some automation purpose. I have a number of scripts, and the beginning of all of which I need to set a fixed set of environment variables. |----SCRIPT 1---| |----SCRIPT ...
0 votes
1 answer
2k views

permission needed for Python to access USB drive?

I got this Python script that reads media files from USB stick. It used to run fine, today it keeps giving me OS Error: [Errno 13] Permission denied: media/myName/usbName when the script runs the ...
11 votes
6 answers
78k views

Linux Bash Shell Script Error: cannot execute: required file not found

I have two similar scripts with different names. One works fine but other throws error. Can anyone please tell me what is the issue? This is my test.sh scripts which works fine [nnice@myhost Scripts]$ ...
1 vote
2 answers
23k views

Systemd service fails with exit-code 2: Start request repeated too quickly

I created a ClamAV on-access scanning systemd service, to be permanent so that I can enable, start and then leave it, without having to touch it again. I can manually start the service, and it works ...
0 votes
3 answers
12k views

Create bash script for ping

Good evening I would need a script in bash that follows this command: ping -c 4 -i "IP" for 3 times. Each ping must be performed at a distance of 10 minutes and only when it fails all 3 times, it send ...
3 votes
2 answers
3k views

How to clone a read-only portion of /proc file system

The problem with reading /proc file system is that it is not atomic. I tried using a library (procfs in Python) that wishes to read it, but it has glitches about 5% of the time because the process ...
1 vote
1 answer
2k views

Problem with inotify-wait as daemon

I have been running inotify-wait on my ftp server in byobu to monitor a folder, move data, and send email notifications for a few months without problems. Since I started this I have added 3 more ...
2 votes
2 answers
3k views

Bash script to sort files into alphabetical folders

I try to organize my files and my folders by a script. It creates the folder with the first letter of my file and moves it there. For the very first time, the script does the job, but if I create new ...
1 vote
3 answers
454 views

How to sort files by size in bash script?

I want to loop over all files in directory on macOS and perform some operations on each file, and do this based on their size, going from big to small. To create a list I can loop over I do this: ...
-1 votes
0 answers
43 views

What command can I use to write a script that automatically creates 5 files at a time? [duplicate]

I have a directory I'm working with and I am trying my hands to create 5 files fils1.txt file2.txt....file5.txt to automatically be under this directory, how do I do it
5 votes
3 answers
16k views

Create an iptables rule if the rule does not already exist

Hi I've created a script to setup Nagios on my remote servers. My script checks iptables to see if a firewall rule already exists and then is supposed to add the rule if it's not already there. ...
3 votes
5 answers
2k views

How to remove spaces at the end of folder names?

I would like to delete the spaces at the end of my folder names. I have a folder called "Project1 " in which I have another folder called "Exercise1 ". I want to use a script to ...
3 votes
1 answer
4k views

How to kill inactive user logins on Solaris?

On a Solaris 10 server if i do a w, I get this: 12:26pm up 85 day(s), 1:13, 6 users, load average: 0.38, 0.36, 0.38 User tty login@ idle JCPU PCPU what blah pts/1 ...
3 votes
1 answer
3k views

How do you make a toggle key using Autokey?

I want to use Autokey to toggle the e key on and off in a program called "xyz". So if I press e, the program thinks I'm holding down e. I press e again, it thinks I released it. How do you do this ...
0 votes
1 answer
2k views

Open URL in new firefox window not tab in a bash script

I want to open a URL in Firefox always in a new Window and not in a new tab. It is on my raspberry PI on iceweasel and I just can't find a soultion for my problem. What do I have: firefox -private-...
0 votes
1 answer
59 views

Bash script to find certain files and add their names to another file

When I find a file whose size is 12kb I want to copy the file name to new file input.xml #!/bin/bash if [ $(find /root -type f -size +12k) ] then grep -r -i -s "the name of file to find it&...
1 vote
1 answer
4k views

bash script for cdo command to run it on different files

I am new in UNIX and I want to apply the CDO (Climate Data Operator) "remapbil" command in order to transform the grid (from a projection to geographical coordinate system). it works perfectly fine ...
5 votes
1 answer
15k views

How to determine interface RUNNING state without ifconfig?

Some background: sometimes my wi-fi connection suddenly goes down so I type sudo ifconfig wlan0 and check for the word RUNNING. The corresponding line looks like: UP BROADCAST RUNNING MULTICAST [...]...
5 votes
2 answers
379 views

i3blocks.conf create onclick event (open firefox with URL)

I have this block in /etc/i3blocks.conf [rss] command=rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50 color=#00FF00 interval=300 I'd like to ...
82 votes
4 answers
63k views

$@ except the 1st argument

I need to write a shell script that runs in this way: ./myscript arg1 arg2_1 arg2_2 arg2_3 ....... arg2_# there is a for loop inside script for i in $@ However, as I know, $@ includes $1 up to $(...
0 votes
1 answer
113 views

ShellCheck does not detect error? How to fix my snippet?

I likely have syntax errors in the following (POSIX) shell snippet: #!/bin/sh if tput setaf >/dev/null 2>&1; then tput_init_linux else tput_init_none; elif tput AF >/dev/null 2>&1;...
10 votes
4 answers
2k views

Inverting an associative array

Let's say I have an associative array in bash, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values ...
0 votes
0 answers
59 views

keep sudo effective without password after update sudo package itself

I want to write an automation script in Bash that requires frequent use of the sudo command. To avoid entering my password repeatedly after the sudo timeout, I've tried the following: #!/bin/bash sudo ...
-1 votes
2 answers
105 views

how to de-duplicate block (timestamp+command) from bash history?

I'm working with bash_history file containing blocks with the following format: #unixtimestamp\ncommand\n here's sample of the bash_history file: #1713308636 cat > ./initramfs/init << "...
1 vote
1 answer
2k views

how to get multiple command output as variable in awk

I am trying to execute 3 command and get the output as a variable which needs some percentage calculation and display the same as the example. can this be executed as script. First command output $ ...
-1 votes
2 answers
76 views

I need to filter out `grep`'s output to find a device's serial number, in order to store it into a variable

I'm working on a section of a script that will find the serial number of a Mac, store it into a variable and rename the device according to the naming standards of my workplace. So far, I have the ...
2 votes
2 answers
2k views

Copying/Moving folders where destination folder already exists

I am writing a bash script to copy/move a folder called "folder" to a directory that already contains "folder" and I would like the contents to be merged. I am attempting to use a solution from this ...
0 votes
0 answers
19 views

Remove PDF pages if content is subset of next page

I have a lot of uni-slides where a list of bullet points gets shown page by page until one slide contains all points. For the sake of explaining a topic this is quite nice but for learning I would ...
199 votes
6 answers
508k views

how can I add (subtract, etc.) two numbers with bash?

I can read the numbers and operation in with: echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op but then all my attempts to add the numbers fail: ...
108 votes
5 answers
245k views

Bash throws error, line 8: $1: unbound variable

I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage ...
0 votes
0 answers
16 views

Process names that begin with "-" [duplicate]

I am using a piece of software that includes a scripting language that has builtins that allow me to call system functions, for example I may write the script: system("whoami") and the ...
1 vote
2 answers
188 views

Compile a script, add it to system so it runs before (or during) system boot

I really don't know how to go about this so I'm asking you guys here for help Long story short with some background details I have a USB3 2.5gb NIC attached to my Raspberry Pi 5 (this SBC also has its ...
105 votes
7 answers
239k views

Using while loop to ssh to multiple servers

I have a file servers.txt, with list of servers: server1.mydomain.com server2.mydomain.com server3.mydomain.com when I read the file line by line with while and echo each line, all works as expected....

1
2 3 4 5
90