I have a function produce which determines whether a file is present and if not it runs the following command. This works fine when the command output simply writes to stdout. However in the command below I pipe the output to a second command and then to a third command before it outputs to stdout. In this sc…
Tag: bash
bash scripting: combine var=$(…) and var=${var%%…} lines?
Is it possible and, if yes, how to convert the following expression to one-liner? Simple DEV=${(lsblk -no KNAME,MODEL | grep ‘ModelNAME’)%%’ModelNAME’} doesn’t work Answer zsh allows you to combine parameter expansions. Bash does not. For either bash or POSIX sh (both of which su…
Running a shell script for a certain duration
I want to be able to run a script on Linux shell (bash) for a certain duration and sleep for a different duration. I’ve written something like the following snippet. However, I see only the sleep happening correctly but the execution stops after first execution. The response I get is ./script.sh: line 3…
How to GREP words, not lines, that contain specific characters, and print entire word
I have a file with tons of lines and words such as this example: What I want to do is list only the word (assuming each 4 character bundle is a word) that contains a specific number, such as 35. In this example, I would want the result printed to be: I have tried a few different ways such as
I’m getting errors in my shell script
MY CODE : THE ERRORS : Answer Your code has lots of issues. You were not consistent in your variable names (e.g. pp vs. PurPrice) You only use $ when you want the value of a variable (not on read’s for example). You can’t use strings with the integer test operators (-eq and so on). You need backti…
Bash Array Key from User Input
So I have a bash script which will scan a directory and run a function on a selected input. It seems that for some reason, the key is not setting from the array properly. What am I missing? All help is much appreciated! EDIT: As a side note, I can run the commands separately and get results. Here is the
Unable to write on /dev/* files
I’m writing a basic char device driver for Linux kernel. For this, the code flow I have considered is as follows: alloc_chrdev_region() -> to use dynamic allocation of major number class_create() -> to create device class in sysfs device_creat() -> to create device under /dev/ cdv_init() -> …
Using a glob expression passed as a bash script argument
TL;DR: Why isn’t invoking ./myscript foo* when myscript has var=$1 the same as invoking ./myscript with var=foo* hardcoded? Longer form I’ve come across a weird issue in a bash script I’m writing. I am sure there is a simple explanation, but I can’t figure it out. I am trying to pass a…
shell difference between redirect position
Is there any difference between these 2 lines? Because Robert told me that the first one only makes the I/O OP outside the for loop. But if I type tail -f /tmp/output.txt this is behaving exactly the same way. Answer They do the same if they succeed. However, there might be notable differences if they fail fo…
CGI script not executing bash commands such as ‘CP’
I have a web server (apache2) set up to execute files with a .cgi file extension. This works for some commands. However, I need it to copy the file /var/www/on.html to the location of /var/www/a1.html. I am using Debian Linux. The script is as follows: I am using cp to copy a html webpage with a green backgro…