I am trying to do some basic scripting in linux (I am a recent transfer from windows) and I am simply trying to open a directory, create either the .odt or .odp files and then open them in their default programs.
I have tried to use "cat > filename.odt" but then i dont know how to stop the writing processes and proceed to next command.
#!/bin/bash
read -p "What would you like the file name to be: " name
cat > "$name".odt
xdg-open "$name.odt"
I want to just create the odt or odp file and then open it in either of their libre programs.
If the file is supposed to be blank when you create it you can just use: touch "$name".odt rather than cat. Also you don't need the quotes around the .odt in your last line. Your new file would look like this:
#!/bin/bash
read -p "What would you like the file name to be: " name
touch "$name".odt
xdg-open "$name".odt
Related
Hi everyone this is saikrishna. I need some help in linux shell scripts. I need to open the different types of files like mp3,mp4,jpg...etc and other extensions are existing in the same folder. I had tried "gnome" code for this but it opens only one file i needed to open all the files one after the other.
is it possible in linux.need help for it
You can list multiple files using ls and then use while to open them one by one:
ls *.mp3 | while read -r file; do xdg-open "$file"; done
see this answer for more details.
I was looking to make script that can copy user files within a Windows user directory to a backup drive. I pretty much want everything except Appdata to be transferred. I made a real simple script, but since the folders I am transferring to have spaces in the name (ex. '/media/gage/Backup\ Drive/'), it says that Drive' does not exist.
I am trying to drag&drop/paste the directory from a file manager onto the terminal and it ends up having 's around the entire path once I drag it over. Is there any way to have the input recognize the file names with the 's around it?
Here's what I have so far (I'm really new to bash scripts)
#!/bin/bash
echo "Enter the full path to the user's directory"
read srcName
echo "Enter the full path to the backup directory"
read dstName
echo "Copying from Users to Backup"
cd $srcName
cp -rp Documents $dstName
cp -rp Pictures $dstName
cp -rp Desktop $dstName
cp -rp Music $dstName
cp -rp Videos $dstName
cp -rp Downloads $dstName
cp -rp Favorites $dstName
Any help is appreciated
Thanks.
Terminals predate both drag&drop and copy&paste, so neither is integrated in a robust way. It's up to each terminal emulator to decide what to do.
Here's how some common ones react when you drag&drop one or more files from a graphical file manager:
xterm does nothing.
gnome-terminal pretends you typed the paths as space separated, shell escaped words, each of which is entirely single quoted with appropriate escapes:
'/path/foo' '/path/Rock Lobster - B-52'\''s.mp3'
konsole pops up a menu to let you choose between copy/move to the current directory of the shell, or to paste the paths as shell escaped words, which are only single quoted if they contain metacharaters:
/path/foo '/path/Rock Lobster - B-52'\''s.mp3'
From what you're describing, you're using gnome-terminal and just didn't try to drag&drop a file containing single quotes to see what else it does to the filename.
So what can you do?
I would recommend you just require that the path be copy-pasted verbatim, rather than drag&dropping files. This is how every other program works, and what you can do with Charles Duffy's solution.
To copy-paste the path as a string rather than dragging a file, you can usually open a Properties or Details tab in the file manager and copy the full path from there.
However, for fun, here's how you could interpret input as drag&dropped files from a file manager if you really wanted to, by populating an array using eval:
#!/bin/bash
echo "Drag&Drop files/dirs and press enter when done."
echo "Do not drag&drop/paste/type text, because it will be evaluated as code."
IFS="" read -r input
eval "files=( $input )"
echo "Here are the things you pasted:"
for file in "${files[#]}"
do
ls -ld "$file"
done
which runs like this:
$ ./test
Drag&Drop files/dirs and press enter when done.
Do not drag&drop/paste/type text, because it will be evaluated as code.
'/usr/local/home/me/Documents' '/usr/local/home/me/Downloads'
Here are the things you pasted:
drwxr-x--- 3 me eng 4096 Aug 7 13:46 /usr/local/home/me/Documents
drwxr-x--- 2 me eng 4096 Aug 17 14:32 /usr/local/home/me/Downloads
Let's say I make a file .history.txt:
touch .history.txt
and I try to write to it:
cat > .history.txt
after having done that all I get is:
bash: .history.txt: is a directory
What I need is to be able to write some text to it like I would be able to any normal file. Any ideas what am I doing wrong?
A file doesn't need to already exist in order to redirect output to it (the shell will create the file if necessary). But Bash is telling you that .history.txt already exists and is a directory, so you can't write to it.
You either need to remove the existing directory rm -rf .history.txt or use a different file name. Then cat > .whatever.txt should work on its own.
Is there a way to "spoof" the file extension of a file in bash for consumption by another program? I can think of doing some shell scripting and making lots of soft-links, but that isn't very scalable.
Let's imagine I have a program I'm trying to use that requires input files to be of a specific file extension, and it has no method of turning off this check.
You could make a fifo with the requisite extension and cat any other file type into it. So, if your crazy program needs to see files that end in .funky, you can do this:
mkfifo file.funky
cat someotherfile > file.funky &
someprogram file.funky
Create a symbolic link for each file you want to have a particular extension, then pass the name of the symlink to the command.
For example suppose you have files with names of the form *.foo and you need to refer to them with extensions of .bar:
for file in *.foo ; do
ln -s $file _$$_$file.bar
done
I precede each symlink name with _$$_ to avoid the possibility of colliding with an existing file name (you don't want to do ln -s file.foo file.bar if file.bar already exists).
With a little more programming, your script can keep track of which symlinks it created and, if you like, clean them up after executing the command.
This assumes, as you stated in the question, that the command can't be forced to accept a different extension.
You could, without too much difficulty, create a wrapper script that replaces the command in question, creating the symlinks, invoking the command, and cleaning up after itself automatically.
I have never used applescript before and I'm trying to find out how to zip a folder on the desktop, that's all and it's giving me a hard time
If you're happy to use Applescript to just invoke sh, you can use
do shell script "zip /Users/you/Desktop/out.zip /Users/you/Desktop/in.file"
do shell script "unzip -f /Users/you/out.zip"
(The -f option is "freshen", which will stop unzip from asking if you want to overwrite files. To always overwrite, use -o.)