How can I create a filename using bash command parameter? [duplicate] - bash

This question already has answers here:
How to use > in an xargs command?
(4 answers)
Closed 2 years ago.
I'm trying to create a filename using command param as such but not sure how to go about doing it. This is what I was trying:
echo "AZ" |xargs date >> $1.txt
I'm trying to create a file named AZ.txt with the date in it.

If you must use xargs, then you'll have to wrap the rest of it in a shell script to delay execution of the redirection until you have constructed the filename:
echo AZ | xargs sh -c 'date >> "$1".txt' sh
The trailing "sh" is to force the xargs argument "AZ" into $1 instead of $0

If the name of the file comes from some command, you can use command substitution:
date > "$(echo AZ)".txt

Related

How do I pipe into printf? [duplicate]

This question already has answers here:
Piping not working with echo command [duplicate]
(4 answers)
How to pass command output as multiple arguments to another command
(5 answers)
Closed 1 year ago.
I'm going nuts trying to understand what is the problem with this simple example (zsh or bash):
echo -n "6842" | printf "%'d"
The output is 0... why though? I'd like the output to be 6,842
Thanks in advance, I've had no luck for an hour now using google trying to figure this out...!
printf doesn't read arguments to format from standard input, but from the command line directly. For example, this works:
$ printf "%'d" 6842
6,842
You can convert output of a command to command-line arguments using command substitution:
$ printf "%'d" $(echo -n 6842)
6,842
If you want to invoke printf inside a pipeline, you can use xargs to read input and execute printf with the appropriate arguments:
echo -n "6842" | xargs printf "%'d"
printf does not format data passed to it on standard input; it takes a set of arguments, the first of which is the format, and the remainder are the values to display.
Luckily, this is exactly what xargs is for; to quote the manual:
xargs - build and execute command lines from standard input
So instead of piping to printf directly, you can pipe to xargs, and tell it to run printf for you with the given arguments. In short:
echo -n "6842" | xargs printf "%'d"

How to strip paths from file names on standard input [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Read file line by line and perform action for each in bash
(3 answers)
Closed 3 years ago.
How could I strip the paths from a list of file names on standard input? I basically need something like basename that iterates over standard input.
while read path; do basename "$path" ; done
Simple loop.
Try xargs basename -a. It'll run faster than a simple loop as basename won't be run for each filename.
$ printf "%s\n" foo/bar bar/baz | xargs basename -a
bar
baz
Note that the above won't correctly handle filenames with spaces. You'll want to use the -0 option to xargs and ensure the input is NUL terminated.
$ printf "%s\0" 'foo/foo bar' bar/baz | xargs -0 basename -a
foo bar
baz
According to this answer
If your filename is in the form of a variable such as $fullpathname
nopathfile="${fullpathname##*/}"
It's in the bash manpage at the section called "Parameter Expansion"

In shell script, how do I use the contents of a file as a parameter [duplicate]

This question already has answers here:
Need to assign the contents of a text file to a variable in a bash script
(4 answers)
Closed 4 years ago.
Suppose in dir.txt I have the following content:
test-dir
I try to use that as a parameter as follows:
echo dir.txt | cp * $1
I want the above to be the equivalent of:
cp * test-dir
What am I doing wrong?
You are giving the string "dir.txt" to a program that does not accept any input by stdin.
You are looking for the following syntax:
cp * "$(<dir.txt)"
$() runs the command inside parenthesis and substitutes its results in its position in the command line. The < is a shorthand to read a file (a cat would also work). The quotes are to avoid problems with spaces.
You can get content of file to variable:
file1=$(cat dir.txt)
echo $file1
Results:
test-dir

Obtain the last command executed by bash, inside a perl script [duplicate]

This question already has answers here:
What's the difference between Perl's backticks, system, and exec?
(5 answers)
How can I store Perl's system function output to a variable?
(8 answers)
Closed 5 years ago.
I would like to write a perl script that do something similar to what fc do. Indeed I want to edit programmatically the last command executed.
Something like this should work
perl -wle 'print system("/bin/bash", "-i", "-c", "history | tail -n 1")'
but system return the exit status of the command executed, not the stdout, that is what I want, while I'm not able to activate the history (-i flag) using back-tic, qx// or opening a pipe.
I know that using back-tic, qx// or opening a pipe it is simple to read the stdout of the command, but int this case how to use the builtin bash hisotry command properly?
Even using system and passing -i to bash, I'm not able to get the expected output from history | tail -n 1. Bi redirecting the output to a file I found its content empty.
perl -wle 'print system("/bin/bash", "-i", "-c", "history | tail -n 1 > /tmp/pippo")'
So am I forced to write the bash history on a file whit history -w and to read that file inside perl?
Use qx to capture shell command output. Read the ~/.bash_history file directly since you cannot capture the output of the shell builtin function history.
You may need to add history -a to your .bashrc file to get bash to write the history file after every command, or add history -w to your bash PROMPT_COMMAND. Other settings are covered nicely over at unix.stackexchange.com.
$ echo "foo"
foo
$ perl -e 'chomp(my $cmd = qx(tail -n 1 ~/.bash_history)); print "$cmd bar\n";'
echo "foo" bar

Command errors when run with #!/bin/sh [duplicate]

This question already has answers here:
Doesn't sh support process substitution <(...)?
(3 answers)
Closed 6 years ago.
Command inside a shell script:
mapfile -t my_array < <( grep '$(ABC)' ${file} |awk -F_ '{print $2}')
Fails when run with #!/bin/sh.
error: syntax error near unexpected token `<'
The same command works with #!/bin/bash.
Process substitution is not a feature present in POSIX sh; it is a ksh extension also adopted by bash and zsh.
Using #!/bin/sh only guarantees presence of functionality given in the POSIX sh specification.

Resources