This forum has been archived. All content is frozen. Please use KDE Discuss instead.

KF5 - How to start kate or dolphin within a bash script

Tags: None
(comma "," separated)
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
How to start kate or dolphin within a bash script ?
KDE Plasma : 5.8.6
KDE Frameworks : 5.26.0
Qt : 5.6.1
Kernel : 4.4.57-18.3-default
System : Opensuse Leap 42.2
Os Type : 64 bit


I need to start two bash script from two desktop links.
a) The first one ( say script_a ) need to start kate as root user to edit 18 small scripts or config files.
b) The second one ( say script_b ) as root user, run a master script that call 18 small scripts. Some of these scripts sometimes may need to start kate for editing some config file when a parameter is missing or wrong.

I need to run in console to see the warnings.

I have try to put
Code: Select all
if [[ "$(id -u)" != "0" ]]; then
   exec sudo "$0" "$@"
fi
at the beginning of the script but got errors.
I have try to start the scripts with "kdesu /path/to/the/script_file" in the .desktop files but got errors.

For now, the .desktop files are configured to be started as root, run in konsole, and dbus registration is set to "run until finished"

Code: Select all
[Desktop Entry]
Comment[en_US]=my initial install edit
Comment=my initial install edit
Exec=/backup_sys/¬INSTALL/MY_SERVER/¬000-b_INSTALL_IN_PROGRESS/SCRIPT_NEED_DURING_INSTALL/00_0_0_1_A_my_initial_install_edit
GenericName[en_US]=my initial install edit
GenericName=my initial install edit
Icon=apparmor_edit_profile
MimeType=
Name[en_US]=my initial install edit
Name=my initial install edit
Path=
StartupNotify=true
Terminal=true
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=wait
X-KDE-SubstituteUID=true
X-KDE-Username=root


Below are logs from the konsole
Code: Select all
At start :
==========
Password:

After typing password
=====================
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
Config timer connecting...
Setting the name of  0x2572450  to  "org.kde.ActivityManager.RunApplication"
Setting the name of  0x25e8920  to  "org.kde.ActivityManager.Resources.Scoring"
Creating directory:  "/root/.local/share/kactivitymanagerd/resources/"
KActivities: Database connection:  "kactivities_db_resources_139739197908864_readwrite"
    query_only:          QVariant(qlonglong, 0)
    journal_mode:        QVariant(QString, "wal")
    wal_autocheckpoint:  QVariant(qlonglong, 100)
    synchronous:         QVariant(qlonglong, 1)
Setting the name of  0x260cf10  to  "org.kde.ActivityManager.ActivityTemplates"
Cleaning up...

When using "save as" in Kate for file /etc/fstab
================================================
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
klauncher not running... launching kdeinit
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
kdeinit5: Shutting down running client.
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
Connecting to deprecated signal QDBusConnectionInterface::serviceOwnerChanged(QString,QString,QString)
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
kf5.kio.core: Refilling KProtocolInfoFactory cache in the hope to find ""
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0
Qt: Session management error: networkIdsList argument is NULL
Wanting MIME Type: "inode/directory"
Wanting MIME Type: "inode/directory"
Wanting MIME Type: "inode/directory"
Wanting MIME Type: "inode/directory"
Wanting MIME Type: "inode/directory"
Wanting MIME Type: "inode/directory"
...............
...............
...............

After saved is finished
=======================
Creating the cache for:  "/etc/fstab.test"
Already in database?  true
      First update :  QDateTime(2017-04-05 15:02:42.000 CEST Qt::TimeSpec(LocalTime))
       Last update :  QDateTime(2017-04-05 15:02:42.000 CEST Qt::TimeSpec(LocalTime))
After the adjustment
     Current score :  0
      First update :  QDateTime(2017-04-05 15:02:42.000 CEST Qt::TimeSpec(LocalTime))
       Last update :  QDateTime(2017-04-05 15:02:42.000 CEST Qt::TimeSpec(LocalTime))
Interval length is  0
         New score :  1



Below is output of :
Code: Select all
superjc@MY-SERVER-LINUX:~> ls -al /run/user
total 0
drwxr-xr-x  4 root    root   80 avril  5 16:45 .
drwxr-xr-x 31 root    root  800 avril  5 16:29 ..
drwx------  7 superjc users 220 avril  5 16:49 1000
drwx------  3 sddm    sddm   60 avril  5 16:29 481
superjc@MY-SERVER-LINUX:~>


Below is output of :
Code: Select all
superjc@MY-SERVER-LINUX:~> ls -al /run/user/1000
total 4
drwx------ 7 superjc users   220 avril  5 16:49 .
drwxr-xr-x 4 root    root     80 avril  5 16:45 ..
drwx------ 2 superjc users    60 avril  5 16:30 dconf
dr-x------ 2 superjc users     0 avril  5 16:30 gvfs
drwx------ 2 superjc users    80 avril  5 16:30 ksocket-superjc
drwx------ 2 superjc users    80 avril  5 16:30 pulse
drwxr-xr-x 2 superjc users    80 avril  5 16:30 systemd
srw------- 1 superjc users     0 avril  5 16:30 kdeinit5__0
srw------- 1 superjc nogroup   0 avril  5 16:49 kdesud_:0
srwxr-xr-x 1 superjc users     0 avril  5 16:30 klauncherTJ2079.1.slave-socket
-rw-r--r-- 1 superjc users    89 avril  5 16:30 KSMserver__0


Any help is welcome.
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS
Hi!

Your approach using kdesu was a good one. If you want to start it with a .desktop-file you would have to open a separate shell.

Example:
Code: Select all
#!/bin/bash
echo Opening fstab with root permission:
kdesu --noignorebutton kate /etc/fstab &
echo kate works in background. If you do not desire it, delete the &

wich will open kate in "background".

Using a desktop-file would look like "Exec=/bin/bash -c '/path/to/script.sh'" or "Exec=/bin/bash -c '/usr/bin/kdesu /usr/bin/kate
/etc/fstab'
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
koffeinfriedhof wrote:Hi!

Your approach using kdesu was a good one. If you want to start it with a .desktop-file you would have to open a separate shell.

Example:
Code: Select all
#!/bin/bash
echo Opening fstab with root permission:
kdesu --noignorebutton kate /etc/fstab &
echo kate works in background. If you do not desire it, delete the &

wich will open kate in "background".

Using a desktop-file would look like "Exec=/bin/bash -c '/path/to/script.sh'" or "Exec=/bin/bash -c '/usr/bin/kdesu /usr/bin/kate
/etc/fstab'


I shall give news as soon as possible
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
I use a parameter when calling kate ( list of file to edit ).
It seems that parameter length is limited to about 4105 characters.
See https://forums.opensuse.org/showthread.php/524242-bash-script-parameter-max-size-lengthg?p=2819290#post2819290 for details.
Any help is welcome.
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
https://forums.opensuse.org/showthread.php/524242-bash-script-parameter-max-size-lengthg
Removing kdesu, kate start normally and all files are present.

Any help is welcome.
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS
Is it possible to workaround this size creating an temporary file?
e.g.:
Code: Select all
echo "my big list of things" >> /tmp/[timestamp]_katefile && kate /tmp/[timestamp]_katefile


Can you provide an example how it finally should work?
airdrik
Registered Member
Posts
1854
Karma
5
OS
When I hear about max command/parameter lengths in bash, my first instincts are to suggest using xargs to call your command on each item rather than passing all of them together to the next command. In this case, it would probably be easier to loop over the files and run the command to open kate on them individually. Both of these suggestions assume that running: kate /path/to/some/file when kate is already running (for some user) will open the file in a new tab in the running kate instance. The main difference being: are the file names piped in from some other command/file (use xargs), or are they specified as a glob or variable in the script (use for loop).

Code: Select all
#for loop version
for fname in /my/list /of/files /or/glob/expr*
do
kdesu /usr/bin/kate $fname &
# ampersand required here, otherwise the script will open the files one-at-a-time
# and wait for you to close kate between each one.
done

#xargs version
command which outputs filenames | xargs kdesu /usr/bin/kate


or one-liners for desktop files:
for loop:
Exec=/bin/bash -c 'for f in /path/to/files/*; do /usr/bin/kdesu /usr/bin/kate $f & done'
xargs:
Exec=/bin/bash -c 'command which outputs filenames | xargs kdesu /usr/bin/kate'

of course your command which outputs filenames could be: echo /my/list /of/files ...


airdrik, proud to be a member of KDE forums since 2008-Dec.
beojan
Registered Member
Posts
9
Karma
0
OS
Kate no longer starts as root. For your first script, you should probably use sudoedit instead. For your second, you'll need to think of a different situation.

Code: Select all
$ sudo kwrite 
Executing KWrite as root is not possible. To edit files as root use:
SUDO_EDITOR=kwrite sudoedit <file>
User avatar
Rog131
Registered Member
Posts
828
Karma
10
beojan wrote:Kate no longer starts as root. For your first script, you should probably use sudoedit instead. For your second, you'll need to think of a different situation.


That is KDE Applications 17.04 : https://www.kde.org/announcements/fulll ... .04.0#kate -> https://cgit.kde.org/kate.git/commit/?i ... 46b0e12a7e
Disallow executing kate and kwrite as root on Linux ...


but

with the KDE Frameworks 5.34 or later the password is requested if needed. No need to start the kate/kwrite with the root rights: https://phabricator.kde.org/D4847 & https://phabricator.kde.org/D5394
...With this patch kate-part will try to save the document contents with elevated privileges in case the regular save failed. So that KAuth graphical prompt is presented to user...

Image
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
koffeinfriedhof wrote:Is it possible to workaround this size creating an temporary file?
e.g.:
Code: Select all
echo "my big list of things" >> /tmp/[timestamp]_katefile && kate /tmp/[timestamp]_katefile


Can you provide an example how it finally should work?

-Create an application launcher (desktop file) then select your script : /home/some_user/bin/my_kate_launcher.sh
-Choose run in terminal (to see errors)
-Choose do not close when command exits (to see errors)
-Choose run as different user : root

Here the script :
Code: Select all
#!/bin/bash
#
# This file :  /home/some_user/bin/my_kate_launcher.sh
#
#

MY_FILE_EDIT_A=" PATH_TO_FILE_A_1  PATH_TO_FILE_A_2 .............."
MY_FILE_EDIT_B=" PATH_TO_FILE_B_1  PATH_TO_FILE_B_2 .........."

echo "$MY_FILE_EDIT_A" > /tmp/MY_FILE_EDIT_A.txt
chmod -v 777 /tmp/MY_FILE_EDIT_A.txt

echo "$MY_FILE_EDIT_B" > /tmp/MY_FILE_EDIT_B.txt
chmod -v 777 /tmp/MY_FILE_EDIT_B.txt

cat /tmp/MY_FILE_EDIT_A.txt  /tmp/MY_FILE_EDIT_B.txt > /tmp/MY_FILE_EDIT.txt
chmod -v 777 /tmp/MY_FILE_EDIT.txt

export $(dbus-launch)
/usr/bin/kate  `cat /tmp/MY_FILE_EDIT.txt`


I am running opensuse leap 42.2, KDE Frameworks: 5.26.0
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
airdrik wrote:When I hear about max command/parameter lengths in bash, my first instincts are to suggest using xargs to call your command on each item rather than passing all of them together to the next command. In this case, it would probably be easier to loop over the files and run the command to open kate on them individually. Both of these suggestions assume that running: kate /path/to/some/file when kate is already running (for some user) will open the file in a new tab in the running kate instance. The main difference being: are the file names piped in from some other command/file (use xargs), or are they specified as a glob or variable in the script (use for loop).

Code: Select all
#for loop version
for fname in /my/list /of/files /or/glob/expr*
do
kdesu /usr/bin/kate $fname &
# ampersand required here, otherwise the script will open the files one-at-a-time
# and wait for you to close kate between each one.
done

#xargs version
command which outputs filenames | xargs kdesu /usr/bin/kate


or one-liners for desktop files:
for loop:
Exec=/bin/bash -c 'for f in /path/to/files/*; do /usr/bin/kdesu /usr/bin/kate $f & done'
xargs:
Exec=/bin/bash -c 'command which outputs filenames | xargs kdesu /usr/bin/kate'

of course your command which outputs filenames could be: echo /my/list /of/files ...


Have try the loop version.
For every files, open a new instance of kate and ask for the root password
This is not interesting.
I think my solution better.
User avatar
jcdole
Registered Member
Posts
49
Karma
0
OS
beojan wrote:Kate no longer starts as root. For your first script, you should probably use sudoedit instead. For your second, you'll need to think of a different situation.

Code: Select all
$ sudo kwrite 
Executing KWrite as root is not possible. To edit files as root use:
SUDO_EDITOR=kwrite sudoedit <file>


Seems to open the file in /tmp but save the modified file also in /tmp
User avatar
zachus
Registered Member
Posts
56
Karma
0
OS

fine distinction

Sun Dec 16, 2018 9:51 pm
sudo kwrite
Executing KWrite as root is not possible. To edit files as root use:



to average Joe, these 2 cases are the same thing. :D :D :D 8-) >:D :o :-\




Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], q.ignora, watchstar